editor.go 15 KB

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