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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. "fmt"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/storage"
  15. "code.gitea.io/gitea/modules/upload"
  16. gouuid "github.com/satori/go.uuid"
  17. )
  18. func RenderAttachmentSettings(ctx *context.Context) {
  19. renderAttachmentSettings(ctx)
  20. }
  21. func renderAttachmentSettings(ctx *context.Context) {
  22. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  23. ctx.Data["AttachmentStoreType"] = setting.Attachment.StoreType
  24. ctx.Data["AttachmentAllowedTypes"] = setting.Attachment.AllowedTypes
  25. ctx.Data["AttachmentMaxSize"] = setting.Attachment.MaxSize
  26. ctx.Data["AttachmentMaxFiles"] = setting.Attachment.MaxFiles
  27. }
  28. // UploadAttachment response for uploading issue's attachment
  29. func UploadAttachment(ctx *context.Context) {
  30. if !setting.Attachment.Enabled {
  31. ctx.Error(404, "attachment is not enabled")
  32. return
  33. }
  34. file, header, err := ctx.Req.FormFile("file")
  35. if err != nil {
  36. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  37. return
  38. }
  39. defer file.Close()
  40. buf := make([]byte, 1024)
  41. n, _ := file.Read(buf)
  42. if n > 0 {
  43. buf = buf[:n]
  44. }
  45. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.Attachment.AllowedTypes, ","))
  46. if err != nil {
  47. ctx.Error(400, err.Error())
  48. return
  49. }
  50. datasetID, _ := strconv.ParseInt(ctx.Req.FormValue("dataset_id"), 10, 64)
  51. attach, err := models.NewAttachment(&models.Attachment{
  52. IsPrivate: true,
  53. UploaderID: ctx.User.ID,
  54. Name: header.Filename,
  55. DatasetID: datasetID,
  56. }, buf, file)
  57. if err != nil {
  58. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  59. return
  60. }
  61. log.Trace("New attachment uploaded: %s", attach.UUID)
  62. ctx.JSON(200, map[string]string{
  63. "uuid": attach.UUID,
  64. })
  65. }
  66. func UpdatePublicAttachment(ctx *context.Context) {
  67. file := ctx.Query("file")
  68. isPrivate, _ := strconv.ParseBool(ctx.Query("is_private"))
  69. attach, err := models.GetAttachmentByUUID(file)
  70. if err != nil {
  71. ctx.Error(404, err.Error())
  72. return
  73. }
  74. attach.IsPrivate = isPrivate
  75. models.UpdateAttachment(attach)
  76. }
  77. // DeleteAttachment response for deleting issue's attachment
  78. func DeleteAttachment(ctx *context.Context) {
  79. file := ctx.Query("file")
  80. attach, err := models.GetAttachmentByUUID(file)
  81. if err != nil {
  82. ctx.Error(400, err.Error())
  83. return
  84. }
  85. if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
  86. ctx.Error(403)
  87. return
  88. }
  89. err = models.DeleteAttachment(attach, true)
  90. if err != nil {
  91. ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
  92. return
  93. }
  94. ctx.JSON(200, map[string]string{
  95. "uuid": attach.UUID,
  96. })
  97. }
  98. // GetAttachment serve attachements
  99. func GetAttachment(ctx *context.Context) {
  100. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  101. if err != nil {
  102. if models.IsErrAttachmentNotExist(err) {
  103. ctx.Error(404)
  104. } else {
  105. ctx.ServerError("GetAttachmentByUUID", err)
  106. }
  107. return
  108. }
  109. repository, unitType, err := attach.LinkedRepository()
  110. if err != nil {
  111. ctx.ServerError("LinkedRepository", err)
  112. return
  113. }
  114. if repository == nil { //If not linked
  115. if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) { //We block if not the uploader
  116. ctx.Error(http.StatusNotFound)
  117. return
  118. }
  119. } else { //If we have the repository we check access
  120. perm, err := models.GetUserRepoPermission(repository, ctx.User)
  121. if err != nil {
  122. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err.Error())
  123. return
  124. }
  125. if !perm.CanRead(unitType) {
  126. ctx.Error(http.StatusNotFound)
  127. return
  128. }
  129. }
  130. dataSet, err := attach.LinkedDataSet()
  131. if err != nil {
  132. ctx.ServerError("LinkedDataSet", err)
  133. return
  134. }
  135. if dataSet != nil {
  136. isPermit, err := models.GetUserDataSetPermission(dataSet, ctx.User)
  137. if err != nil {
  138. ctx.Error(http.StatusInternalServerError, "GetUserDataSetPermission", err.Error())
  139. return
  140. }
  141. if !isPermit {
  142. ctx.Error(http.StatusNotFound)
  143. return
  144. }
  145. }
  146. //If we have matched and access to release or issue
  147. if setting.Attachment.StoreType == storage.MinioStorageType {
  148. url, err := storage.Attachments.PresignedGetURL(attach.RelativePath(), attach.Name)
  149. if err != nil {
  150. ctx.ServerError("PresignedGetURL", err)
  151. return
  152. }
  153. if err = increaseDownloadCount(attach, dataSet); err != nil {
  154. ctx.ServerError("Update", err)
  155. return
  156. }
  157. http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
  158. } else {
  159. fr, err := storage.Attachments.Open(attach.RelativePath())
  160. if err != nil {
  161. ctx.ServerError("Open", err)
  162. return
  163. }
  164. defer fr.Close()
  165. if err = increaseDownloadCount(attach, dataSet); err != nil {
  166. ctx.ServerError("Update", err)
  167. return
  168. }
  169. if err = ServeData(ctx, attach.Name, fr); err != nil {
  170. ctx.ServerError("ServeData", err)
  171. return
  172. }
  173. }
  174. }
  175. func increaseDownloadCount(attach *models.Attachment, dataSet *models.Dataset) error {
  176. if err := attach.IncreaseDownloadCount(); err != nil {
  177. return err
  178. }
  179. if dataSet != nil {
  180. if err := models.IncreaseDownloadCount(dataSet.ID); err != nil {
  181. return err
  182. }
  183. }
  184. return nil
  185. }
  186. // Get a presigned url for put object
  187. func GetPresignedPutObjectURL(ctx *context.Context) {
  188. if !setting.Attachment.Enabled {
  189. ctx.Error(404, "attachment is not enabled")
  190. return
  191. }
  192. err := upload.VerifyFileType(ctx.Params("file_type"), strings.Split(setting.Attachment.AllowedTypes, ","))
  193. if err != nil {
  194. ctx.Error(400, err.Error())
  195. return
  196. }
  197. if setting.Attachment.StoreType == storage.MinioStorageType {
  198. uuid := gouuid.NewV4().String()
  199. url, err := storage.Attachments.PresignedPutURL(models.AttachmentRelativePath(uuid))
  200. if err != nil {
  201. ctx.ServerError("PresignedPutURL", err)
  202. return
  203. }
  204. ctx.JSON(200, map[string]string{
  205. "uuid": uuid,
  206. "url": url,
  207. })
  208. } else {
  209. ctx.Error(404, "storage type is not enabled")
  210. return
  211. }
  212. }
  213. // AddAttachment response for add attachment record
  214. func AddAttachment(ctx *context.Context) {
  215. uuid := ctx.Query("uuid")
  216. has, err := storage.Attachments.HasObject(models.AttachmentRelativePath(uuid))
  217. if err != nil {
  218. ctx.ServerError("HasObject", err)
  219. return
  220. }
  221. if !has {
  222. ctx.Error(404, "attachment has not been uploaded")
  223. return
  224. }
  225. _, err = models.InsertAttachment(&models.Attachment{
  226. UUID: uuid,
  227. UploaderID: ctx.User.ID,
  228. IsPrivate: true,
  229. Name: ctx.Query("file_name"),
  230. Size: ctx.QueryInt64("size"),
  231. DatasetID: ctx.QueryInt64("dataset_id"),
  232. })
  233. if err != nil {
  234. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  235. return
  236. }
  237. ctx.JSON(200, map[string]string{
  238. "result_code": "0",
  239. })
  240. }
  241. func NewMultipart(ctx *context.Context) {
  242. if !setting.Attachment.Enabled {
  243. ctx.Error(404, "attachment is not enabled")
  244. return
  245. }
  246. err := upload.VerifyFileType(ctx.Params("file_type"), strings.Split(setting.Attachment.AllowedTypes, ","))
  247. if err != nil {
  248. ctx.Error(400, err.Error())
  249. return
  250. }
  251. if setting.Attachment.StoreType == storage.MinioStorageType {
  252. uuid := gouuid.NewV4().String()
  253. url, err := storage.NewMultiPartUpload(uuid)
  254. if err != nil {
  255. ctx.ServerError("NewMultipart", err)
  256. return
  257. }
  258. ctx.JSON(200, map[string]string{
  259. "uuid": uuid,
  260. "url": url,
  261. })
  262. } else {
  263. ctx.Error(404, "storage type is not enabled")
  264. return
  265. }
  266. }
  267. func CompleteMultipart(ctx *context.Context) {
  268. uuid := ctx.Query("uuid")
  269. uploadID := ctx.Query("uploadID")
  270. completedParts := ctx.Query("completedParts")
  271. _, err := storage.CompleteMultiPartUpload(uuid, uploadID, completedParts)
  272. if err != nil {
  273. ctx.Error(500, fmt.Sprintf("CompleteMultiPartUpload failed: %v", err))
  274. return
  275. }
  276. _, err = models.InsertAttachment(&models.Attachment{
  277. UUID: uuid,
  278. UploaderID: ctx.User.ID,
  279. IsPrivate: true,
  280. Name: ctx.Query("file_name"),
  281. Size: ctx.QueryInt64("size"),
  282. DatasetID: ctx.QueryInt64("dataset_id"),
  283. })
  284. if err != nil {
  285. ctx.Error(500, fmt.Sprintf("InsertAttachment: %v", err))
  286. return
  287. }
  288. ctx.JSON(200, map[string]string{
  289. "result_code": "0",
  290. })
  291. }