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.

upload.go 7.6 kB

Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
9 years ago
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. }