Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

editor.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. "io/ioutil"
  8. "net/http"
  9. "path"
  10. "strings"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/templates"
  19. )
  20. const (
  21. tplEditFile base.TplName = "repo/editor/edit"
  22. tplEditDiffPreview base.TplName = "repo/editor/diff_preview"
  23. tplDeleteFile base.TplName = "repo/editor/delete"
  24. tplUploadFile base.TplName = "repo/editor/upload"
  25. frmCommitChoiceDirect string = "direct"
  26. frmCommitChoiceNewBranch string = "commit-to-new-branch"
  27. )
  28. func renderCommitRights(ctx *context.Context) bool {
  29. canCommit, err := ctx.Repo.CanCommitToBranch(ctx.User)
  30. if err != nil {
  31. log.Error(4, "CanCommitToBranch: %v", err)
  32. }
  33. ctx.Data["CanCommitToBranch"] = canCommit
  34. return canCommit
  35. }
  36. // getParentTreeFields returns list of parent tree names and corresponding tree paths
  37. // based on given tree path.
  38. func getParentTreeFields(treePath string) (treeNames []string, treePaths []string) {
  39. if len(treePath) == 0 {
  40. return treeNames, treePaths
  41. }
  42. treeNames = strings.Split(treePath, "/")
  43. treePaths = make([]string, len(treeNames))
  44. for i := range treeNames {
  45. treePaths[i] = strings.Join(treeNames[:i+1], "/")
  46. }
  47. return treeNames, treePaths
  48. }
  49. func editFile(ctx *context.Context, isNewFile bool) {
  50. ctx.Data["PageIsEdit"] = true
  51. ctx.Data["IsNewFile"] = isNewFile
  52. ctx.Data["RequireHighlightJS"] = true
  53. ctx.Data["RequireSimpleMDE"] = true
  54. canCommit := renderCommitRights(ctx)
  55. treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
  56. if !isNewFile {
  57. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  58. if err != nil {
  59. ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
  60. return
  61. }
  62. // No way to edit a directory online.
  63. if entry.IsDir() {
  64. ctx.Handle(404, "entry.IsDir", nil)
  65. return
  66. }
  67. blob := entry.Blob()
  68. if blob.Size() >= setting.UI.MaxDisplayFileSize {
  69. ctx.Handle(404, "blob.Size", err)
  70. return
  71. }
  72. dataRc, err := blob.Data()
  73. if err != nil {
  74. ctx.Handle(404, "blob.Data", err)
  75. return
  76. }
  77. ctx.Data["FileSize"] = blob.Size()
  78. ctx.Data["FileName"] = blob.Name()
  79. buf := make([]byte, 1024)
  80. n, _ := dataRc.Read(buf)
  81. buf = buf[:n]
  82. // Only text file are editable online.
  83. if !base.IsTextFile(buf) {
  84. ctx.Handle(404, "base.IsTextFile", nil)
  85. return
  86. }
  87. d, _ := ioutil.ReadAll(dataRc)
  88. buf = append(buf, d...)
  89. if content, err := templates.ToUTF8WithErr(buf); err != nil {
  90. if err != nil {
  91. log.Error(4, "ToUTF8WithErr: %v", err)
  92. }
  93. ctx.Data["FileContent"] = string(buf)
  94. } else {
  95. ctx.Data["FileContent"] = content
  96. }
  97. } else {
  98. treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
  99. }
  100. ctx.Data["TreeNames"] = treeNames
  101. ctx.Data["TreePaths"] = treePaths
  102. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  103. ctx.Data["commit_summary"] = ""
  104. ctx.Data["commit_message"] = ""
  105. if canCommit {
  106. ctx.Data["commit_choice"] = frmCommitChoiceDirect
  107. } else {
  108. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  109. }
  110. ctx.Data["new_branch_name"] = ""
  111. ctx.Data["last_commit"] = ctx.Repo.Commit.ID
  112. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  113. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  114. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  115. ctx.Data["EditorconfigURLPrefix"] = fmt.Sprintf("%s/api/v1/repos/%s/editorconfig/", setting.AppSubURL, ctx.Repo.Repository.FullName())
  116. ctx.HTML(200, tplEditFile)
  117. }
  118. // EditFile render edit file page
  119. func EditFile(ctx *context.Context) {
  120. editFile(ctx, false)
  121. }
  122. // NewFile render create file page
  123. func NewFile(ctx *context.Context) {
  124. editFile(ctx, true)
  125. }
  126. func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bool) {
  127. ctx.Data["PageIsEdit"] = true
  128. ctx.Data["IsNewFile"] = isNewFile
  129. ctx.Data["RequireHighlightJS"] = true
  130. ctx.Data["RequireSimpleMDE"] = true
  131. canCommit := renderCommitRights(ctx)
  132. oldBranchName := ctx.Repo.BranchName
  133. branchName := oldBranchName
  134. oldTreePath := ctx.Repo.TreePath
  135. lastCommit := form.LastCommit
  136. form.LastCommit = ctx.Repo.Commit.ID.String()
  137. if form.CommitChoice == frmCommitChoiceNewBranch {
  138. branchName = form.NewBranchName
  139. }
  140. form.TreePath = strings.Trim(form.TreePath, " /")
  141. treeNames, treePaths := getParentTreeFields(form.TreePath)
  142. ctx.Data["TreePath"] = form.TreePath
  143. ctx.Data["TreeNames"] = treeNames
  144. ctx.Data["TreePaths"] = treePaths
  145. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + branchName
  146. ctx.Data["FileContent"] = form.Content
  147. ctx.Data["commit_summary"] = form.CommitSummary
  148. ctx.Data["commit_message"] = form.CommitMessage
  149. ctx.Data["commit_choice"] = form.CommitChoice
  150. ctx.Data["new_branch_name"] = branchName
  151. ctx.Data["last_commit"] = form.LastCommit
  152. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  153. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  154. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  155. if ctx.HasError() {
  156. ctx.HTML(200, tplEditFile)
  157. return
  158. }
  159. if len(form.TreePath) == 0 {
  160. ctx.Data["Err_TreePath"] = true
  161. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_cannot_be_empty"), tplEditFile, &form)
  162. return
  163. }
  164. if oldBranchName != branchName {
  165. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  166. ctx.Data["Err_NewBranchName"] = true
  167. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplEditFile, &form)
  168. return
  169. }
  170. } else if !canCommit {
  171. ctx.Data["Err_NewBranchName"] = true
  172. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  173. ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplEditFile, &form)
  174. return
  175. }
  176. var newTreePath string
  177. for index, part := range treeNames {
  178. newTreePath = path.Join(newTreePath, part)
  179. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
  180. if err != nil {
  181. if git.IsErrNotExist(err) {
  182. // Means there is no item with that name, so we're good
  183. break
  184. }
  185. ctx.Handle(500, "Repo.Commit.GetTreeEntryByPath", err)
  186. return
  187. }
  188. if index != len(treeNames)-1 {
  189. if !entry.IsDir() {
  190. ctx.Data["Err_TreePath"] = true
  191. ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), tplEditFile, &form)
  192. return
  193. }
  194. } else {
  195. if entry.IsLink() {
  196. ctx.Data["Err_TreePath"] = true
  197. ctx.RenderWithErr(ctx.Tr("repo.editor.file_is_a_symlink", part), tplEditFile, &form)
  198. return
  199. }
  200. if entry.IsDir() {
  201. ctx.Data["Err_TreePath"] = true
  202. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_a_directory", part), tplEditFile, &form)
  203. return
  204. }
  205. }
  206. }
  207. if !isNewFile {
  208. _, err := ctx.Repo.Commit.GetTreeEntryByPath(oldTreePath)
  209. if err != nil {
  210. if git.IsErrNotExist(err) {
  211. ctx.Data["Err_TreePath"] = true
  212. ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), tplEditFile, &form)
  213. } else {
  214. ctx.Handle(500, "GetTreeEntryByPath", err)
  215. }
  216. return
  217. }
  218. if lastCommit != ctx.Repo.CommitID {
  219. files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
  220. if err != nil {
  221. ctx.Handle(500, "GetFilesChangedSinceCommit", err)
  222. return
  223. }
  224. for _, file := range files {
  225. if file == form.TreePath {
  226. ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+lastCommit+"..."+ctx.Repo.CommitID), tplEditFile, &form)
  227. return
  228. }
  229. }
  230. }
  231. }
  232. if oldTreePath != form.TreePath {
  233. // We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber.
  234. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(form.TreePath)
  235. if err != nil {
  236. if !git.IsErrNotExist(err) {
  237. ctx.Handle(500, "GetTreeEntryByPath", err)
  238. return
  239. }
  240. }
  241. if entry != nil {
  242. ctx.Data["Err_TreePath"] = true
  243. ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", form.TreePath), tplEditFile, &form)
  244. return
  245. }
  246. }
  247. message := strings.TrimSpace(form.CommitSummary)
  248. if len(message) == 0 {
  249. if isNewFile {
  250. message = ctx.Tr("repo.editor.add", form.TreePath)
  251. } else {
  252. message = ctx.Tr("repo.editor.update", form.TreePath)
  253. }
  254. }
  255. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  256. if len(form.CommitMessage) > 0 {
  257. message += "\n\n" + form.CommitMessage
  258. }
  259. if err := ctx.Repo.Repository.UpdateRepoFile(ctx.User, models.UpdateRepoFileOptions{
  260. LastCommitID: lastCommit,
  261. OldBranch: oldBranchName,
  262. NewBranch: branchName,
  263. OldTreeName: oldTreePath,
  264. NewTreeName: form.TreePath,
  265. Message: message,
  266. Content: strings.Replace(form.Content, "\r", "", -1),
  267. IsNewFile: isNewFile,
  268. }); err != nil {
  269. ctx.Data["Err_TreePath"] = true
  270. ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, err), tplEditFile, &form)
  271. return
  272. }
  273. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName + "/" + strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(form.TreePath))
  274. }
  275. // EditFilePost response for editing file
  276. func EditFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  277. editFilePost(ctx, form, false)
  278. }
  279. // NewFilePost response for creating file
  280. func NewFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  281. editFilePost(ctx, form, true)
  282. }
  283. // DiffPreviewPost render preview diff page
  284. func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
  285. treePath := ctx.Repo.TreePath
  286. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
  287. if err != nil {
  288. ctx.Error(500, "GetTreeEntryByPath: "+err.Error())
  289. return
  290. } else if entry.IsDir() {
  291. ctx.Error(422)
  292. return
  293. }
  294. diff, err := ctx.Repo.Repository.GetDiffPreview(ctx.Repo.BranchName, treePath, form.Content)
  295. if err != nil {
  296. ctx.Error(500, "GetDiffPreview: "+err.Error())
  297. return
  298. }
  299. if diff.NumFiles() == 0 {
  300. ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
  301. return
  302. }
  303. ctx.Data["File"] = diff.Files[0]
  304. ctx.HTML(200, tplEditDiffPreview)
  305. }
  306. // DeleteFile render delete file page
  307. func DeleteFile(ctx *context.Context) {
  308. ctx.Data["PageIsDelete"] = true
  309. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  310. ctx.Data["TreePath"] = ctx.Repo.TreePath
  311. canCommit := renderCommitRights(ctx)
  312. ctx.Data["commit_summary"] = ""
  313. ctx.Data["commit_message"] = ""
  314. if canCommit {
  315. ctx.Data["commit_choice"] = frmCommitChoiceDirect
  316. } else {
  317. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  318. }
  319. ctx.Data["new_branch_name"] = ""
  320. ctx.HTML(200, tplDeleteFile)
  321. }
  322. // DeleteFilePost response for deleting file
  323. func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
  324. ctx.Data["PageIsDelete"] = true
  325. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  326. ctx.Data["TreePath"] = ctx.Repo.TreePath
  327. canCommit := renderCommitRights(ctx)
  328. oldBranchName := ctx.Repo.BranchName
  329. branchName := oldBranchName
  330. if form.CommitChoice == frmCommitChoiceNewBranch {
  331. branchName = form.NewBranchName
  332. }
  333. ctx.Data["commit_summary"] = form.CommitSummary
  334. ctx.Data["commit_message"] = form.CommitMessage
  335. ctx.Data["commit_choice"] = form.CommitChoice
  336. ctx.Data["new_branch_name"] = branchName
  337. if ctx.HasError() {
  338. ctx.HTML(200, tplDeleteFile)
  339. return
  340. }
  341. if oldBranchName != branchName {
  342. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  343. ctx.Data["Err_NewBranchName"] = true
  344. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplDeleteFile, &form)
  345. return
  346. }
  347. } else if !canCommit {
  348. ctx.Data["Err_NewBranchName"] = true
  349. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  350. ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplDeleteFile, &form)
  351. return
  352. }
  353. message := strings.TrimSpace(form.CommitSummary)
  354. if len(message) == 0 {
  355. message = ctx.Tr("repo.editor.delete", ctx.Repo.TreePath)
  356. }
  357. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  358. if len(form.CommitMessage) > 0 {
  359. message += "\n\n" + form.CommitMessage
  360. }
  361. if err := ctx.Repo.Repository.DeleteRepoFile(ctx.User, models.DeleteRepoFileOptions{
  362. LastCommitID: ctx.Repo.CommitID,
  363. OldBranch: oldBranchName,
  364. NewBranch: branchName,
  365. TreePath: ctx.Repo.TreePath,
  366. Message: message,
  367. }); err != nil {
  368. ctx.Handle(500, "DeleteRepoFile", err)
  369. return
  370. }
  371. ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath))
  372. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName)
  373. }
  374. func renderUploadSettings(ctx *context.Context) {
  375. ctx.Data["RequireDropzone"] = true
  376. ctx.Data["UploadAllowedTypes"] = strings.Join(setting.Repository.Upload.AllowedTypes, ",")
  377. ctx.Data["UploadMaxSize"] = setting.Repository.Upload.FileMaxSize
  378. ctx.Data["UploadMaxFiles"] = setting.Repository.Upload.MaxFiles
  379. }
  380. // UploadFile render upload file page
  381. func UploadFile(ctx *context.Context) {
  382. ctx.Data["PageIsUpload"] = true
  383. renderUploadSettings(ctx)
  384. canCommit := renderCommitRights(ctx)
  385. treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
  386. if len(treeNames) == 0 {
  387. // We must at least have one element for user to input.
  388. treeNames = []string{""}
  389. }
  390. ctx.Data["TreeNames"] = treeNames
  391. ctx.Data["TreePaths"] = treePaths
  392. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  393. ctx.Data["commit_summary"] = ""
  394. ctx.Data["commit_message"] = ""
  395. if canCommit {
  396. ctx.Data["commit_choice"] = frmCommitChoiceDirect
  397. } else {
  398. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  399. }
  400. ctx.Data["new_branch_name"] = ""
  401. ctx.HTML(200, tplUploadFile)
  402. }
  403. // UploadFilePost response for uploading file
  404. func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
  405. ctx.Data["PageIsUpload"] = true
  406. renderUploadSettings(ctx)
  407. canCommit := renderCommitRights(ctx)
  408. oldBranchName := ctx.Repo.BranchName
  409. branchName := oldBranchName
  410. if form.CommitChoice == frmCommitChoiceNewBranch {
  411. branchName = form.NewBranchName
  412. }
  413. form.TreePath = strings.Trim(form.TreePath, " /")
  414. treeNames, treePaths := getParentTreeFields(form.TreePath)
  415. if len(treeNames) == 0 {
  416. // We must at least have one element for user to input.
  417. treeNames = []string{""}
  418. }
  419. ctx.Data["TreePath"] = form.TreePath
  420. ctx.Data["TreeNames"] = treeNames
  421. ctx.Data["TreePaths"] = treePaths
  422. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + branchName
  423. ctx.Data["commit_summary"] = form.CommitSummary
  424. ctx.Data["commit_message"] = form.CommitMessage
  425. ctx.Data["commit_choice"] = form.CommitChoice
  426. ctx.Data["new_branch_name"] = branchName
  427. if ctx.HasError() {
  428. ctx.HTML(200, tplUploadFile)
  429. return
  430. }
  431. if oldBranchName != branchName {
  432. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  433. ctx.Data["Err_NewBranchName"] = true
  434. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form)
  435. return
  436. }
  437. } else if !canCommit {
  438. ctx.Data["Err_NewBranchName"] = true
  439. ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
  440. ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplUploadFile, &form)
  441. return
  442. }
  443. var newTreePath string
  444. for _, part := range treeNames {
  445. newTreePath = path.Join(newTreePath, part)
  446. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
  447. if err != nil {
  448. if git.IsErrNotExist(err) {
  449. // Means there is no item with that name, so we're good
  450. break
  451. }
  452. ctx.Handle(500, "Repo.Commit.GetTreeEntryByPath", err)
  453. return
  454. }
  455. // User can only upload files to a directory.
  456. if !entry.IsDir() {
  457. ctx.Data["Err_TreePath"] = true
  458. ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), tplUploadFile, &form)
  459. return
  460. }
  461. }
  462. message := strings.TrimSpace(form.CommitSummary)
  463. if len(message) == 0 {
  464. message = ctx.Tr("repo.editor.upload_files_to_dir", form.TreePath)
  465. }
  466. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  467. if len(form.CommitMessage) > 0 {
  468. message += "\n\n" + form.CommitMessage
  469. }
  470. if err := ctx.Repo.Repository.UploadRepoFiles(ctx.User, models.UploadRepoFileOptions{
  471. LastCommitID: ctx.Repo.CommitID,
  472. OldBranch: oldBranchName,
  473. NewBranch: branchName,
  474. TreePath: form.TreePath,
  475. Message: message,
  476. Files: form.Files,
  477. }); err != nil {
  478. ctx.Data["Err_TreePath"] = true
  479. ctx.RenderWithErr(ctx.Tr("repo.editor.unable_to_upload_files", form.TreePath, err), tplUploadFile, &form)
  480. return
  481. }
  482. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName + "/" + form.TreePath)
  483. }
  484. // UploadFileToServer upload file to server file dir not git
  485. func UploadFileToServer(ctx *context.Context) {
  486. file, header, err := ctx.Req.FormFile("file")
  487. if err != nil {
  488. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  489. return
  490. }
  491. defer file.Close()
  492. buf := make([]byte, 1024)
  493. n, _ := file.Read(buf)
  494. if n > 0 {
  495. buf = buf[:n]
  496. }
  497. fileType := http.DetectContentType(buf)
  498. if len(setting.Repository.Upload.AllowedTypes) > 0 {
  499. allowed := false
  500. for _, t := range setting.Repository.Upload.AllowedTypes {
  501. t := strings.Trim(t, " ")
  502. if t == "*/*" || t == fileType {
  503. allowed = true
  504. break
  505. }
  506. }
  507. if !allowed {
  508. ctx.Error(400, ErrFileTypeForbidden.Error())
  509. return
  510. }
  511. }
  512. upload, err := models.NewUpload(header.Filename, buf, file)
  513. if err != nil {
  514. ctx.Error(500, fmt.Sprintf("NewUpload: %v", err))
  515. return
  516. }
  517. log.Trace("New file uploaded: %s", upload.UUID)
  518. ctx.JSON(200, map[string]string{
  519. "uuid": upload.UUID,
  520. })
  521. }
  522. // RemoveUploadFileFromServer remove file from server file dir
  523. func RemoveUploadFileFromServer(ctx *context.Context, form auth.RemoveUploadFileForm) {
  524. if len(form.File) == 0 {
  525. ctx.Status(204)
  526. return
  527. }
  528. if err := models.DeleteUploadByUUID(form.File); err != nil {
  529. ctx.Error(500, fmt.Sprintf("DeleteUploadByUUID: %v", err))
  530. return
  531. }
  532. log.Trace("Upload file removed: %s", form.File)
  533. ctx.Status(204)
  534. }