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 34 kB

4 years ago
4 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
5 years ago
4 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 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
4 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
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
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 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
5 years ago
5 years ago
5 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
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
4 years ago
5 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 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
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
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
5 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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/modules/auth"
  16. "code.gitea.io/gitea/modules/base"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/modules/context"
  19. "code.gitea.io/gitea/modules/labelmsg"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/minio_ext"
  22. "code.gitea.io/gitea/modules/notification"
  23. "code.gitea.io/gitea/modules/setting"
  24. "code.gitea.io/gitea/modules/storage"
  25. "code.gitea.io/gitea/modules/upload"
  26. "code.gitea.io/gitea/modules/worker"
  27. gouuid "github.com/satori/go.uuid"
  28. )
  29. const (
  30. //result of decompress
  31. DecompressSuccess = "0"
  32. DecompressFailed = "1"
  33. tplAttachmentUpload base.TplName = "repo/attachment/upload"
  34. tplAttachmentEdit base.TplName = "repo/attachment/edit"
  35. )
  36. type CloudBrainDataset struct {
  37. UUID string `json:"id"`
  38. Name string `json:"name"`
  39. Path string `json:"place"`
  40. UserName string `json:"provider"`
  41. CreateTime string `json:"created_at"`
  42. }
  43. type UploadForm struct {
  44. UploadID string `form:"uploadId"`
  45. UuID string `form:"uuid"`
  46. PartSize int64 `form:"size"`
  47. Offset int64 `form:"offset"`
  48. PartNumber int `form:"chunkNumber"`
  49. PartFile multipart.File `form:"file"`
  50. }
  51. func RenderAttachmentSettings(ctx *context.Context) {
  52. renderAttachmentSettings(ctx)
  53. }
  54. func renderAttachmentSettings(ctx *context.Context) {
  55. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  56. ctx.Data["AttachmentStoreType"] = setting.Attachment.StoreType
  57. ctx.Data["AttachmentAllowedTypes"] = setting.Attachment.AllowedTypes
  58. ctx.Data["AttachmentMaxSize"] = setting.Attachment.MaxSize
  59. ctx.Data["AttachmentMaxFiles"] = setting.Attachment.MaxFiles
  60. }
  61. func UploadAttachmentUI(ctx *context.Context) {
  62. ctx.Data["datasetId"] = ctx.Query("datasetId")
  63. ctx.Data["PageIsDataset"] = true
  64. ctx.HTML(200, tplAttachmentUpload)
  65. }
  66. func EditAttachmentUI(ctx *context.Context) {
  67. id, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  68. ctx.Data["PageIsDataset"] = true
  69. attachment, _ := models.GetAttachmentByID(id)
  70. if attachment == nil {
  71. ctx.Error(404, "The attachment does not exits.")
  72. }
  73. ctx.Data["Attachment"] = attachment
  74. ctx.HTML(200, tplAttachmentEdit)
  75. }
  76. func EditAttachment(ctx *context.Context, form auth.EditAttachmentForm) {
  77. err := models.UpdateAttachmentDescription(&models.Attachment{
  78. ID: form.ID,
  79. Description: form.Description,
  80. })
  81. if err != nil {
  82. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.edit_attachment_fail")))
  83. }
  84. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  85. }
  86. // UploadAttachment response for uploading issue's attachment
  87. func UploadAttachment(ctx *context.Context) {
  88. if !setting.Attachment.Enabled {
  89. ctx.Error(404, "attachment is not enabled")
  90. return
  91. }
  92. file, header, err := ctx.Req.FormFile("file")
  93. if err != nil {
  94. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  95. return
  96. }
  97. defer file.Close()
  98. buf := make([]byte, 1024)
  99. n, _ := file.Read(buf)
  100. if n > 0 {
  101. buf = buf[:n]
  102. }
  103. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.Attachment.AllowedTypes, ","))
  104. if err != nil {
  105. ctx.Error(400, err.Error())
  106. return
  107. }
  108. datasetID, _ := strconv.ParseInt(ctx.Req.FormValue("dataset_id"), 10, 64)
  109. attach, err := models.NewAttachment(&models.Attachment{
  110. IsPrivate: true,
  111. UploaderID: ctx.User.ID,
  112. Name: header.Filename,
  113. DatasetID: datasetID,
  114. }, buf, file)
  115. if err != nil {
  116. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  117. return
  118. }
  119. log.Trace("New attachment uploaded: %s", attach.UUID)
  120. ctx.JSON(200, map[string]string{
  121. "uuid": attach.UUID,
  122. })
  123. }
  124. func UpdatePublicAttachment(ctx *context.Context) {
  125. file := ctx.Query("file")
  126. isPrivate, _ := strconv.ParseBool(ctx.Query("is_private"))
  127. attach, err := models.GetAttachmentByUUID(file)
  128. if err != nil {
  129. ctx.Error(404, err.Error())
  130. return
  131. }
  132. attach.IsPrivate = isPrivate
  133. models.UpdateAttachment(attach)
  134. }
  135. // DeleteAttachment response for deleting issue's attachment
  136. func DeleteAttachment(ctx *context.Context) {
  137. file := ctx.Query("file")
  138. attach, err := models.GetAttachmentByUUID(file)
  139. if err != nil {
  140. ctx.Error(400, err.Error())
  141. return
  142. }
  143. //issue 214: mod del-dataset permission
  144. if !models.CanDelAttachment(ctx.IsSigned, ctx.User, attach) {
  145. ctx.Error(403)
  146. return
  147. }
  148. err = models.DeleteAttachment(attach, true)
  149. if err != nil {
  150. ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
  151. return
  152. }
  153. attachjson, _ := json.Marshal(attach)
  154. labelmsg.SendDeleteAttachToLabelSys(string(attachjson))
  155. DeleteAllUnzipFile(attach, "")
  156. _, err = models.DeleteFileChunkById(attach.UUID)
  157. if err != nil {
  158. ctx.Error(500, fmt.Sprintf("DeleteFileChunkById: %v", err))
  159. return
  160. }
  161. ctx.JSON(200, map[string]string{
  162. "uuid": attach.UUID,
  163. })
  164. }
  165. func DownloadUserIsOrgOrCollaboration(ctx *context.Context, attach *models.Attachment) bool {
  166. dataset, err := models.GetDatasetByID(attach.DatasetID)
  167. if err != nil {
  168. log.Info("query dataset error")
  169. } else {
  170. repo, err := models.GetRepositoryByID(dataset.RepoID)
  171. if err != nil {
  172. log.Info("query repo error.")
  173. } else {
  174. repo.GetOwner()
  175. if ctx.User != nil {
  176. if repo.Owner.IsOrganization() {
  177. if repo.Owner.IsUserPartOfOrg(ctx.User.ID) {
  178. log.Info("org user may visit the attach.")
  179. return true
  180. }
  181. }
  182. isCollaborator, _ := repo.IsCollaborator(ctx.User.ID)
  183. if isCollaborator {
  184. log.Info("Collaborator user may visit the attach.")
  185. return true
  186. }
  187. }
  188. }
  189. }
  190. return false
  191. }
  192. // GetAttachment serve attachements
  193. func GetAttachment(ctx *context.Context) {
  194. typeCloudBrain := ctx.QueryInt("type")
  195. err := checkTypeCloudBrain(typeCloudBrain)
  196. if err != nil {
  197. ctx.ServerError("checkTypeCloudBrain failed", err)
  198. return
  199. }
  200. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  201. if err != nil {
  202. if models.IsErrAttachmentNotExist(err) {
  203. ctx.Error(404)
  204. } else {
  205. ctx.ServerError("GetAttachmentByUUID", err)
  206. }
  207. return
  208. }
  209. repository, unitType, err := attach.LinkedRepository()
  210. if err != nil {
  211. ctx.ServerError("LinkedRepository", err)
  212. return
  213. }
  214. dataSet, err := attach.LinkedDataSet()
  215. if err != nil {
  216. ctx.ServerError("LinkedDataSet", err)
  217. return
  218. }
  219. if repository == nil && dataSet != nil {
  220. repository, _ = models.GetRepositoryByID(dataSet.RepoID)
  221. unitType = models.UnitTypeDatasets
  222. }
  223. if repository == nil { //If not linked
  224. //if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) && attach.IsPrivate { //We block if not the uploader
  225. //log.Info("ctx.IsSigned =" + fmt.Sprintf("%v", ctx.IsSigned))
  226. if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) && attach.IsPrivate && !DownloadUserIsOrgOrCollaboration(ctx, attach) { //We block if not the uploader
  227. ctx.Error(http.StatusNotFound)
  228. return
  229. }
  230. } else { //If we have the repository we check access
  231. perm, errPermission := models.GetUserRepoPermission(repository, ctx.User)
  232. if errPermission != nil {
  233. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", errPermission.Error())
  234. return
  235. }
  236. if !perm.CanRead(unitType) {
  237. ctx.Error(http.StatusNotFound)
  238. return
  239. }
  240. }
  241. if dataSet != nil {
  242. if !ctx.IsSigned {
  243. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  244. ctx.Redirect(setting.AppSubURL + "/user/login")
  245. return
  246. } else {
  247. isPermit, err := models.GetUserDataSetPermission(dataSet, ctx.User)
  248. if err != nil {
  249. ctx.Error(http.StatusInternalServerError, "GetUserDataSetPermission", err.Error())
  250. return
  251. }
  252. if !isPermit {
  253. ctx.Error(http.StatusNotFound)
  254. return
  255. }
  256. }
  257. }
  258. //If we have matched and access to release or issue
  259. if setting.Attachment.StoreType == storage.MinioStorageType {
  260. url := ""
  261. if typeCloudBrain == models.TypeCloudBrainOne {
  262. url, err = storage.Attachments.PresignedGetURL(setting.Attachment.Minio.BasePath+attach.RelativePath(), attach.Name)
  263. if err != nil {
  264. ctx.ServerError("PresignedGetURL", err)
  265. return
  266. }
  267. } else {
  268. if setting.PROXYURL != "" {
  269. url = setting.PROXYURL + "/obs_proxy_download?uuid=" + attach.UUID + "&file_name=" + attach.Name
  270. log.Info("return url=" + url)
  271. } else {
  272. objectName := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(attach.UUID[0:1], attach.UUID[1:2], attach.UUID, attach.Name)), "/")
  273. url, err = storage.ObsGetPreSignedUrl(objectName, attach.Name)
  274. if err != nil {
  275. ctx.ServerError("ObsGetPreSignedUrl", err)
  276. return
  277. }
  278. }
  279. }
  280. if err = increaseDownloadCount(attach, dataSet); err != nil {
  281. ctx.ServerError("Update", err)
  282. return
  283. }
  284. if dataSet != nil {
  285. http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
  286. } else {
  287. fr, err := storage.Attachments.Open(attach.RelativePath())
  288. if err != nil {
  289. ctx.ServerError("Open", err)
  290. return
  291. }
  292. defer fr.Close()
  293. if err = ServeData(ctx, attach.Name, fr); err != nil {
  294. ctx.ServerError("ServeData", err)
  295. return
  296. }
  297. }
  298. } else {
  299. fr, err := storage.Attachments.Open(attach.RelativePath())
  300. if err != nil {
  301. ctx.ServerError("Open", err)
  302. return
  303. }
  304. defer fr.Close()
  305. if err = increaseDownloadCount(attach, dataSet); err != nil {
  306. ctx.ServerError("Update", err)
  307. return
  308. }
  309. if err = ServeData(ctx, attach.Name, fr); err != nil {
  310. ctx.ServerError("ServeData", err)
  311. return
  312. }
  313. }
  314. }
  315. func increaseDownloadCount(attach *models.Attachment, dataSet *models.Dataset) error {
  316. if err := attach.IncreaseDownloadCount(); err != nil {
  317. return err
  318. }
  319. if dataSet != nil {
  320. if err := models.IncreaseDownloadCount(dataSet.ID); err != nil {
  321. return err
  322. }
  323. }
  324. return nil
  325. }
  326. // Get a presigned url for put object
  327. func GetPresignedPutObjectURL(ctx *context.Context) {
  328. if !setting.Attachment.Enabled {
  329. ctx.Error(404, "attachment is not enabled")
  330. return
  331. }
  332. err := upload.VerifyFileType(ctx.Params("file_type"), strings.Split(setting.Attachment.AllowedTypes, ","))
  333. if err != nil {
  334. ctx.Error(400, err.Error())
  335. return
  336. }
  337. if setting.Attachment.StoreType == storage.MinioStorageType {
  338. uuid := gouuid.NewV4().String()
  339. url, err := storage.Attachments.PresignedPutURL(models.AttachmentRelativePath(uuid))
  340. if err != nil {
  341. ctx.ServerError("PresignedPutURL", err)
  342. return
  343. }
  344. ctx.JSON(200, map[string]string{
  345. "uuid": uuid,
  346. "url": url,
  347. })
  348. } else {
  349. ctx.Error(404, "storage type is not enabled")
  350. return
  351. }
  352. }
  353. // AddAttachment response for add attachment record
  354. func AddAttachment(ctx *context.Context) {
  355. typeCloudBrain := ctx.QueryInt("type")
  356. fileName := ctx.Query("file_name")
  357. err := checkTypeCloudBrain(typeCloudBrain)
  358. if err != nil {
  359. ctx.ServerError("checkTypeCloudBrain failed", err)
  360. return
  361. }
  362. uuid := ctx.Query("uuid")
  363. has := false
  364. if typeCloudBrain == models.TypeCloudBrainOne {
  365. has, err = storage.Attachments.HasObject(setting.Attachment.Minio.BasePath + models.AttachmentRelativePath(uuid))
  366. if err != nil {
  367. ctx.ServerError("HasObject", err)
  368. return
  369. }
  370. } else {
  371. has, err = storage.ObsHasObject(setting.BasePath + models.AttachmentRelativePath(uuid) + "/" + fileName)
  372. if err != nil {
  373. ctx.ServerError("ObsHasObject", err)
  374. return
  375. }
  376. }
  377. if !has {
  378. ctx.Error(404, "attachment has not been uploaded")
  379. return
  380. }
  381. datasetId := ctx.QueryInt64("dataset_id")
  382. dataset, err := models.GetDatasetByID(datasetId)
  383. if err != nil {
  384. ctx.Error(404, "dataset does not exist.")
  385. return
  386. }
  387. attachment, err := models.InsertAttachment(&models.Attachment{
  388. UUID: uuid,
  389. UploaderID: ctx.User.ID,
  390. IsPrivate: dataset.IsPrivate(),
  391. Name: fileName,
  392. Size: ctx.QueryInt64("size"),
  393. DatasetID: ctx.QueryInt64("dataset_id"),
  394. Type: typeCloudBrain,
  395. })
  396. if err != nil {
  397. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  398. return
  399. }
  400. if attachment.DatasetID != 0 {
  401. if isCanDecompress(attachment.Name) {
  402. if typeCloudBrain == models.TypeCloudBrainOne {
  403. err = worker.SendDecompressTask(contexExt.Background(), uuid, attachment.Name)
  404. if err != nil {
  405. log.Error("SendDecompressTask(%s) failed:%s", uuid, err.Error())
  406. } else {
  407. attachment.DecompressState = models.DecompressStateIng
  408. err = models.UpdateAttachment(attachment)
  409. if err != nil {
  410. log.Error("UpdateAttachment state(%s) failed:%s", uuid, err.Error())
  411. }
  412. }
  413. }
  414. //todo:decompress type_two
  415. }
  416. }
  417. ctx.JSON(200, map[string]string{
  418. "result_code": "0",
  419. })
  420. }
  421. func isCanDecompress(name string) bool {
  422. if strings.HasSuffix(name, ".zip") || strings.HasSuffix(name, ".tar.gz") || strings.HasSuffix(name, ".tgz") {
  423. return true
  424. }
  425. return false
  426. }
  427. func UpdateAttachmentDecompressState(ctx *context.Context) {
  428. uuid := ctx.Query("uuid")
  429. result := ctx.Query("result")
  430. attach, err := models.GetAttachmentByUUID(uuid)
  431. if err != nil {
  432. log.Error("GetAttachmentByUUID(%s) failed:%s", uuid, err.Error())
  433. return
  434. }
  435. if result == DecompressSuccess {
  436. attach.DecompressState = models.DecompressStateDone
  437. } else if result == DecompressFailed {
  438. attach.DecompressState = models.DecompressStateFailed
  439. } else {
  440. log.Error("result is error:", result)
  441. return
  442. }
  443. err = models.UpdateAttachment(attach)
  444. if err != nil {
  445. log.Error("UpdateAttachment(%s) failed:%s", uuid, err.Error())
  446. return
  447. }
  448. log.Info("start to send msg to labelsystem ")
  449. dataset, _ := models.GetDatasetByID(attach.DatasetID)
  450. var labelMap map[string]string
  451. labelMap = make(map[string]string)
  452. labelMap["UUID"] = uuid
  453. labelMap["Type"] = fmt.Sprint(attach.Type)
  454. labelMap["UploaderID"] = fmt.Sprint(attach.UploaderID)
  455. labelMap["RepoID"] = fmt.Sprint(dataset.RepoID)
  456. labelMap["AttachName"] = attach.Name
  457. attachjson, _ := json.Marshal(labelMap)
  458. labelmsg.SendAddAttachToLabelSys(string(attachjson))
  459. log.Info("end to send msg to labelsystem ")
  460. ctx.JSON(200, map[string]string{
  461. "result_code": "0",
  462. })
  463. }
  464. func getCloudOneMinioPrefix(scene string, fileChunk *models.FileChunk) string {
  465. if scene == Attachment_model {
  466. return fileChunk.ObjectName
  467. } else {
  468. return setting.Attachment.Minio.BasePath + models.AttachmentRelativePath(fileChunk.UUID)
  469. }
  470. }
  471. func getCloudTwoOBSPrefix(scene string, fileChunk *models.FileChunk, fileName string) string {
  472. if scene == Attachment_model {
  473. return fileChunk.ObjectName
  474. } else {
  475. return setting.BasePath + models.AttachmentRelativePath(fileChunk.UUID) + "/" + fileName
  476. }
  477. }
  478. func GetSuccessChunks(ctx *context.Context) {
  479. fileMD5 := ctx.Query("md5")
  480. typeCloudBrain := ctx.QueryInt("type")
  481. fileName := ctx.Query("file_name")
  482. scene := ctx.Query("scene")
  483. modeluuid := ctx.Query("modeluuid")
  484. log.Info("scene=" + scene)
  485. var chunks string
  486. err := checkTypeCloudBrain(typeCloudBrain)
  487. if err != nil {
  488. ctx.ServerError("checkTypeCloudBrain failed", err)
  489. return
  490. }
  491. fileChunk, err := models.GetFileChunkByMD5AndUser(fileMD5, ctx.User.ID, typeCloudBrain)
  492. if err != nil {
  493. if models.IsErrFileChunkNotExist(err) {
  494. ctx.JSON(200, map[string]string{
  495. "uuid": "",
  496. "uploaded": "0",
  497. "uploadID": "",
  498. "chunks": "",
  499. })
  500. } else {
  501. ctx.ServerError("GetFileChunkByMD5", err)
  502. }
  503. return
  504. }
  505. isExist := false
  506. if typeCloudBrain == models.TypeCloudBrainOne {
  507. isExist, err = storage.Attachments.HasObject(getCloudOneMinioPrefix(scene, fileChunk))
  508. if err != nil {
  509. ctx.ServerError("HasObject failed", err)
  510. return
  511. }
  512. } else {
  513. oldFileName := fileName
  514. oldAttachment, _ := models.GetAttachmentByUUID(fileChunk.UUID)
  515. if oldAttachment != nil {
  516. oldFileName = oldAttachment.Name
  517. }
  518. isExist, err = storage.ObsHasObject(getCloudTwoOBSPrefix(scene, fileChunk, oldFileName))
  519. if err != nil {
  520. ctx.ServerError("ObsHasObject failed", err)
  521. return
  522. }
  523. }
  524. if isExist {
  525. if fileChunk.IsUploaded == models.FileNotUploaded {
  526. log.Info("the file has been uploaded but not recorded")
  527. fileChunk.IsUploaded = models.FileUploaded
  528. if err = models.UpdateFileChunk(fileChunk); err != nil {
  529. log.Error("UpdateFileChunk failed:", err.Error())
  530. }
  531. }
  532. } else {
  533. if fileChunk.IsUploaded == models.FileUploaded {
  534. log.Info("the file has been recorded but not uploaded")
  535. fileChunk.IsUploaded = models.FileNotUploaded
  536. if err = models.UpdateFileChunk(fileChunk); err != nil {
  537. log.Error("UpdateFileChunk failed:", err.Error())
  538. }
  539. }
  540. if typeCloudBrain == models.TypeCloudBrainOne {
  541. chunks, err = storage.GetPartInfos(getChunkMinioExistObjectName(scene, fileChunk, fileName), fileChunk.UploadID)
  542. if err != nil {
  543. log.Error("GetPartInfos failed:%v", err.Error())
  544. }
  545. } else {
  546. chunks, err = storage.GetObsPartInfos(getChunkOBSExistObjectName(scene, fileChunk, fileName), fileChunk.UploadID)
  547. if err != nil {
  548. log.Error("GetObsPartInfos failed:%v", err.Error())
  549. }
  550. }
  551. if err != nil {
  552. models.DeleteFileChunk(fileChunk)
  553. ctx.JSON(200, map[string]string{
  554. "uuid": "",
  555. "uploaded": "0",
  556. "uploadID": "",
  557. "chunks": "",
  558. })
  559. return
  560. }
  561. }
  562. var attachID int64
  563. attach, err := models.GetAttachmentByUUID(fileChunk.UUID)
  564. if err != nil {
  565. if models.IsErrAttachmentNotExist(err) {
  566. attachID = 0
  567. } else {
  568. ctx.ServerError("GetAttachmentByUUID", err)
  569. return
  570. }
  571. } else {
  572. attachID = attach.ID
  573. }
  574. if attach == nil {
  575. ctx.JSON(200, map[string]string{
  576. "uuid": fileChunk.UUID,
  577. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  578. "uploadID": fileChunk.UploadID,
  579. "chunks": string(chunks),
  580. "attachID": "0",
  581. "datasetID": "0",
  582. "fileName": "",
  583. "datasetName": "",
  584. })
  585. return
  586. }
  587. if scene == Attachment_model {
  588. //使用description存储模型信息
  589. dbmodeluuid := attach.Description
  590. modelname := ""
  591. if dbmodeluuid != modeluuid {
  592. model, err := models.QueryModelById(dbmodeluuid)
  593. if err == nil {
  594. modelname = model.Name
  595. }
  596. //copy
  597. srcObjectName := fileChunk.ObjectName
  598. destObjectName := getObjectName(fileName, modeluuid)
  599. if typeCloudBrain == models.TypeCloudBrainOne {
  600. bucketName := setting.Attachment.Minio.Bucket
  601. storage.MinioCopyAFile(bucketName, srcObjectName, bucketName, destObjectName)
  602. } else {
  603. bucketName := setting.Bucket
  604. storage.ObsCopyFile(bucketName, srcObjectName, bucketName, destObjectName)
  605. }
  606. UpdateModelSize(modeluuid)
  607. ctx.JSON(200, map[string]string{
  608. "uuid": fileChunk.UUID,
  609. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  610. "uploadID": fileChunk.UploadID,
  611. "chunks": string(chunks),
  612. "attachID": strconv.Itoa(int(attachID)),
  613. "fileName": attach.Name,
  614. })
  615. return
  616. } else {
  617. model, err := models.QueryModelById(dbmodeluuid)
  618. if err == nil {
  619. modelname = model.Name
  620. }
  621. ctx.JSON(200, map[string]string{
  622. "uuid": fileChunk.UUID,
  623. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  624. "uploadID": fileChunk.UploadID,
  625. "chunks": string(chunks),
  626. "attachID": strconv.Itoa(int(attachID)),
  627. "modeluuid": dbmodeluuid,
  628. "fileName": attach.Name,
  629. "modelName": modelname,
  630. })
  631. return
  632. }
  633. } else {
  634. dataset, err := models.GetDatasetByID(attach.DatasetID)
  635. if err != nil {
  636. ctx.ServerError("GetDatasetByID", err)
  637. return
  638. }
  639. ctx.JSON(200, map[string]string{
  640. "uuid": fileChunk.UUID,
  641. "uploaded": strconv.Itoa(fileChunk.IsUploaded),
  642. "uploadID": fileChunk.UploadID,
  643. "chunks": string(chunks),
  644. "attachID": strconv.Itoa(int(attachID)),
  645. "datasetID": strconv.Itoa(int(attach.DatasetID)),
  646. "fileName": attach.Name,
  647. "datasetName": dataset.Title,
  648. })
  649. }
  650. }
  651. func getObjectName(filename string, modeluuid string) string {
  652. return strings.TrimPrefix(path.Join(Model_prefix, path.Join(modeluuid[0:1], modeluuid[1:2], modeluuid, filename)), "/")
  653. }
  654. func getMinioInitObjectName(scene string, uuid, modeluuid string, filename string) string {
  655. if scene == Attachment_model {
  656. return strings.TrimPrefix(path.Join(Model_prefix, path.Join(modeluuid[0:1], modeluuid[1:2], modeluuid, filename)), "/")
  657. } else {
  658. return strings.TrimPrefix(path.Join(setting.Attachment.Minio.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  659. }
  660. }
  661. func getChunkMinioExistObjectName(scene string, fileChunk *models.FileChunk, filename string) string {
  662. if scene == Attachment_model {
  663. return fileChunk.ObjectName
  664. } else {
  665. return strings.TrimPrefix(path.Join(setting.Attachment.Minio.BasePath, path.Join(fileChunk.UUID[0:1], fileChunk.UUID[1:2], fileChunk.UUID)), "/")
  666. }
  667. }
  668. func getOBSInitObjectName(scene string, uuid, modeluuid string, filename string) string {
  669. if scene == Attachment_model {
  670. return strings.TrimPrefix(path.Join(Model_prefix, path.Join(modeluuid[0:1], modeluuid[1:2], modeluuid, filename)), "/")
  671. } else {
  672. return strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, filename)), "/")
  673. }
  674. }
  675. func getChunkOBSExistObjectName(scene string, fileChunk *models.FileChunk, filename string) string {
  676. if scene == Attachment_model {
  677. return fileChunk.ObjectName
  678. } else {
  679. return strings.TrimPrefix(path.Join(setting.BasePath, path.Join(fileChunk.UUID[0:1], fileChunk.UUID[1:2], fileChunk.UUID, filename)), "/")
  680. }
  681. }
  682. func NewMultipart(ctx *context.Context) {
  683. if !setting.Attachment.Enabled {
  684. ctx.Error(404, "attachment is not enabled")
  685. return
  686. }
  687. err := upload.VerifyFileType(ctx.Query("fileType"), strings.Split(setting.Attachment.AllowedTypes, ","))
  688. if err != nil {
  689. ctx.Error(400, err.Error())
  690. return
  691. }
  692. typeCloudBrain := ctx.QueryInt("type")
  693. err = checkTypeCloudBrain(typeCloudBrain)
  694. if err != nil {
  695. ctx.ServerError("checkTypeCloudBrain failed", err)
  696. return
  697. }
  698. fileName := ctx.Query("file_name")
  699. scene := ctx.Query("scene")
  700. modeluuid := ctx.Query("modeluuid")
  701. if setting.Attachment.StoreType == storage.MinioStorageType {
  702. totalChunkCounts := ctx.QueryInt("totalChunkCounts")
  703. if totalChunkCounts > minio_ext.MaxPartsCount {
  704. ctx.Error(400, fmt.Sprintf("chunk counts(%d) is too much", totalChunkCounts))
  705. return
  706. }
  707. fileSize := ctx.QueryInt64("size")
  708. if fileSize > minio_ext.MaxMultipartPutObjectSize {
  709. ctx.Error(400, fmt.Sprintf("file size(%d) is too big", fileSize))
  710. return
  711. }
  712. uuid := gouuid.NewV4().String()
  713. var uploadID string
  714. var objectName string
  715. if typeCloudBrain == models.TypeCloudBrainOne {
  716. objectName = getMinioInitObjectName(scene, uuid, modeluuid, fileName)
  717. uploadID, err = storage.NewMultiPartUpload(objectName)
  718. if err != nil {
  719. ctx.ServerError("NewMultipart", err)
  720. return
  721. }
  722. } else {
  723. objectName = getOBSInitObjectName(scene, uuid, modeluuid, fileName)
  724. uploadID, err = storage.NewObsMultiPartUpload(objectName)
  725. if err != nil {
  726. ctx.ServerError("NewObsMultiPartUpload", err)
  727. return
  728. }
  729. }
  730. _, err = models.InsertFileChunk(&models.FileChunk{
  731. UUID: uuid,
  732. UserID: ctx.User.ID,
  733. UploadID: uploadID,
  734. ObjectName: objectName,
  735. Md5: ctx.Query("md5"),
  736. Size: fileSize,
  737. TotalChunks: totalChunkCounts,
  738. Type: typeCloudBrain,
  739. })
  740. if err != nil {
  741. ctx.Error(500, fmt.Sprintf("InsertFileChunk: %v", err))
  742. return
  743. }
  744. ctx.JSON(200, map[string]string{
  745. "uuid": uuid,
  746. "uploadID": uploadID,
  747. })
  748. } else {
  749. ctx.Error(404, "storage type is not enabled")
  750. return
  751. }
  752. }
  753. func PutOBSProxyUpload(ctx *context.Context) {
  754. uuid := ctx.Query("uuid")
  755. uploadID := ctx.Query("uploadId")
  756. partNumber := ctx.QueryInt("partNumber")
  757. fileName := ctx.Query("file_name")
  758. RequestBody := ctx.Req.Body()
  759. if RequestBody == nil {
  760. ctx.Error(500, fmt.Sprintf("FormFile: %v", RequestBody))
  761. return
  762. }
  763. objectName := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  764. err := storage.ObsMultiPartUpload(objectName, uploadID, partNumber, fileName, RequestBody.ReadCloser())
  765. if err != nil {
  766. log.Info("upload error.")
  767. }
  768. }
  769. func GetOBSProxyDownload(ctx *context.Context) {
  770. uuid := ctx.Query("uuid")
  771. fileName := ctx.Query("file_name")
  772. objectName := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  773. body, err := storage.ObsDownloadAFile(setting.Bucket, objectName)
  774. if err != nil {
  775. log.Info("upload error.")
  776. } else {
  777. defer body.Close()
  778. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+fileName)
  779. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  780. p := make([]byte, 1024)
  781. var readErr error
  782. var readCount int
  783. // 读取对象内容
  784. for {
  785. readCount, readErr = body.Read(p)
  786. if readCount > 0 {
  787. ctx.Resp.Write(p[:readCount])
  788. //fmt.Printf("%s", p[:readCount])
  789. }
  790. if readErr != nil {
  791. break
  792. }
  793. }
  794. }
  795. }
  796. func GetMultipartUploadUrl(ctx *context.Context) {
  797. uuid := ctx.Query("uuid")
  798. uploadID := ctx.Query("uploadID")
  799. partNumber := ctx.QueryInt("chunkNumber")
  800. size := ctx.QueryInt64("size")
  801. fileName := ctx.Query("file_name")
  802. scene := ctx.Query("scene")
  803. typeCloudBrain := ctx.QueryInt("type")
  804. err := checkTypeCloudBrain(typeCloudBrain)
  805. if err != nil {
  806. ctx.ServerError("checkTypeCloudBrain failed", err)
  807. return
  808. }
  809. fileChunk, err := models.GetFileChunkByUUID(uuid)
  810. if err != nil {
  811. if models.IsErrFileChunkNotExist(err) {
  812. ctx.Error(404)
  813. } else {
  814. ctx.ServerError("GetFileChunkByUUID", err)
  815. }
  816. return
  817. }
  818. url := ""
  819. if typeCloudBrain == models.TypeCloudBrainOne {
  820. if size > minio_ext.MinPartSize {
  821. ctx.Error(400, fmt.Sprintf("chunk size(%d) is too big", size))
  822. return
  823. }
  824. url, err = storage.GenMultiPartSignedUrl(getChunkMinioExistObjectName(scene, fileChunk, fileName), uploadID, partNumber, size)
  825. if err != nil {
  826. ctx.Error(500, fmt.Sprintf("GenMultiPartSignedUrl failed: %v", err))
  827. return
  828. }
  829. } else {
  830. if setting.PROXYURL != "" {
  831. url = setting.PROXYURL + "/obs_proxy_multipart?uuid=" + uuid + "&uploadId=" + uploadID + "&partNumber=" + fmt.Sprint(partNumber) + "&file_name=" + fileName
  832. log.Info("return url=" + url)
  833. } else {
  834. url, err = storage.ObsGenMultiPartSignedUrl(getChunkOBSExistObjectName(scene, fileChunk, fileName), uploadID, partNumber)
  835. if err != nil {
  836. ctx.Error(500, fmt.Sprintf("ObsGenMultiPartSignedUrl failed: %v", err))
  837. return
  838. }
  839. log.Info("url=" + url)
  840. }
  841. }
  842. ctx.JSON(200, map[string]string{
  843. "url": url,
  844. })
  845. }
  846. func CompleteMultipart(ctx *context.Context) {
  847. uuid := ctx.Query("uuid")
  848. uploadID := ctx.Query("uploadID")
  849. typeCloudBrain := ctx.QueryInt("type")
  850. fileName := ctx.Query("file_name")
  851. scene := ctx.Query("scene")
  852. modeluuid := ctx.Query("modeluuid")
  853. log.Warn("uuid:" + uuid)
  854. log.Warn("modeluuid:" + modeluuid)
  855. log.Warn("scene:" + scene)
  856. log.Warn("typeCloudBrain:" + strconv.Itoa(typeCloudBrain))
  857. err := checkTypeCloudBrain(typeCloudBrain)
  858. if err != nil {
  859. ctx.ServerError("checkTypeCloudBrain failed", err)
  860. return
  861. }
  862. fileChunk, err := models.GetFileChunkByUUID(uuid)
  863. if err != nil {
  864. if models.IsErrFileChunkNotExist(err) {
  865. ctx.Error(404)
  866. } else {
  867. ctx.ServerError("GetFileChunkByUUID", err)
  868. }
  869. return
  870. }
  871. if typeCloudBrain == models.TypeCloudBrainOne {
  872. _, err = storage.CompleteMultiPartUpload(getChunkMinioExistObjectName(scene, fileChunk, fileName), uploadID, fileChunk.TotalChunks)
  873. if err != nil {
  874. ctx.Error(500, fmt.Sprintf("CompleteMultiPartUpload failed: %v", err))
  875. return
  876. }
  877. } else {
  878. err = storage.CompleteObsMultiPartUpload(getChunkOBSExistObjectName(scene, fileChunk, fileName), uploadID, fileChunk.TotalChunks)
  879. if err != nil {
  880. ctx.Error(500, fmt.Sprintf("CompleteObsMultiPartUpload failed: %v", err))
  881. return
  882. }
  883. }
  884. fileChunk.IsUploaded = models.FileUploaded
  885. err = models.UpdateFileChunk(fileChunk)
  886. if err != nil {
  887. ctx.Error(500, fmt.Sprintf("UpdateFileChunk: %v", err))
  888. return
  889. }
  890. if scene == Attachment_model {
  891. //更新模型大小信息
  892. UpdateModelSize(modeluuid)
  893. _, err := models.InsertAttachment(&models.Attachment{
  894. UUID: uuid,
  895. UploaderID: ctx.User.ID,
  896. IsPrivate: true,
  897. Name: fileName,
  898. Size: ctx.QueryInt64("size"),
  899. DatasetID: 0,
  900. Description: modeluuid,
  901. Type: typeCloudBrain,
  902. })
  903. if err != nil {
  904. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  905. return
  906. }
  907. ctx.JSON(200, map[string]string{
  908. "result_code": "0",
  909. })
  910. } else {
  911. dataset, _ := models.GetDatasetByID(ctx.QueryInt64("dataset_id"))
  912. log.Warn("insert attachment to datasetId:" + strconv.FormatInt(dataset.ID, 10))
  913. attachment, err := models.InsertAttachment(&models.Attachment{
  914. UUID: uuid,
  915. UploaderID: ctx.User.ID,
  916. IsPrivate: dataset.IsPrivate(),
  917. Name: fileName,
  918. Size: ctx.QueryInt64("size"),
  919. DatasetID: ctx.QueryInt64("dataset_id"),
  920. Description: ctx.Query("description"),
  921. Type: typeCloudBrain,
  922. })
  923. if err != nil {
  924. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  925. return
  926. }
  927. attachment.UpdateDatasetUpdateUnix()
  928. repository, _ := models.GetRepositoryByID(dataset.RepoID)
  929. notification.NotifyOtherTask(ctx.User, repository, fmt.Sprint(repository.IsPrivate, attachment.IsPrivate), attachment.Name, models.ActionUploadAttachment)
  930. if attachment.DatasetID != 0 {
  931. if isCanDecompress(attachment.Name) {
  932. if typeCloudBrain == models.TypeCloudBrainOne {
  933. err = worker.SendDecompressTask(contexExt.Background(), uuid, attachment.Name)
  934. if err != nil {
  935. log.Error("SendDecompressTask(%s) failed:%s", uuid, err.Error())
  936. } else {
  937. updateAttachmentDecompressStateIng(attachment)
  938. }
  939. }
  940. if typeCloudBrain == models.TypeCloudBrainTwo {
  941. attachjson, _ := json.Marshal(attachment)
  942. err = labelmsg.SendDecompressAttachToLabelOBS(string(attachjson))
  943. if err != nil {
  944. log.Error("SendDecompressTask to labelsystem (%s) failed:%s", attachment.UUID, err.Error())
  945. } else {
  946. updateAttachmentDecompressStateIng(attachment)
  947. }
  948. }
  949. } else {
  950. var labelMap map[string]string
  951. labelMap = make(map[string]string)
  952. labelMap["UUID"] = uuid
  953. labelMap["Type"] = fmt.Sprint(attachment.Type)
  954. labelMap["UploaderID"] = fmt.Sprint(attachment.UploaderID)
  955. labelMap["RepoID"] = fmt.Sprint(dataset.RepoID)
  956. labelMap["AttachName"] = attachment.Name
  957. attachjson, _ := json.Marshal(labelMap)
  958. labelmsg.SendAddAttachToLabelSys(string(attachjson))
  959. }
  960. }
  961. }
  962. ctx.JSON(200, map[string]string{
  963. "result_code": "0",
  964. })
  965. }
  966. func HandleUnDecompressAttachment() {
  967. attachs, err := models.GetUnDecompressAttachments()
  968. if err != nil {
  969. log.Error("GetUnDecompressAttachments failed:", err.Error())
  970. return
  971. }
  972. for _, attach := range attachs {
  973. if attach.Type == models.TypeCloudBrainOne {
  974. err = worker.SendDecompressTask(contexExt.Background(), attach.UUID, attach.Name)
  975. if err != nil {
  976. log.Error("SendDecompressTask(%s) failed:%s", attach.UUID, err.Error())
  977. } else {
  978. updateAttachmentDecompressStateIng(attach)
  979. }
  980. } else if attach.Type == models.TypeCloudBrainTwo {
  981. attachjson, _ := json.Marshal(attach)
  982. err = labelmsg.SendDecompressAttachToLabelOBS(string(attachjson))
  983. if err != nil {
  984. log.Error("SendDecompressTask to labelsystem (%s) failed:%s", attach.UUID, err.Error())
  985. } else {
  986. updateAttachmentDecompressStateIng(attach)
  987. }
  988. }
  989. }
  990. return
  991. }
  992. func updateAttachmentDecompressStateIng(attach *models.Attachment) {
  993. attach.DecompressState = models.DecompressStateIng
  994. err := models.UpdateAttachment(attach)
  995. if err != nil {
  996. log.Error("UpdateAttachment state(%s) failed:%s", attach.UUID, err.Error())
  997. }
  998. }
  999. func QueryAllPublicDataset(ctx *context.Context) {
  1000. attachs, err := models.GetAllPublicAttachments()
  1001. if err != nil {
  1002. ctx.JSON(200, map[string]string{
  1003. "result_code": "-1",
  1004. "error_msg": err.Error(),
  1005. "data": "",
  1006. })
  1007. return
  1008. }
  1009. queryDatasets(ctx, attachs)
  1010. }
  1011. func QueryPrivateDataset(ctx *context.Context) {
  1012. username := ctx.Params(":username")
  1013. attachs, err := models.GetPrivateAttachments(username)
  1014. if err != nil {
  1015. ctx.JSON(200, map[string]string{
  1016. "result_code": "-1",
  1017. "error_msg": err.Error(),
  1018. "data": "",
  1019. })
  1020. return
  1021. }
  1022. for _, attach := range attachs {
  1023. attach.Name = username
  1024. }
  1025. queryDatasets(ctx, attachs)
  1026. }
  1027. func queryDatasets(ctx *context.Context, attachs []*models.AttachmentUsername) {
  1028. var datasets []CloudBrainDataset
  1029. if len(attachs) == 0 {
  1030. log.Info("dataset is null")
  1031. ctx.JSON(200, map[string]string{
  1032. "result_code": "0",
  1033. "error_msg": "",
  1034. "data": "",
  1035. })
  1036. return
  1037. }
  1038. for _, attch := range attachs {
  1039. has, err := storage.Attachments.HasObject(setting.Attachment.Minio.BasePath + models.AttachmentRelativePath(attch.UUID))
  1040. if err != nil || !has {
  1041. continue
  1042. }
  1043. datasets = append(datasets, CloudBrainDataset{strconv.FormatInt(attch.ID, 10),
  1044. attch.Attachment.Name,
  1045. setting.Attachment.Minio.RealPath +
  1046. setting.Attachment.Minio.Bucket + "/" +
  1047. setting.Attachment.Minio.BasePath +
  1048. models.AttachmentRelativePath(attch.UUID) +
  1049. attch.UUID,
  1050. attch.Name,
  1051. attch.CreatedUnix.Format("2006-01-02 03:04:05 PM")})
  1052. }
  1053. data, err := json.Marshal(datasets)
  1054. if err != nil {
  1055. log.Error("json.Marshal failed:", err.Error())
  1056. ctx.JSON(200, map[string]string{
  1057. "result_code": "-1",
  1058. "error_msg": err.Error(),
  1059. "data": "",
  1060. })
  1061. return
  1062. }
  1063. ctx.JSON(200, map[string]string{
  1064. "result_code": "0",
  1065. "error_msg": "",
  1066. "data": string(data),
  1067. })
  1068. return
  1069. }
  1070. func checkTypeCloudBrain(typeCloudBrain int) error {
  1071. if typeCloudBrain != models.TypeCloudBrainOne && typeCloudBrain != models.TypeCloudBrainTwo {
  1072. log.Error("type error:", typeCloudBrain)
  1073. return errors.New("type error")
  1074. }
  1075. return nil
  1076. }