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

upload.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2016 The Gogs 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. "strings"
  7. "fmt"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/context"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/setting"
  14. "net/http"
  15. "path"
  16. )
  17. const (
  18. UPLOAD base.TplName = "repo/upload"
  19. )
  20. func renderUploadSettings(ctx *context.Context) {
  21. ctx.Data["RequireDropzone"] = true
  22. ctx.Data["IsUploadEnabled"] = setting.Repository.Upload.Enabled
  23. ctx.Data["UploadAllowedTypes"] = strings.Join(setting.Repository.Upload.AllowedTypes, ",")
  24. ctx.Data["UploadMaxSize"] = setting.Repository.Upload.FileMaxSize
  25. ctx.Data["UploadMaxFiles"] = setting.Repository.Upload.MaxFiles
  26. }
  27. func UploadFile(ctx *context.Context) {
  28. ctx.Data["PageIsUpload"] = true
  29. userName := ctx.Repo.Owner.Name
  30. repoName := ctx.Repo.Repository.Name
  31. branchName := ctx.Repo.BranchName
  32. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  33. treeName := ctx.Repo.TreeName
  34. treeNames := []string{""}
  35. if len(treeName) > 0 {
  36. treeNames = strings.Split(treeName, "/")
  37. }
  38. ctx.Data["UserName"] = userName
  39. ctx.Data["RepoName"] = repoName
  40. ctx.Data["BranchName"] = branchName
  41. ctx.Data["TreeName"] = treeName
  42. ctx.Data["TreeNames"] = treeNames
  43. ctx.Data["BranchLink"] = branchLink
  44. ctx.Data["CommitSummary"] = ""
  45. ctx.Data["CommitMessage"] = ""
  46. ctx.Data["CommitChoice"] = "direct"
  47. ctx.Data["NewBranchName"] = ""
  48. ctx.Data["CommitDirectlyToThisBranch"] = ctx.Tr("repo.commit_directly_to_this_branch", "<strong class=\"branch-name\">"+branchName+"</strong>")
  49. ctx.Data["CreateNewBranch"] = ctx.Tr("repo.create_new_branch", "<strong>"+ctx.Tr("repo.new_branch")+"</strong>")
  50. renderUploadSettings(ctx)
  51. ctx.HTML(200, UPLOAD)
  52. }
  53. func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
  54. ctx.Data["PageIsUpload"] = true
  55. renderUploadSettings(ctx)
  56. userName := ctx.Repo.Owner.Name
  57. repoName := ctx.Repo.Repository.Name
  58. oldBranchName := ctx.Repo.BranchName
  59. branchName := oldBranchName
  60. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  61. commitChoice := form.CommitChoice
  62. files := form.Files
  63. if commitChoice == "commit-to-new-branch" {
  64. branchName = form.NewBranchName
  65. }
  66. treeName := form.TreeName
  67. treeName = strings.Trim(treeName, " ")
  68. treeName = strings.Trim(treeName, "/")
  69. treeNames := []string{""}
  70. if len(treeName) > 0 {
  71. treeNames = strings.Split(treeName, "/")
  72. }
  73. ctx.Data["UserName"] = userName
  74. ctx.Data["RepoName"] = repoName
  75. ctx.Data["BranchName"] = branchName
  76. ctx.Data["TreeName"] = treeName
  77. ctx.Data["TreeNames"] = treeNames
  78. ctx.Data["BranchLink"] = branchLink
  79. ctx.Data["CommitSummary"] = form.CommitSummary
  80. ctx.Data["CommitMessage"] = form.CommitMessage
  81. ctx.Data["CommitChoice"] = commitChoice
  82. ctx.Data["NewBranchName"] = branchName
  83. ctx.Data["CommitDirectlyToThisBranch"] = ctx.Tr("repo.commit_directly_to_this_branch", "<strong class=\"branch-name\">"+oldBranchName+"</strong>")
  84. ctx.Data["CreateNewBranch"] = ctx.Tr("repo.create_new_branch", "<strong>"+ctx.Tr("repo.new_branch")+"</strong>")
  85. if ctx.HasError() {
  86. ctx.HTML(200, UPLOAD)
  87. return
  88. }
  89. if oldBranchName != branchName {
  90. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  91. ctx.Data["Err_Branchname"] = true
  92. ctx.RenderWithErr(ctx.Tr("repo.branch_already_exists"), UPLOAD, &form)
  93. log.Error(4, "%s: %s - %s", "BranchName", branchName, "Branch already exists")
  94. return
  95. }
  96. }
  97. treepath := ""
  98. for _, part := range treeNames {
  99. treepath = path.Join(treepath, part)
  100. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treepath)
  101. if err != nil {
  102. // Means there is no item with that name, so we're good
  103. break
  104. }
  105. if !entry.IsDir() {
  106. ctx.Data["Err_Filename"] = true
  107. ctx.RenderWithErr(ctx.Tr("repo.directory_is_a_file"), UPLOAD, &form)
  108. log.Error(4, "%s: %s - %s", "UploadFile", treeName, "Directory given is a file")
  109. return
  110. }
  111. }
  112. message := ""
  113. if form.CommitSummary != "" {
  114. message = strings.Trim(form.CommitSummary, " ")
  115. } else {
  116. message = ctx.Tr("repo.add_files_to_dir", "'"+treeName+"'")
  117. }
  118. if strings.Trim(form.CommitMessage, " ") != "" {
  119. message += "\n\n" + strings.Trim(form.CommitMessage, " ")
  120. }
  121. if err := ctx.Repo.Repository.UploadRepoFiles(ctx.User, oldBranchName, branchName, treeName, message, files); err != nil {
  122. ctx.Data["Err_Directory"] = true
  123. ctx.RenderWithErr(ctx.Tr("repo.unable_to_upload_files"), UPLOAD, &form)
  124. log.Error(4, "%s: %v", "UploadFile", err)
  125. return
  126. }
  127. // Was successful, so now need to call models.CommitRepoAction() with the new commitID for webhooks and watchers
  128. if branch, err := ctx.Repo.Repository.GetBranch(branchName); err != nil {
  129. log.Error(4, "repo.Repository.GetBranch(%s): %v", branchName, err)
  130. } else if commit, err := branch.GetCommit(); err != nil {
  131. log.Error(4, "branch.GetCommit(): %v", err)
  132. } else {
  133. pc := &models.PushCommits{
  134. Len: 1,
  135. Commits: []*models.PushCommit{models.CommitToPushCommit(commit)},
  136. }
  137. oldCommitID := ctx.Repo.CommitID
  138. newCommitID := commit.ID.String()
  139. if branchName != oldBranchName {
  140. oldCommitID = "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  141. }
  142. if err := models.CommitRepoAction(ctx.User.ID, ctx.Repo.Owner.ID, ctx.User.LowerName, ctx.Repo.Owner.Email,
  143. ctx.Repo.Repository.ID, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.Name, "refs/heads/"+branchName, pc,
  144. oldCommitID, newCommitID); err != nil {
  145. log.Error(4, "models.CommitRepoAction(branch = %s): %v", branchName, err)
  146. }
  147. models.HookQueue.Add(ctx.Repo.Repository.ID)
  148. }
  149. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + treeName)
  150. }
  151. func UploadFileToServer(ctx *context.Context) {
  152. if !setting.Repository.Upload.Enabled {
  153. ctx.Error(404, "upload is not enabled")
  154. return
  155. }
  156. file, header, err := ctx.Req.FormFile("file")
  157. if err != nil {
  158. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  159. return
  160. }
  161. defer file.Close()
  162. buf := make([]byte, 1024)
  163. n, _ := file.Read(buf)
  164. if n > 0 {
  165. buf = buf[:n]
  166. }
  167. fileType := http.DetectContentType(buf)
  168. if len(setting.Repository.Upload.AllowedTypes) > 0 {
  169. allowed := false
  170. for _, t := range setting.Repository.Upload.AllowedTypes {
  171. t := strings.Trim(t, " ")
  172. if t == "*/*" || t == fileType {
  173. allowed = true
  174. break
  175. }
  176. }
  177. if !allowed {
  178. ctx.Error(400, ErrFileTypeForbidden.Error())
  179. return
  180. }
  181. }
  182. up, err := models.NewUpload(header.Filename, buf, file, ctx.User.ID, ctx.Repo.Repository.ID)
  183. if err != nil {
  184. ctx.Error(500, fmt.Sprintf("NewUpload: %v", err))
  185. return
  186. }
  187. log.Trace("New file uploaded: %s", up.UUID)
  188. ctx.JSON(200, map[string]string{
  189. "uuid": up.UUID,
  190. })
  191. }
  192. func RemoveUploadFileFromServer(ctx *context.Context, form auth.RemoveUploadFileForm) {
  193. if !setting.Repository.Upload.Enabled {
  194. ctx.Error(404, "upload is not enabled")
  195. return
  196. }
  197. if len(form.File) == 0 {
  198. ctx.Error(404, "invalid params")
  199. return
  200. }
  201. uuid := form.File
  202. if err := models.RemoveUpload(uuid, ctx.User.ID, ctx.Repo.Repository.ID); err != nil {
  203. ctx.Error(500, fmt.Sprintf("RemoveUpload: %v", err))
  204. return
  205. }
  206. log.Trace("Upload file removed: %s", uuid)
  207. ctx.JSON(200, map[string]string{
  208. "uuid": uuid,
  209. })
  210. }