imageutils.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handler
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "io"
  20. "mime/multipart"
  21. "net/http"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "yunion.io/x/jsonutils"
  26. "yunion.io/x/log"
  27. "yunion.io/x/pkg/errors"
  28. "yunion.io/x/pkg/util/stringutils"
  29. "yunion.io/x/pkg/utils"
  30. "yunion.io/x/onecloud/pkg/appsrv"
  31. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  32. "yunion.io/x/onecloud/pkg/httperrors"
  33. "yunion.io/x/onecloud/pkg/mcclient"
  34. "yunion.io/x/onecloud/pkg/mcclient/auth"
  35. modules "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  36. )
  37. var IMAGE_DOWNLOAD_PUBLIC_KEY = stringutils.UUID4()
  38. func readImageForm(r *multipart.Reader) (map[string]string, *multipart.Part, error) {
  39. params := make(map[string]string)
  40. maxValueBytes := int64(10 << 20)
  41. for {
  42. p, err := r.NextPart()
  43. if err == io.EOF {
  44. break
  45. }
  46. if err != nil {
  47. return nil, nil, err
  48. }
  49. name := p.FormName()
  50. if name == "" {
  51. continue
  52. }
  53. filename := p.FileName()
  54. var b bytes.Buffer
  55. _, hasContentTypeHeader := p.Header["Content-Type"]
  56. if !hasContentTypeHeader && filename == "" {
  57. // value, store as string in memory
  58. n, err := io.CopyN(&b, p, maxValueBytes+1)
  59. if err != nil && err != io.EOF {
  60. return nil, nil, err
  61. }
  62. maxValueBytes -= n
  63. if maxValueBytes < 0 {
  64. return nil, nil, multipart.ErrMessageTooLarge
  65. }
  66. params[name] = b.String()
  67. continue
  68. }
  69. if name == "image" || name == "file" {
  70. return params, p, nil
  71. } else {
  72. return nil, nil, fmt.Errorf("no file uploaded")
  73. }
  74. }
  75. return nil, nil, fmt.Errorf("empty form")
  76. }
  77. func imageUploadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  78. const (
  79. invalidForm = "invalid form"
  80. )
  81. reader, e := r.MultipartReader()
  82. if e != nil {
  83. httperrors.InvalidInputError(ctx, w, invalidForm)
  84. return
  85. }
  86. p, f, e := readImageForm(reader)
  87. if e != nil {
  88. httperrors.InvalidInputError(ctx, w, invalidForm)
  89. return
  90. }
  91. params := jsonutils.NewDict()
  92. name, ok := p["name"]
  93. if !ok {
  94. httperrors.InvalidInputError(ctx, w, "missing image name")
  95. return
  96. }
  97. params.Add(jsonutils.NewString(name), "name")
  98. _imageSize, ok := p["image_size"]
  99. if !ok {
  100. httperrors.InvalidInputError(ctx, w, "missing image size")
  101. return
  102. }
  103. imageSize, e := strconv.ParseInt(_imageSize, 10, 64)
  104. if e != nil {
  105. httperrors.InvalidInputError(ctx, w, "invalid image size")
  106. return
  107. }
  108. properties := map[string]string{}
  109. prefix := "properties."
  110. // add all other params
  111. for k, v := range p {
  112. if k == "name" || k == "image_size" {
  113. continue
  114. }
  115. if strings.HasPrefix(k, prefix) {
  116. properties[strings.TrimPrefix(k, prefix)] = v
  117. continue
  118. }
  119. params.Add(jsonutils.NewString(v), k)
  120. }
  121. if len(properties) > 0 {
  122. params.Set("properties", jsonutils.Marshal(properties))
  123. }
  124. token := AppContextToken(ctx)
  125. s := auth.GetSession(ctx, token, FetchRegion(r))
  126. res, e := modules.Images.Upload(s, params, f, imageSize)
  127. if e != nil {
  128. httperrors.GeneralServerError(ctx, w, e)
  129. return
  130. } else {
  131. appsrv.SendJSON(w, res)
  132. }
  133. }
  134. func imageDownloadValidateStatus(s *mcclient.ClientSession, id string, format string) error {
  135. var image jsonutils.JSONObject
  136. var err error
  137. if len(format) == 0 {
  138. image, err = modules.Images.Get(s, id, nil)
  139. if err != nil {
  140. return errors.Wrap(err, "images.get")
  141. }
  142. } else {
  143. resp, e := modules.Images.GetSpecific(s, id, "subformats", nil)
  144. if e != nil {
  145. return errors.Wrap(e, "images.get.subformats")
  146. }
  147. images, _ := resp.(*jsonutils.JSONArray).GetArray()
  148. for i := range images {
  149. if f, _ := images[i].GetString("format"); f == format {
  150. image = images[i]
  151. }
  152. }
  153. }
  154. status, _ := image.GetString("status")
  155. if status != "active" {
  156. return httperrors.NewInvalidStatusError("image is not in status 'active'")
  157. }
  158. return nil
  159. }
  160. func imageDownloadUrl(id string, format string) (jsonutils.JSONObject, error) {
  161. // 加密下载url
  162. expired := time.Now().Add(24 * time.Hour)
  163. imageInfo := jsonutils.NewDict()
  164. imageInfo.Set("id", jsonutils.NewString(id))
  165. imageInfo.Set("format", jsonutils.NewString(format))
  166. imageInfo.Set("expired", jsonutils.NewInt(expired.Unix()))
  167. token, err := utils.EncryptAESBase64Url(IMAGE_DOWNLOAD_PUBLIC_KEY, imageInfo.String())
  168. if err != nil {
  169. return nil, httperrors.NewGeneralError(err)
  170. }
  171. ret := jsonutils.NewDict()
  172. ret.Set("signature", jsonutils.NewString(token))
  173. return ret, nil
  174. }
  175. func imageDownload(ctx context.Context, w http.ResponseWriter, s *mcclient.ClientSession, id string, format string) {
  176. meta, body, size, err := modules.Images.Download2(s, id, format, false)
  177. if err != nil {
  178. httperrors.GeneralServerError(ctx, w, err)
  179. return
  180. }
  181. name, _ := meta.GetString("name")
  182. if len(name) == 0 {
  183. format, _ := meta.GetString("disk_format")
  184. name = "os_image"
  185. if len(format) > 0 {
  186. name += "." + format
  187. }
  188. }
  189. hdr := http.Header{}
  190. hdr.Set("Content-Type", "application/octet-stream")
  191. hdr.Set("Content-Disposition", fmt.Sprintf("Attachment; filename=%s", name))
  192. appsrv.SendStream(w, false, hdr, body, size)
  193. return
  194. }
  195. func imageDownloadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  196. params, query, _ := appsrv.FetchEnv(ctx, w, r)
  197. token := AppContextToken(ctx)
  198. s := auth.GetSession(ctx, token, FetchRegion(r))
  199. // input params
  200. imageId, ok := params["<image_id>"]
  201. if !ok || len(imageId) == 0 {
  202. httperrors.MissingParameterError(ctx, w, "image_id")
  203. return
  204. }
  205. // 是否直接下载
  206. format, _ := query.GetString("format")
  207. err := imageDownloadValidateStatus(s, imageId, format)
  208. if err != nil {
  209. httperrors.GeneralServerError(ctx, w, err)
  210. return
  211. }
  212. direct, _ := query.Bool("direct")
  213. if !direct {
  214. ret, err := imageDownloadUrl(imageId, format)
  215. if err != nil {
  216. httperrors.GeneralServerError(ctx, w, err)
  217. return
  218. }
  219. appsrv.SendJSON(w, ret)
  220. return
  221. }
  222. // 直接下载镜像
  223. imageDownload(ctx, w, s, imageId, format)
  224. }
  225. func imageDownloadByUrlHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  226. _, query, _ := appsrv.FetchEnv(ctx, w, r)
  227. // input params
  228. token, err := query.GetString("signature")
  229. if len(token) == 0 {
  230. log.Debugf("get signature %s", err)
  231. httperrors.MissingParameterError(ctx, w, "signature")
  232. return
  233. }
  234. data, err := utils.DescryptAESBase64Url(IMAGE_DOWNLOAD_PUBLIC_KEY, token)
  235. if err != nil {
  236. httperrors.InputParameterError(ctx, w, "invalid download token")
  237. return
  238. }
  239. d, _ := jsonutils.ParseString(data)
  240. id, _ := d.GetString("id")
  241. if err != nil || len(id) == 0 {
  242. httperrors.InputParameterError(ctx, w, "invalid download token")
  243. return
  244. }
  245. expired, _ := d.Int("expired")
  246. if time.Now().Unix() > expired {
  247. httperrors.BadRequestError(ctx, w, "image download url is expired")
  248. return
  249. }
  250. format, _ := d.GetString("format")
  251. s := auth.GetAdminSession(ctx, consts.GetRegion())
  252. imageDownload(ctx, w, s, id, format)
  253. }
  254. func uploadHandlerInfo(method, prefix string, handler func(context.Context, http.ResponseWriter, *http.Request)) *appsrv.SHandlerInfo {
  255. log.Debugf("%s - %s", method, prefix)
  256. hi := appsrv.SHandlerInfo{}
  257. hi.SetMethod(method)
  258. hi.SetPath(prefix)
  259. hi.SetHandler(handler)
  260. hi.SetProcessTimeout(6 * time.Hour)
  261. hi.SetWorkerManager(GetUploaderWorker())
  262. return &hi
  263. }