editor.go 15 KB

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