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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.UploadEnabled
  23. ctx.Data["UploadAllowedTypes"] = setting.UploadAllowedTypes
  24. ctx.Data["UploadMaxSize"] = setting.UploadMaxSize
  25. ctx.Data["UploadMaxFiles"] = setting.UploadMaxFiles
  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.PushCommit{
  136. commit.ID.String(),
  137. commit.Message(),
  138. commit.Author.Email,
  139. commit.Author.Name,
  140. }},
  141. }
  142. oldCommitID := ctx.Repo.CommitID
  143. newCommitID := commit.ID.String()
  144. if branchName != oldBranchName {
  145. oldCommitID = "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  146. }
  147. if err := models.CommitRepoAction(ctx.User.ID, ctx.Repo.Owner.ID, ctx.User.LowerName, ctx.Repo.Owner.Email,
  148. ctx.Repo.Repository.ID, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.Name, "refs/heads/"+branchName, pc,
  149. oldCommitID, newCommitID); err != nil {
  150. log.Error(4, "models.CommitRepoAction(branch = %s): %v", branchName, err)
  151. }
  152. models.HookQueue.Add(ctx.Repo.Repository.ID)
  153. }
  154. // Leaving this off until forked repos that get a branch can compare with forks master and not upstream
  155. //if oldBranchName != branchName {
  156. // ctx.Redirect(EscapeUrl(ctx.Repo.RepoLink + "/compare/" + oldBranchName + "..." + branchName))
  157. //} else {
  158. ctx.Redirect(EscapeUrl(ctx.Repo.RepoLink + "/src/" + branchName + "/" + treeName))
  159. //}
  160. }
  161. func UploadFileToServer(ctx *context.Context) {
  162. if !setting.UploadEnabled {
  163. ctx.Error(404, "upload is not enabled")
  164. return
  165. }
  166. file, header, err := ctx.Req.FormFile("file")
  167. if err != nil {
  168. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  169. return
  170. }
  171. defer file.Close()
  172. buf := make([]byte, 1024)
  173. n, _ := file.Read(buf)
  174. if n > 0 {
  175. buf = buf[:n]
  176. }
  177. fileType := http.DetectContentType(buf)
  178. if len(setting.UploadAllowedTypes) > 0 {
  179. allowedTypes := strings.Split(setting.UploadAllowedTypes, ",")
  180. allowed := false
  181. for _, t := range allowedTypes {
  182. t := strings.Trim(t, " ")
  183. if t == "*/*" || t == fileType {
  184. allowed = true
  185. break
  186. }
  187. }
  188. if !allowed {
  189. ctx.Error(400, ErrFileTypeForbidden.Error())
  190. return
  191. }
  192. }
  193. up, err := models.NewUpload(header.Filename, buf, file, ctx.User.ID, ctx.Repo.Repository.ID)
  194. if err != nil {
  195. ctx.Error(500, fmt.Sprintf("NewUpload: %v", err))
  196. return
  197. }
  198. log.Trace("New file uploaded: %s", up.UUID)
  199. ctx.JSON(200, map[string]string{
  200. "uuid": up.UUID,
  201. })
  202. }
  203. func RemoveUploadFileFromServer(ctx *context.Context, form auth.RemoveUploadFileForm) {
  204. if !setting.UploadEnabled {
  205. ctx.Error(404, "upload is not enabled")
  206. return
  207. }
  208. if len(form.File) == 0 {
  209. ctx.Error(404, "invalid params")
  210. return
  211. }
  212. uuid := form.File
  213. if err := models.RemoveUpload(uuid, ctx.User.ID, ctx.Repo.Repository.ID); err != nil {
  214. ctx.Error(500, fmt.Sprintf("RemoveUpload: %v", err))
  215. return
  216. }
  217. log.Trace("Upload file removed: %s", uuid)
  218. ctx.JSON(200, map[string]string{
  219. "uuid": uuid,
  220. })
  221. }