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

editor.go 18KB

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