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. tmplEditorEdit = "repo/editor/edit"
  23. tmplEditorDiffPreview = "repo/editor/diff_preview"
  24. tmplEditorDelete = "repo/editor/delete"
  25. tmplEditorUpload = "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, treePaths []string) {
  30. if treePath == "" {
  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.NotFoundOrError(gitutil.NewError(err), "get tree entry")
  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.Error(err, "get blob data")
  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(tmplEditorEdit)
  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(tmplEditorEdit)
  133. return
  134. }
  135. if f.TreePath == "" {
  136. c.FormErr("TreePath")
  137. c.RenderWithErr(c.Tr("repo.editor.filename_cannot_be_empty"), tmplEditorEdit, &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), tmplEditorEdit, &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.Error(err, "get tree entry")
  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), tmplEditorEdit, &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), tmplEditorEdit, &f)
  169. return
  170. } else if entry.IsTree() {
  171. c.FormErr("TreePath")
  172. c.RenderWithErr(c.Tr("repo.editor.filename_is_a_directory", part), tmplEditorEdit, &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), tmplEditorEdit, &f)
  183. } else {
  184. c.Error(err, "get tree entry")
  185. }
  186. return
  187. }
  188. if lastCommit != c.Repo.CommitID {
  189. files, err := c.Repo.Commit.FilesChangedAfter(lastCommit)
  190. if err != nil {
  191. c.Error(err, "get changed files")
  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), tmplEditorEdit, &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.Error(err, "get tree entry")
  208. return
  209. }
  210. }
  211. if entry != nil {
  212. c.FormErr("TreePath")
  213. c.RenderWithErr(c.Tr("repo.editor.file_already_exists", f.TreePath), tmplEditorEdit, &f)
  214. return
  215. }
  216. }
  217. message := strings.TrimSpace(f.CommitSummary)
  218. if message == "" {
  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. OldBranch: oldBranchName,
  231. NewBranch: branchName,
  232. OldTreeName: oldTreePath,
  233. NewTreeName: f.TreePath,
  234. Message: message,
  235. Content: strings.ReplaceAll(f.Content, "\r", ""),
  236. IsNewFile: isNewFile,
  237. }); err != nil {
  238. log.Error("Failed to update repo file: %v", err)
  239. c.FormErr("TreePath")
  240. c.RenderWithErr(c.Tr("repo.editor.fail_to_update_file", f.TreePath, errors.InternalServerError), tmplEditorEdit, &f)
  241. return
  242. }
  243. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  244. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  245. } else {
  246. c.Redirect(c.Repo.RepoLink + "/src/" + branchName + "/" + f.TreePath)
  247. }
  248. }
  249. func EditFilePost(c *context.Context, f form.EditRepoFile) {
  250. editFilePost(c, f, false)
  251. }
  252. func NewFilePost(c *context.Context, f form.EditRepoFile) {
  253. editFilePost(c, f, true)
  254. }
  255. func DiffPreviewPost(c *context.Context, f form.EditPreviewDiff) {
  256. treePath := c.Repo.TreePath
  257. entry, err := c.Repo.Commit.TreeEntry(treePath)
  258. if err != nil {
  259. c.Error(err, "get tree entry")
  260. return
  261. } else if entry.IsTree() {
  262. c.Status(http.StatusUnprocessableEntity)
  263. return
  264. }
  265. diff, err := c.Repo.Repository.GetDiffPreview(c.Repo.BranchName, treePath, f.Content)
  266. if err != nil {
  267. c.Error(err, "get diff preview")
  268. return
  269. }
  270. if diff.NumFiles() == 0 {
  271. c.PlainText(http.StatusOK, c.Tr("repo.editor.no_changes_to_show"))
  272. return
  273. }
  274. c.Data["File"] = diff.Files[0]
  275. c.Success(tmplEditorDiffPreview)
  276. }
  277. func DeleteFile(c *context.Context) {
  278. c.PageIs("Delete")
  279. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  280. c.Data["TreePath"] = c.Repo.TreePath
  281. c.Data["commit_summary"] = ""
  282. c.Data["commit_message"] = ""
  283. c.Data["commit_choice"] = "direct"
  284. c.Data["new_branch_name"] = ""
  285. c.Success(tmplEditorDelete)
  286. }
  287. func DeleteFilePost(c *context.Context, f form.DeleteRepoFile) {
  288. c.PageIs("Delete")
  289. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  290. c.Repo.TreePath = pathutil.Clean(c.Repo.TreePath)
  291. c.Data["TreePath"] = c.Repo.TreePath
  292. oldBranchName := c.Repo.BranchName
  293. branchName := oldBranchName
  294. if f.IsNewBrnach() {
  295. branchName = f.NewBranchName
  296. }
  297. c.Data["commit_summary"] = f.CommitSummary
  298. c.Data["commit_message"] = f.CommitMessage
  299. c.Data["commit_choice"] = f.CommitChoice
  300. c.Data["new_branch_name"] = branchName
  301. if c.HasError() {
  302. c.Success(tmplEditorDelete)
  303. return
  304. }
  305. if oldBranchName != branchName {
  306. if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
  307. c.FormErr("NewBranchName")
  308. c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), tmplEditorDelete, &f)
  309. return
  310. }
  311. }
  312. message := strings.TrimSpace(f.CommitSummary)
  313. if message == "" {
  314. message = c.Tr("repo.editor.delete", c.Repo.TreePath)
  315. }
  316. f.CommitMessage = strings.TrimSpace(f.CommitMessage)
  317. if len(f.CommitMessage) > 0 {
  318. message += "\n\n" + f.CommitMessage
  319. }
  320. if err := c.Repo.Repository.DeleteRepoFile(c.User, db.DeleteRepoFileOptions{
  321. LastCommitID: c.Repo.CommitID,
  322. OldBranch: oldBranchName,
  323. NewBranch: branchName,
  324. TreePath: c.Repo.TreePath,
  325. Message: message,
  326. }); err != nil {
  327. log.Error("Failed to delete repo file: %v", err)
  328. c.RenderWithErr(c.Tr("repo.editor.fail_to_delete_file", c.Repo.TreePath, errors.InternalServerError), tmplEditorDelete, &f)
  329. return
  330. }
  331. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  332. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  333. } else {
  334. c.Flash.Success(c.Tr("repo.editor.file_delete_success", c.Repo.TreePath))
  335. c.Redirect(c.Repo.RepoLink + "/src/" + branchName)
  336. }
  337. }
  338. func renderUploadSettings(c *context.Context) {
  339. c.RequireDropzone()
  340. c.Data["UploadAllowedTypes"] = strings.Join(conf.Repository.Upload.AllowedTypes, ",")
  341. c.Data["UploadMaxSize"] = conf.Repository.Upload.FileMaxSize
  342. c.Data["UploadMaxFiles"] = conf.Repository.Upload.MaxFiles
  343. }
  344. func UploadFile(c *context.Context) {
  345. c.PageIs("Upload")
  346. renderUploadSettings(c)
  347. treeNames, treePaths := getParentTreeFields(c.Repo.TreePath)
  348. if len(treeNames) == 0 {
  349. // We must at least have one element for user to input.
  350. treeNames = []string{""}
  351. }
  352. c.Data["TreeNames"] = treeNames
  353. c.Data["TreePaths"] = treePaths
  354. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  355. c.Data["commit_summary"] = ""
  356. c.Data["commit_message"] = ""
  357. c.Data["commit_choice"] = "direct"
  358. c.Data["new_branch_name"] = ""
  359. c.Success(tmplEditorUpload)
  360. }
  361. func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
  362. c.PageIs("Upload")
  363. renderUploadSettings(c)
  364. oldBranchName := c.Repo.BranchName
  365. branchName := oldBranchName
  366. if f.IsNewBrnach() {
  367. branchName = f.NewBranchName
  368. }
  369. f.TreePath = pathutil.Clean(f.TreePath)
  370. treeNames, treePaths := getParentTreeFields(f.TreePath)
  371. if len(treeNames) == 0 {
  372. // We must at least have one element for user to input.
  373. treeNames = []string{""}
  374. }
  375. c.Data["TreePath"] = f.TreePath
  376. c.Data["TreeNames"] = treeNames
  377. c.Data["TreePaths"] = treePaths
  378. c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName
  379. c.Data["commit_summary"] = f.CommitSummary
  380. c.Data["commit_message"] = f.CommitMessage
  381. c.Data["commit_choice"] = f.CommitChoice
  382. c.Data["new_branch_name"] = branchName
  383. if c.HasError() {
  384. c.Success(tmplEditorUpload)
  385. return
  386. }
  387. if oldBranchName != branchName {
  388. if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
  389. c.FormErr("NewBranchName")
  390. c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), tmplEditorUpload, &f)
  391. return
  392. }
  393. }
  394. var newTreePath string
  395. for _, part := range treeNames {
  396. newTreePath = path.Join(newTreePath, part)
  397. entry, err := c.Repo.Commit.TreeEntry(newTreePath)
  398. if err != nil {
  399. if gitutil.IsErrRevisionNotExist(err) {
  400. // Means there is no item with that name, so we're good
  401. break
  402. }
  403. c.Error(err, "get tree entry")
  404. return
  405. }
  406. // User can only upload files to a directory.
  407. if !entry.IsTree() {
  408. c.FormErr("TreePath")
  409. c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), tmplEditorUpload, &f)
  410. return
  411. }
  412. }
  413. message := strings.TrimSpace(f.CommitSummary)
  414. if message == "" {
  415. message = c.Tr("repo.editor.upload_files_to_dir", f.TreePath)
  416. }
  417. f.CommitMessage = strings.TrimSpace(f.CommitMessage)
  418. if len(f.CommitMessage) > 0 {
  419. message += "\n\n" + f.CommitMessage
  420. }
  421. if err := c.Repo.Repository.UploadRepoFiles(c.User, db.UploadRepoFileOptions{
  422. LastCommitID: c.Repo.CommitID,
  423. OldBranch: oldBranchName,
  424. NewBranch: branchName,
  425. TreePath: f.TreePath,
  426. Message: message,
  427. Files: f.Files,
  428. }); err != nil {
  429. log.Error("Failed to upload files: %v", err)
  430. c.FormErr("TreePath")
  431. c.RenderWithErr(c.Tr("repo.editor.unable_to_upload_files", f.TreePath, errors.InternalServerError), tmplEditorUpload, &f)
  432. return
  433. }
  434. if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
  435. c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
  436. } else {
  437. c.Redirect(c.Repo.RepoLink + "/src/" + branchName + "/" + f.TreePath)
  438. }
  439. }
  440. func UploadFileToServer(c *context.Context) {
  441. file, header, err := c.Req.FormFile("file")
  442. if err != nil {
  443. c.Error(err, "get file")
  444. return
  445. }
  446. defer file.Close()
  447. buf := make([]byte, 1024)
  448. n, _ := file.Read(buf)
  449. if n > 0 {
  450. buf = buf[:n]
  451. }
  452. fileType := http.DetectContentType(buf)
  453. if len(conf.Repository.Upload.AllowedTypes) > 0 {
  454. allowed := false
  455. for _, t := range conf.Repository.Upload.AllowedTypes {
  456. t := strings.Trim(t, " ")
  457. if t == "*/*" || t == fileType {
  458. allowed = true
  459. break
  460. }
  461. }
  462. if !allowed {
  463. c.PlainText(http.StatusBadRequest, ErrFileTypeForbidden.Error())
  464. return
  465. }
  466. }
  467. upload, err := db.NewUpload(header.Filename, buf, file)
  468. if err != nil {
  469. c.Error(err, "new upload")
  470. return
  471. }
  472. log.Trace("New file uploaded by user[%d]: %s", c.UserID(), upload.UUID)
  473. c.JSONSuccess(map[string]string{
  474. "uuid": upload.UUID,
  475. })
  476. }
  477. func RemoveUploadFileFromServer(c *context.Context, f form.RemoveUploadFile) {
  478. if f.File == "" {
  479. c.Status(http.StatusNoContent)
  480. return
  481. }
  482. if err := db.DeleteUploadByUUID(f.File); err != nil {
  483. c.Error(err, "delete upload by UUID")
  484. return
  485. }
  486. log.Trace("Upload file removed: %s", f.File)
  487. c.Status(http.StatusNoContent)
  488. }