You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment.go 23 kB

4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. contexExt "context"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "mime/multipart"
  11. "net/http"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "code.gitea.io/gitea/models"
  16. "code.gitea.io/gitea/modules/context"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/minio_ext"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/storage"
  21. "code.gitea.io/gitea/modules/upload"
  22. "code.gitea.io/gitea/modules/worker"
  23. gouuid "github.com/satori/go.uuid"
  24. )
  25. const (
  26. //result of decompress
  27. DecompressSuccess = "0"
  28. DecompressFailed = "1"
  29. )
  30. type CloudBrainDataset struct {
  31. UUID string `json:"id"`
  32. Name string `json:"name"`
  33. Path string `json:"place"`
  34. UserName string `json:"provider"`
  35. CreateTime string `json:"created_at"`
  36. }
  37. type UploadForm struct {
  38. UploadID string `form:"uploadId"`
  39. UuID string `form:"uuid"`
  40. PartSize int64 `form:"size"`
  41. Offset int64 `form:"offset"`
  42. PartNumber int `form:"chunkNumber"`
  43. PartFile multipart.File `form:"file"`
  44. }
  45. func RenderAttachmentSettings(ctx *context.Context) {
  46. renderAttachmentSettings(ctx)
  47. }
  48. func renderAttachmentSettings(ctx *context.Context) {
  49. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  50. ctx.Data["AttachmentStoreType"] = setting.Attachment.StoreType
  51. ctx.Data["AttachmentAllowedTypes"] = setting.Attachment.AllowedTypes
  52. ctx.Data["AttachmentMaxSize"] = setting.Attachment.MaxSize
  53. ctx.Data["AttachmentMaxFiles"] = setting.Attachment.MaxFiles
  54. }
  55. // UploadAttachment response for uploading issue's attachment
  56. func UploadAttachment(ctx *context.Context) {
  57. if !setting.Attachment.Enabled {
  58. ctx.Error(404, "attachment is not enabled")
  59. return
  60. }
  61. file, header, err := ctx.Req.FormFile("file")
  62. if err != nil {
  63. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  64. return
  65. }
  66. defer file.Close()
  67. buf := make([]byte, 1024)
  68. n, _ := file.Read(buf)
  69. if n > 0 {
  70. buf = buf[:n]
  71. }
  72. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.Attachment.AllowedTypes, ","))
  73. if err != nil {
  74. ctx.Error(400, err.Error())
  75. return
  76. }
  77. datasetID, _ := strconv.ParseInt(ctx.Req.FormValue("dataset_id"), 10, 64)
  78. attach, err := models.NewAttachment(&models.Attachment{
  79. IsPrivate: true,
  80. UploaderID: ctx.User.ID,
  81. Name: header.Filename,
  82. DatasetID: datasetID,
  83. }, buf, file)
  84. if err != nil {
  85. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  86. return
  87. }
  88. log.Trace("New attachment uploaded: %s", attach.UUID)
  89. ctx.JSON(200, map[string]string{
  90. "uuid": attach.UUID,
  91. })
  92. }
  93. func UpdatePublicAttachment(ctx *context.Context) {
  94. file := ctx.Query("file")
  95. isPrivate, _ := strconv.ParseBool(ctx.Query("is_private"))
  96. attach, err := models.GetAttachmentByUUID(file)
  97. if err != nil {
  98. ctx.Error(404, err.Error())
  99. return
  100. }
  101. attach.IsPrivate = isPrivate
  102. models.UpdateAttachment(attach)
  103. }
  104. // DeleteAttachment response for deleting issue's attachment
  105. func DeleteAttachment(ctx *context.Context) {
  106. file := ctx.Query("file")
  107. attach, err := models.GetAttachmentByUUID(file)
  108. if err != nil {
  109. ctx.Error(400, err.Error())
  110. return
  111. }
  112. if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
  113. ctx.Error(403)
  114. return
  115. }
  116. err = models.DeleteAttachment(attach, false)
  117. if err != nil {
  118. ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
  119. return
  120. }
  121. ctx.JSON(200, map[string]string{
  122. "uuid": attach.UUID,
  123. })
  124. }
  125. func DownloadUserIsOrgOrCollaboration(ctx *context.Context, attach *models.Attachment) bool {
  126. dataset, err := models.GetDatasetByID(attach.DatasetID)
  127. if err != nil {
  128. log.Info("query dataset error")
  129. } else {
  130. repo, err := models.GetRepositoryByID(dataset.RepoID)
  131. if err != nil {
  132. log.Info("query repo error.")
  133. } else {
  134. repo.GetOwner()
  135. if ctx.User != nil {
  136. if repo.Owner.IsOrganization() {
  137. if repo.Owner.IsUserPartOfOrg(ctx.User.ID) {
  138. log.Info("org user may visit the attach.")
  139. return true
  140. }
  141. }
  142. isCollaborator, _ := repo.IsCollaborator(ctx.User.ID)
  143. if isCollaborator {
  144. log.Info("Collaborator user may visit the attach.")
  145. return true
  146. }
  147. }
  148. }
  149. }
  150. return false
  151. }
  152. // GetAttachment serve attachements
  153. func GetAttachment(ctx *context.Context) {
  154. typeCloudBrain := ctx.QueryInt("type")
  155. err := checkTypeCloudBrain(typeCloudBrain)
  156. if err != nil {
  157. ctx.ServerError("checkTypeCloudBrain failed", err)
  158. return
  159. }
  160. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  161. if err != nil {
  162. if models.IsErrAttachmentNotExist(err) {
  163. ctx.Error(404)
  164. } else {
  165. ctx.ServerError("GetAttachmentByUUID", err)
  166. }
  167. return
  168. }
  169. repository, unitType, err := attach.LinkedRepository()
  170. if err != nil {
  171. ctx.ServerError("LinkedRepository", err)
  172. return
  173. }
  174. dataSet, err := attach.LinkedDataSet()
  175. if err != nil {
  176. ctx.ServerError("LinkedDataSet", err)
  177. return
  178. }
  179. if repository == nil && dataSet != nil {
  180. repository, _ = models.GetRepositoryByID(dataSet.RepoID)
  181. unitType = models.UnitTypeDatasets
  182. }
  183. if repository == nil { //If not linked
  184. //if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) && attach.IsPrivate { //We block if not the uploader
  185. //log.Info("ctx.IsSigned =" + fmt.Sprintf("%v", ctx.IsSigned))
  186. if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) && attach.IsPrivate && !DownloadUserIsOrgOrCollaboration(ctx, attach) { //We block if not the uploader
  187. ctx.Error(http.StatusNotFound)
  188. return
  189. }
  190. } else { //If we have the repository we check access
  191. perm, errPermission := models.GetUserRepoPermission(repository, ctx.User)
  192. if errPermission != nil {
  193. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", errPermission.Error())
  194. return
  195. }
  196. if !perm.CanRead(unitType) {
  197. ctx.Error(http.StatusNotFound)
  198. return
  199. }
  200. }
  201. if dataSet != nil {
  202. isPermit, err := models.GetUserDataSetPermission(dataSet, ctx.User)
  203. if err != nil {
  204. ctx.Error(http.StatusInternalServerError, "GetUserDataSetPermission", err.Error())
  205. return
  206. }
  207. if !isPermit {
  208. ctx.Error(http.StatusNotFound)
  209. return
  210. }
  211. }
  212. //If we have matched and access to release or issue
  213. if setting.Attachment.StoreType == storage.MinioStorageType {
  214. url := ""
  215. if typeCloudBrain == models.TypeCloudBrainOne {
  216. url, err = storage.Attachments.PresignedGetURL(setting.Attachment.Minio.BasePath+attach.RelativePath(), attach.Name)
  217. if err != nil {
  218. ctx.ServerError("PresignedGetURL", err)
  219. return
  220. }
  221. } else {
  222. url, err = storage.ObsGetPreSignedUrl(attach.UUID, attach.Name)
  223. if err != nil {
  224. ctx.ServerError("ObsGetPreSignedUrl", err)
  225. return
  226. }
  227. }
  228. if err = increaseDownloadCount(attach, dataSet); err != nil {
  229. ctx.ServerError("Update", err)
  230. return
  231. }
  232. http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
  233. } else {
  234. fr, err := storage.Attachments.Open(attach.RelativePath())
  235. if err != nil {
  236. ctx.ServerError("Open", err)
  237. return
  238. }
  239. defer fr.Close()
  240. if err = increaseDownloadCount(attach, dataSet); err != nil {
  241. ctx.ServerError("Update", err)
  242. return
  243. }
  244. if err = ServeData(ctx, attach.Name, fr); err != nil {
  245. ctx.ServerError("ServeData", err)
  246. return
  247. }
  248. }
  249. }
  250. func increaseDownloadCount(attach *models.Attachment, dataSet *models.Dataset) error {
  251. if err := attach.IncreaseDownloadCount(); err != nil {
  252. return err
  253. }
  254. if dataSet != nil {
  255. if err := models.IncreaseDownloadCount(dataSet.ID); err != nil {
  256. return err
  257. }
  258. }
  259. return nil
  260. }
  261. // Get a presigned url for put object
  262. func GetPresignedPutObjectURL(ctx *context.Context) {
  263. if !setting.Attachment.Enabled {
  264. ctx.Error(404, "attachment is not enabled")
  265. return
  266. }
  267. err := upload.VerifyFileType(ctx.Params("file_type"), strings.Split(setting.Attachment.AllowedTypes, ","))
  268. if err != nil {
  269. ctx.Error(400, err.Error())
  270. return
  271. }
  272. if setting.Attachment.StoreType == storage.MinioStorageType {
  273. uuid := gouuid.NewV4().String()
  274. url, err := storage.Attachments.PresignedPutURL(models.AttachmentRelativePath(uuid))
  275. if err != nil {
  276. ctx.ServerError("PresignedPutURL", err)
  277. return
  278. }
  279. ctx.JSON(200, map[string]string{
  280. "uuid": uuid,
  281. "url": url,
  282. })
  283. } else {
  284. ctx.Error(404, "storage type is not enabled")
  285. return
  286. }
  287. }
  288. // AddAttachment response for add attachment record
  289. func AddAttachment(ctx *context.Context) {
  290. typeCloudBrain := ctx.QueryInt("type")
  291. err := checkTypeCloudBrain(typeCloudBrain)
  292. if err != nil {
  293. ctx.ServerError("checkTypeCloudBrain failed", err)
  294. return
  295. }
  296. uuid := ctx.Query("uuid")
  297. has := false
  298. if typeCloudBrain == models.TypeCloudBrainOne {
  299. has, err = storage.Attachments.HasObject(models.AttachmentRelativePath(uuid))
  300. if err != nil {
  301. ctx.ServerError("HasObject", err)
  302. return
  303. }
  304. } else {
  305. has, err = storage.ObsHasObject(setting.BasePath + models.AttachmentRelativePath(uuid) + "/" + uuid)
  306. if err != nil {
  307. ctx.ServerError("ObsHasObject", err)
  308. return
  309. }
  310. }
  311. if !has {
  312. ctx.Error(404, "attachment has not been uploaded")
  313. return
  314. }
  315. attachment, err := models.InsertAttachment(&models.Attachment{
  316. UUID: uuid,
  317. UploaderID: ctx.User.ID,
  318. IsPrivate: true,
  319. Name: ctx.Query("file_name"),
  320. Size: ctx.QueryInt64("size"),
  321. DatasetID: ctx.QueryInt64("dataset_id"),
  322. Type: typeCloudBrain,
  323. })
  324. if err != nil {
  325. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  326. return
  327. }
  328. if attachment.DatasetID != 0 {
  329. if strings.HasSuffix(attachment.Name, ".zip") {
  330. if typeCloudBrain == models.TypeCloudBrainOne {
  331. err = worker.SendDecompressTask(contexExt.Background(), uuid)
  332. if err != nil {
  333. log.Error("SendDecompressTask(%s) failed:%s", uuid, err.Error())
  334. } else {
  335. attachment.DecompressState = models.DecompressStateIng
  336. err = models.UpdateAttachment(attachment)
  337. if err != nil {
  338. log.Error("UpdateAttachment state(%s) failed:%s", uuid, err.Error())
  339. }
  340. }
  341. }
  342. //todo:decompress type_two
  343. }
  344. }
  345. ctx.JSON(200, map[string]string{
  346. "result_code": "0",
  347. })
  348. }
  349. func UpdateAttachmentDecompressState(ctx *context.Context) {
  350. uuid := ctx.Query("uuid")
  351. result := ctx.Query("result")
  352. attach, err := models.GetAttachmentByUUID(uuid)
  353. if err != nil {
  354. log.Error("GetAttachmentByUUID(%s) failed:%s", uuid, err.Error())
  355. return
  356. }
  357. if result == DecompressSuccess {
  358. attach.DecompressState = models.DecompressStateDone
  359. } else if result == DecompressFailed {
  360. attach.DecompressState = models.DecompressStateFailed
  361. } else {
  362. log.Error("result is error:", result)
  363. return
  364. }
  365. err = models.UpdateAttachment(attach)
  366. if err != nil {
  367. log.Error("UpdateAttachment(%s) failed:%s", uuid, err.Error())
  368. return
  369. }
  370. ctx.JSON(200, map[string]string{
  371. "result_code": "0",
  372. })
  373. }
  374. func GetSuccessChunks(ctx *context.Context) {
  375. fileMD5 := ctx.Query("md5")
  376. typeCloudBrain := ctx.QueryInt("type")
  377. var chunks string
  378. err := checkTypeCloudBrain(typeCloudBrain)
  379. if err != nil {
  380. ctx.ServerError("checkTypeCloudBrain failed", err)
  381. return
  382. }
  383. fileChunk, err := models.GetFileChunkByMD5AndUser(fileMD5, ctx.User.ID, typeCloudBrain)
  384. if err != nil {
  385. if models.IsErrFileChunkNotExist(err) {
  386. ctx.JSON(200, map[string]string{
  387. "uuid": "",
  388. "uploaded": "0",
  389. "uploadID": "",
  390. "chunks": "",
  391. })
  392. } else {
  393. ctx.ServerError("GetFileChunkByMD5", err)
  394. }
  395. return
  396. }
  397. isExist := false
  398. if typeCloudBrain == models.TypeCloudBrainOne {
  399. isExist, err = storage.Attachments.HasObject(models.AttachmentRelativePath(fileChunk.UUID))
  400. if err != nil {
  401. ctx.ServerError("HasObject failed", err)
  402. return
  403. }
  404. } else {
  405. isExist, err = storage.ObsHasObject(setting.BasePath + models.AttachmentRelativePath(fileChunk.UUID) + "/" + fileChunk.UUID)
  406. if err != nil {
  407. ctx.ServerError("ObsHasObject failed", err)
  408. return
  409. }
  410. }
  411. if isExist {
  412. if fileChunk.IsUploaded == models.FileNotUploaded {
  413. log.Info("the file has been uploaded but not recorded")
  414. fileChunk.IsUploaded = models.FileUploaded
  415. if err = models.UpdateFileChunk(fileChunk); err != nil {
  416. log.Error("UpdateFileChunk failed:", err.Error())
  417. }
  418. }
  419. } else {
  420. if fileChunk.IsUploaded == models.FileUploaded {
  421. log.Info("the file has been recorded but not uploaded")
  422. fileChunk.IsUploaded = models.FileNotUploaded
  423. if err = models.UpdateFileChunk(fileChunk); err != nil {
  424. log.Error("UpdateFileChunk failed:", err.Error())
  425. }
  426. }
  427. if typeCloudBrain == models.TypeCloudBrainOne {
  428. chunks, err = storage.GetPartInfos(fileChunk.UUID, fileChunk.UploadID)
  429. if err != nil {
  430. ctx.ServerError("GetPartInfos failed", err)
  431. return
  432. }
  433. } else {
  434. chunks, err = storage.GetObsPartInfos(fileChunk.UUID, fileChunk.UploadID)
  435. if err != nil {
  436. ctx.ServerError("GetObsPartInfos failed", err)
  437. return
  438. }
  439. }
  440. }
  441. var attachID int64
  442. attach, err := models.GetAttachmentByUUID(fileChunk.UUID)
  443. if err != nil {
  444. if models.IsErrAttachmentNotExist(err) {
  445. attachID = 0
  446. } else {
  447. ctx.ServerError("GetAttachmentByUUID", err)
  448. return
  449. }
  450. } else {
  451. attachID = attach.ID
  452. }
  453. if attach == nil {
  454. ctx.JSON(200, map[string]string{
  455. "uuid": fileChunk.UUID,
  456. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  457. "uploadID": fileChunk.UploadID,
  458. "chunks": string(chunks),
  459. "attachID": "0",
  460. "datasetID": "0",
  461. "fileName": "",
  462. "datasetName": "",
  463. })
  464. return
  465. }
  466. dataset, err := models.GetDatasetByID(attach.DatasetID)
  467. if err != nil {
  468. ctx.ServerError("GetDatasetByID", err)
  469. return
  470. }
  471. ctx.JSON(200, map[string]string{
  472. "uuid": fileChunk.UUID,
  473. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  474. "uploadID": fileChunk.UploadID,
  475. "chunks": string(chunks),
  476. "attachID": strconv.Itoa(int(attachID)),
  477. "datasetID": strconv.Itoa(int(attach.DatasetID)),
  478. "fileName": attach.Name,
  479. "datasetName": dataset.Title,
  480. })
  481. }
  482. func NewMultipart(ctx *context.Context) {
  483. if !setting.Attachment.Enabled {
  484. ctx.Error(404, "attachment is not enabled")
  485. return
  486. }
  487. err := upload.VerifyFileType(ctx.Query("fileType"), strings.Split(setting.Attachment.AllowedTypes, ","))
  488. if err != nil {
  489. ctx.Error(400, err.Error())
  490. return
  491. }
  492. typeCloudBrain := ctx.QueryInt("type")
  493. err = checkTypeCloudBrain(typeCloudBrain)
  494. if err != nil {
  495. ctx.ServerError("checkTypeCloudBrain failed", err)
  496. return
  497. }
  498. fileName := ctx.Query("file_name")
  499. if setting.Attachment.StoreType == storage.MinioStorageType {
  500. totalChunkCounts := ctx.QueryInt("totalChunkCounts")
  501. if totalChunkCounts > minio_ext.MaxPartsCount {
  502. ctx.Error(400, fmt.Sprintf("chunk counts(%d) is too much", totalChunkCounts))
  503. return
  504. }
  505. fileSize := ctx.QueryInt64("size")
  506. if fileSize > minio_ext.MaxMultipartPutObjectSize {
  507. ctx.Error(400, fmt.Sprintf("file size(%d) is too big", fileSize))
  508. return
  509. }
  510. uuid := gouuid.NewV4().String()
  511. var uploadID string
  512. if typeCloudBrain == models.TypeCloudBrainOne {
  513. uploadID, err = storage.NewMultiPartUpload(uuid)
  514. if err != nil {
  515. ctx.ServerError("NewMultipart", err)
  516. return
  517. }
  518. } else {
  519. uploadID, err = storage.NewObsMultiPartUpload(uuid, fileName)
  520. if err != nil {
  521. ctx.ServerError("NewObsMultiPartUpload", err)
  522. return
  523. }
  524. }
  525. _, err = models.InsertFileChunk(&models.FileChunk{
  526. UUID: uuid,
  527. UserID: ctx.User.ID,
  528. UploadID: uploadID,
  529. Md5: ctx.Query("md5"),
  530. Size: fileSize,
  531. TotalChunks: totalChunkCounts,
  532. Type: typeCloudBrain,
  533. })
  534. if err != nil {
  535. ctx.Error(500, fmt.Sprintf("InsertFileChunk: %v", err))
  536. return
  537. }
  538. ctx.JSON(200, map[string]string{
  539. "uuid": uuid,
  540. "uploadID": uploadID,
  541. })
  542. } else {
  543. ctx.Error(404, "storage type is not enabled")
  544. return
  545. }
  546. }
  547. func GetMultipartUploadUrl(ctx *context.Context) {
  548. uuid := ctx.Query("uuid")
  549. uploadID := ctx.Query("uploadID")
  550. partNumber := ctx.QueryInt("chunkNumber")
  551. size := ctx.QueryInt64("size")
  552. fileName := ctx.Query("file_name")
  553. typeCloudBrain := ctx.QueryInt("type")
  554. err := checkTypeCloudBrain(typeCloudBrain)
  555. if err != nil {
  556. ctx.ServerError("checkTypeCloudBrain failed", err)
  557. return
  558. }
  559. url := ""
  560. if typeCloudBrain == models.TypeCloudBrainOne {
  561. if size > minio_ext.MinPartSize {
  562. ctx.Error(400, fmt.Sprintf("chunk size(%d) is too big", size))
  563. return
  564. }
  565. url, err = storage.GenMultiPartSignedUrl(uuid, uploadID, partNumber, size)
  566. if err != nil {
  567. ctx.Error(500, fmt.Sprintf("GenMultiPartSignedUrl failed: %v", err))
  568. return
  569. }
  570. } else {
  571. url, err = storage.ObsGenMultiPartSignedUrl(uuid, uploadID, partNumber, fileName)
  572. if err != nil {
  573. ctx.Error(500, fmt.Sprintf("ObsGenMultiPartSignedUrl failed: %v", err))
  574. return
  575. }
  576. }
  577. ctx.JSON(200, map[string]string{
  578. "url": url,
  579. })
  580. }
  581. func GetObsKey(ctx *context.Context) {
  582. uuid := gouuid.NewV4().String()
  583. key := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  584. ctx.JSON(200, map[string]string{
  585. "uuid": uuid,
  586. "key": key,
  587. "access_key_id": setting.AccessKeyID,
  588. "secret_access_key": setting.SecretAccessKey,
  589. "server": setting.Endpoint,
  590. "bucket": setting.Bucket,
  591. })
  592. }
  593. func CompleteMultipart(ctx *context.Context) {
  594. uuid := ctx.Query("uuid")
  595. uploadID := ctx.Query("uploadID")
  596. typeCloudBrain := ctx.QueryInt("type")
  597. fileName := ctx.Query("file_name")
  598. err := checkTypeCloudBrain(typeCloudBrain)
  599. if err != nil {
  600. ctx.ServerError("checkTypeCloudBrain failed", err)
  601. return
  602. }
  603. fileChunk, err := models.GetFileChunkByUUID(uuid)
  604. if err != nil {
  605. if models.IsErrFileChunkNotExist(err) {
  606. ctx.Error(404)
  607. } else {
  608. ctx.ServerError("GetFileChunkByUUID", err)
  609. }
  610. return
  611. }
  612. if typeCloudBrain == models.TypeCloudBrainOne {
  613. _, err = storage.CompleteMultiPartUpload(uuid, uploadID)
  614. if err != nil {
  615. ctx.Error(500, fmt.Sprintf("CompleteMultiPartUpload failed: %v", err))
  616. return
  617. }
  618. } else {
  619. err = storage.CompleteObsMultiPartUpload(uuid, uploadID, fileName)
  620. if err != nil {
  621. ctx.Error(500, fmt.Sprintf("CompleteObsMultiPartUpload failed: %v", err))
  622. return
  623. }
  624. }
  625. fileChunk.IsUploaded = models.FileUploaded
  626. err = models.UpdateFileChunk(fileChunk)
  627. if err != nil {
  628. ctx.Error(500, fmt.Sprintf("UpdateFileChunk: %v", err))
  629. return
  630. }
  631. attachment, err := models.InsertAttachment(&models.Attachment{
  632. UUID: uuid,
  633. UploaderID: ctx.User.ID,
  634. IsPrivate: true,
  635. Name: fileName,
  636. Size: ctx.QueryInt64("size"),
  637. DatasetID: ctx.QueryInt64("dataset_id"),
  638. Type: typeCloudBrain,
  639. })
  640. if err != nil {
  641. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  642. return
  643. }
  644. if attachment.DatasetID != 0 {
  645. if typeCloudBrain == models.TypeCloudBrainOne {
  646. if strings.HasSuffix(attachment.Name, ".zip") {
  647. err = worker.SendDecompressTask(contexExt.Background(), uuid)
  648. if err != nil {
  649. log.Error("SendDecompressTask(%s) failed:%s", uuid, err.Error())
  650. } else {
  651. attachment.DecompressState = models.DecompressStateIng
  652. err = models.UpdateAttachment(attachment)
  653. if err != nil {
  654. log.Error("UpdateAttachment state(%s) failed:%s", uuid, err.Error())
  655. }
  656. }
  657. }
  658. }
  659. }
  660. ctx.JSON(200, map[string]string{
  661. "result_code": "0",
  662. })
  663. }
  664. func UpdateMultipart(ctx *context.Context) {
  665. uuid := ctx.Query("uuid")
  666. partNumber := ctx.QueryInt("chunkNumber")
  667. etag := ctx.Query("etag")
  668. fileChunk, err := models.GetFileChunkByUUID(uuid)
  669. if err != nil {
  670. if models.IsErrFileChunkNotExist(err) {
  671. ctx.Error(404)
  672. } else {
  673. ctx.ServerError("GetFileChunkByUUID", err)
  674. }
  675. return
  676. }
  677. fileChunk.CompletedParts = append(fileChunk.CompletedParts, strconv.Itoa(partNumber)+"-"+strings.Replace(etag, "\"", "", -1))
  678. err = models.UpdateFileChunk(fileChunk)
  679. if err != nil {
  680. ctx.Error(500, fmt.Sprintf("UpdateFileChunk: %v", err))
  681. return
  682. }
  683. ctx.JSON(200, map[string]string{
  684. "result_code": "0",
  685. })
  686. }
  687. func HandleUnDecompressAttachment() {
  688. attachs, err := models.GetUnDecompressAttachments()
  689. if err != nil {
  690. log.Error("GetUnDecompressAttachments failed:", err.Error())
  691. return
  692. }
  693. for _, attach := range attachs {
  694. err = worker.SendDecompressTask(contexExt.Background(), attach.UUID)
  695. if err != nil {
  696. log.Error("SendDecompressTask(%s) failed:%s", attach.UUID, err.Error())
  697. } else {
  698. attach.DecompressState = models.DecompressStateIng
  699. err = models.UpdateAttachment(attach)
  700. if err != nil {
  701. log.Error("UpdateAttachment state(%s) failed:%s", attach.UUID, err.Error())
  702. }
  703. }
  704. }
  705. return
  706. }
  707. func QueryAllPublicDataset(ctx *context.Context) {
  708. attachs, err := models.GetAllPublicAttachments()
  709. if err != nil {
  710. ctx.JSON(200, map[string]string{
  711. "result_code": "-1",
  712. "error_msg": err.Error(),
  713. "data": "",
  714. })
  715. return
  716. }
  717. queryDatasets(ctx, attachs)
  718. }
  719. func QueryPrivateDataset(ctx *context.Context) {
  720. username := ctx.Params(":username")
  721. attachs, err := models.GetPrivateAttachments(username)
  722. if err != nil {
  723. ctx.JSON(200, map[string]string{
  724. "result_code": "-1",
  725. "error_msg": err.Error(),
  726. "data": "",
  727. })
  728. return
  729. }
  730. for _, attach := range attachs {
  731. attach.Name = username
  732. }
  733. queryDatasets(ctx, attachs)
  734. }
  735. func queryDatasets(ctx *context.Context, attachs []*models.AttachmentUsername) {
  736. var datasets []CloudBrainDataset
  737. if len(attachs) == 0 {
  738. log.Info("dataset is null")
  739. ctx.JSON(200, map[string]string{
  740. "result_code": "0",
  741. "error_msg": "",
  742. "data": "",
  743. })
  744. return
  745. }
  746. for _, attch := range attachs {
  747. has, err := storage.Attachments.HasObject(models.AttachmentRelativePath(attch.UUID))
  748. if err != nil || !has {
  749. continue
  750. }
  751. datasets = append(datasets, CloudBrainDataset{strconv.FormatInt(attch.ID, 10),
  752. attch.Attachment.Name,
  753. setting.Attachment.Minio.RealPath +
  754. setting.Attachment.Minio.Bucket + "/" +
  755. setting.Attachment.Minio.BasePath +
  756. models.AttachmentRelativePath(attch.UUID) +
  757. attch.UUID,
  758. attch.Name,
  759. attch.CreatedUnix.Format("2006-01-02 03:04:05 PM")})
  760. }
  761. data, err := json.Marshal(datasets)
  762. if err != nil {
  763. log.Error("json.Marshal failed:", err.Error())
  764. ctx.JSON(200, map[string]string{
  765. "result_code": "-1",
  766. "error_msg": err.Error(),
  767. "data": "",
  768. })
  769. return
  770. }
  771. ctx.JSON(200, map[string]string{
  772. "result_code": "0",
  773. "error_msg": "",
  774. "data": string(data),
  775. })
  776. return
  777. }
  778. func checkTypeCloudBrain(typeCloudBrain int) error {
  779. if typeCloudBrain != models.TypeCloudBrainOne && typeCloudBrain != models.TypeCloudBrainTwo {
  780. log.Error("type error:", typeCloudBrain)
  781. return errors.New("type error")
  782. }
  783. return nil
  784. }