editor.go 16 KB

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