選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

upload.go 7.2KB

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