editor.go 16 KB

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