(c *context.Context, isNewFile bool)
| 44 | } |
| 45 | |
| 46 | func editFile(c *context.Context, isNewFile bool) { |
| 47 | c.PageIs("Edit") |
| 48 | c.RequireHighlightJS() |
| 49 | c.RequireSimpleMDE() |
| 50 | c.Data["IsNewFile"] = isNewFile |
| 51 | |
| 52 | treeNames, treePaths := getParentTreeFields(c.Repo.TreePath) |
| 53 | |
| 54 | if !isNewFile { |
| 55 | entry, err := c.Repo.Commit.TreeEntry(c.Repo.TreePath) |
| 56 | if err != nil { |
| 57 | c.NotFoundOrError(gitutil.NewError(err), "get tree entry") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | // No way to edit a directory online. |
| 62 | if entry.IsTree() { |
| 63 | c.NotFound() |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | blob := entry.Blob() |
| 68 | p, err := blob.Bytes() |
| 69 | if err != nil { |
| 70 | c.Error(err, "get blob data") |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | c.Data["FileSize"] = blob.Size() |
| 75 | c.Data["FileName"] = blob.Name() |
| 76 | |
| 77 | // Only text file are editable online. |
| 78 | if !tool.IsTextFile(p) { |
| 79 | c.NotFound() |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | if content, err := template.ToUTF8WithErr(p); err != nil { |
| 84 | if err != nil { |
| 85 | log.Error("Failed to convert encoding to UTF-8: %v", err) |
| 86 | } |
| 87 | c.Data["FileContent"] = string(p) |
| 88 | } else { |
| 89 | c.Data["FileContent"] = content |
| 90 | } |
| 91 | } else { |
| 92 | treeNames = append(treeNames, "") // Append empty string to allow user name the new file. |
| 93 | } |
| 94 | |
| 95 | c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath) |
| 96 | c.Data["TreeNames"] = treeNames |
| 97 | c.Data["TreePaths"] = treePaths |
| 98 | c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName |
| 99 | c.Data["commit_summary"] = "" |
| 100 | c.Data["commit_message"] = "" |
| 101 | c.Data["commit_choice"] = "direct" |
| 102 | c.Data["new_branch_name"] = "" |
| 103 | c.Data["last_commit"] = c.Repo.Commit.ID |
no test coverage detected