editor.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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.CommitChoice == "commit-to-new-branch" {
  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. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + template.EscapePound(form.TreePath))
  246. }
  247. func EditFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  248. editFilePost(ctx, form, false)
  249. }
  250. func NewFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  251. editFilePost(ctx, form, true)
  252. }
  253. func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
  254. treePath := ctx.Repo.TreePath
  255. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
  256. if err != nil {
  257. ctx.Error(500, "GetTreeEntryByPath: "+err.Error())
  258. return
  259. } else if entry.IsDir() {
  260. ctx.Error(422)
  261. return
  262. }
  263. diff, err := ctx.Repo.Repository.GetDiffPreview(ctx.Repo.BranchName, treePath, form.Content)
  264. if err != nil {
  265. ctx.Error(500, "GetDiffPreview: "+err.Error())
  266. return
  267. }
  268. if diff.NumFiles() == 0 {
  269. ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
  270. return
  271. }
  272. ctx.Data["File"] = diff.Files[0]
  273. ctx.HTML(200, EDIT_DIFF_PREVIEW)
  274. }
  275. func DeleteFile(ctx *context.Context) {
  276. ctx.Data["PageIsDelete"] = true
  277. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  278. ctx.Data["TreePath"] = ctx.Repo.TreePath
  279. ctx.Data["commit_summary"] = ""
  280. ctx.Data["commit_message"] = ""
  281. ctx.Data["commit_choice"] = "direct"
  282. ctx.Data["new_branch_name"] = ""
  283. ctx.HTML(200, DELETE_FILE)
  284. }
  285. func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
  286. ctx.Data["PageIsDelete"] = true
  287. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  288. ctx.Data["TreePath"] = ctx.Repo.TreePath
  289. oldBranchName := ctx.Repo.BranchName
  290. branchName := oldBranchName
  291. if form.CommitChoice == "commit-to-new-branch" {
  292. branchName = form.NewBranchName
  293. }
  294. ctx.Data["commit_summary"] = form.CommitSummary
  295. ctx.Data["commit_message"] = form.CommitMessage
  296. ctx.Data["commit_choice"] = form.CommitChoice
  297. ctx.Data["new_branch_name"] = branchName
  298. if ctx.HasError() {
  299. ctx.HTML(200, DELETE_FILE)
  300. return
  301. }
  302. if oldBranchName != branchName {
  303. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  304. ctx.Data["Err_NewBranchName"] = true
  305. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), DELETE_FILE, &form)
  306. return
  307. }
  308. }
  309. message := strings.TrimSpace(form.CommitSummary)
  310. if len(message) == 0 {
  311. message = ctx.Tr("repo.editor.delete", ctx.Repo.TreePath)
  312. }
  313. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  314. if len(form.CommitMessage) > 0 {
  315. message += "\n\n" + form.CommitMessage
  316. }
  317. if err := ctx.Repo.Repository.DeleteRepoFile(ctx.User, models.DeleteRepoFileOptions{
  318. LastCommitID: ctx.Repo.CommitID,
  319. OldBranch: oldBranchName,
  320. NewBranch: branchName,
  321. TreePath: ctx.Repo.TreePath,
  322. Message: message,
  323. }); err != nil {
  324. ctx.Handle(500, "DeleteRepoFile", err)
  325. return
  326. }
  327. ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath))
  328. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName)
  329. }
  330. func renderUploadSettings(ctx *context.Context) {
  331. ctx.Data["RequireDropzone"] = true
  332. ctx.Data["UploadAllowedTypes"] = strings.Join(setting.Repository.Upload.AllowedTypes, ",")
  333. ctx.Data["UploadMaxSize"] = setting.Repository.Upload.FileMaxSize
  334. ctx.Data["UploadMaxFiles"] = setting.Repository.Upload.MaxFiles
  335. }
  336. func UploadFile(ctx *context.Context) {
  337. ctx.Data["PageIsUpload"] = true
  338. renderUploadSettings(ctx)
  339. treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
  340. if len(treeNames) == 0 {
  341. // We must at least have one element for user to input.
  342. treeNames = []string{""}
  343. }
  344. ctx.Data["TreeNames"] = treeNames
  345. ctx.Data["TreePaths"] = treePaths
  346. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  347. ctx.Data["commit_summary"] = ""
  348. ctx.Data["commit_message"] = ""
  349. ctx.Data["commit_choice"] = "direct"
  350. ctx.Data["new_branch_name"] = ""
  351. ctx.HTML(200, UPLOAD_FILE)
  352. }
  353. func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
  354. ctx.Data["PageIsUpload"] = true
  355. renderUploadSettings(ctx)
  356. oldBranchName := ctx.Repo.BranchName
  357. branchName := oldBranchName
  358. if form.CommitChoice == "commit-to-new-branch" {
  359. branchName = form.NewBranchName
  360. }
  361. form.TreePath = strings.Trim(form.TreePath, " /")
  362. treeNames, treePaths := getParentTreeFields(form.TreePath)
  363. if len(treeNames) == 0 {
  364. // We must at least have one element for user to input.
  365. treeNames = []string{""}
  366. }
  367. ctx.Data["TreePath"] = form.TreePath
  368. ctx.Data["TreeNames"] = treeNames
  369. ctx.Data["TreePaths"] = treePaths
  370. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName
  371. ctx.Data["commit_summary"] = form.CommitSummary
  372. ctx.Data["commit_message"] = form.CommitMessage
  373. ctx.Data["commit_choice"] = form.CommitChoice
  374. ctx.Data["new_branch_name"] = branchName
  375. if ctx.HasError() {
  376. ctx.HTML(200, UPLOAD_FILE)
  377. return
  378. }
  379. if oldBranchName != branchName {
  380. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  381. ctx.Data["Err_NewBranchName"] = true
  382. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), UPLOAD_FILE, &form)
  383. return
  384. }
  385. }
  386. var newTreePath string
  387. for _, part := range treeNames {
  388. newTreePath = path.Join(newTreePath, part)
  389. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
  390. if err != nil {
  391. if git.IsErrNotExist(err) {
  392. // Means there is no item with that name, so we're good
  393. break
  394. }
  395. ctx.Handle(500, "Repo.Commit.GetTreeEntryByPath", err)
  396. return
  397. }
  398. // User can only upload files to a directory.
  399. if !entry.IsDir() {
  400. ctx.Data["Err_TreePath"] = true
  401. ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), UPLOAD_FILE, &form)
  402. return
  403. }
  404. }
  405. message := strings.TrimSpace(form.CommitSummary)
  406. if len(message) == 0 {
  407. message = ctx.Tr("repo.editor.upload_files_to_dir", form.TreePath)
  408. }
  409. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  410. if len(form.CommitMessage) > 0 {
  411. message += "\n\n" + form.CommitMessage
  412. }
  413. if err := ctx.Repo.Repository.UploadRepoFiles(ctx.User, models.UploadRepoFileOptions{
  414. LastCommitID: ctx.Repo.CommitID,
  415. OldBranch: oldBranchName,
  416. NewBranch: branchName,
  417. TreePath: form.TreePath,
  418. Message: message,
  419. Files: form.Files,
  420. }); err != nil {
  421. ctx.Data["Err_TreePath"] = true
  422. ctx.RenderWithErr(ctx.Tr("repo.editor.unable_to_upload_files", form.TreePath, err), UPLOAD_FILE, &form)
  423. return
  424. }
  425. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + form.TreePath)
  426. }
  427. func UploadFileToServer(ctx *context.Context) {
  428. file, header, err := ctx.Req.FormFile("file")
  429. if err != nil {
  430. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  431. return
  432. }
  433. defer file.Close()
  434. buf := make([]byte, 1024)
  435. n, _ := file.Read(buf)
  436. if n > 0 {
  437. buf = buf[:n]
  438. }
  439. fileType := http.DetectContentType(buf)
  440. if len(setting.Repository.Upload.AllowedTypes) > 0 {
  441. allowed := false
  442. for _, t := range setting.Repository.Upload.AllowedTypes {
  443. t := strings.Trim(t, " ")
  444. if t == "*/*" || t == fileType {
  445. allowed = true
  446. break
  447. }
  448. }
  449. if !allowed {
  450. ctx.Error(400, ErrFileTypeForbidden.Error())
  451. return
  452. }
  453. }
  454. upload, err := models.NewUpload(header.Filename, buf, file)
  455. if err != nil {
  456. ctx.Error(500, fmt.Sprintf("NewUpload: %v", err))
  457. return
  458. }
  459. log.Trace("New file uploaded: %s", upload.UUID)
  460. ctx.JSON(200, map[string]string{
  461. "uuid": upload.UUID,
  462. })
  463. }
  464. func RemoveUploadFileFromServer(ctx *context.Context, form auth.RemoveUploadFileForm) {
  465. if len(form.File) == 0 {
  466. ctx.Status(204)
  467. return
  468. }
  469. if err := models.DeleteUploadByUUID(form.File); err != nil {
  470. ctx.Error(500, fmt.Sprintf("DeleteUploadByUUID: %v", err))
  471. return
  472. }
  473. log.Trace("Upload file removed: %s", form.File)
  474. ctx.Status(204)
  475. }