119 lines
3.4 KiB
Go
119 lines
3.4 KiB
Go
package repo
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/charset"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/markup"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/typesniffer"
|
|
"code.gitea.io/gitea/modules/util"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
const (
|
|
tplRunFile base.TplName = "repo/editor/run"
|
|
)
|
|
|
|
func RunFile(ctx *context.Context) {
|
|
runFile(ctx, false)
|
|
}
|
|
|
|
func runFile(ctx *context.Context, isNewFile bool) {
|
|
ctx.Data["PageIsEdit"] = true
|
|
ctx.Data["IsNewFile"] = isNewFile
|
|
canCommit := renderCommitRights(ctx)
|
|
|
|
treePath := cleanUploadFileName(ctx.Repo.TreePath)
|
|
if treePath != ctx.Repo.TreePath {
|
|
if isNewFile {
|
|
ctx.Redirect(path.Join(ctx.Repo.RepoLink, "_new", util.PathEscapeSegments(ctx.Repo.BranchName), util.PathEscapeSegments(treePath)))
|
|
} else {
|
|
ctx.Redirect(path.Join(ctx.Repo.RepoLink, "_edit", util.PathEscapeSegments(ctx.Repo.BranchName), util.PathEscapeSegments(treePath)))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Check if the filename (and additional path) is specified in the querystring
|
|
// (filename is a misnomer, but kept for compatibility with GitHub)
|
|
filePath, fileName := path.Split(ctx.Req.URL.Query().Get("filename"))
|
|
filePath = strings.Trim(filePath, "/")
|
|
treeNames, treePaths := getParentTreeFields(path.Join(ctx.Repo.TreePath, filePath))
|
|
|
|
if !isNewFile {
|
|
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
|
|
if err != nil {
|
|
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
|
|
return
|
|
}
|
|
|
|
// No way to edit a directory online.
|
|
if entry.IsDir() {
|
|
ctx.NotFound("entry.IsDir", nil)
|
|
return
|
|
}
|
|
|
|
blob := entry.Blob()
|
|
if blob.Size() >= setting.UI.MaxDisplayFileSize {
|
|
ctx.NotFound("blob.Size", err)
|
|
return
|
|
}
|
|
|
|
dataRc, err := blob.DataAsync()
|
|
if err != nil {
|
|
ctx.NotFound("blob.Data", err)
|
|
return
|
|
}
|
|
|
|
defer dataRc.Close()
|
|
|
|
ctx.Data["FileSize"] = blob.Size()
|
|
ctx.Data["FileName"] = blob.Name()
|
|
|
|
buf := make([]byte, 1024)
|
|
n, _ := util.ReadAtMost(dataRc, buf)
|
|
buf = buf[:n]
|
|
|
|
// Only some file types are editable online as text.
|
|
if !typesniffer.DetectContentType(buf).IsRepresentableAsText() {
|
|
ctx.NotFound("typesniffer.IsRepresentableAsText", nil)
|
|
return
|
|
}
|
|
|
|
d, _ := io.ReadAll(dataRc)
|
|
|
|
buf = append(buf, d...)
|
|
if content, err := charset.ToUTF8(buf, charset.ConvertOpts{KeepBOM: true}); err != nil {
|
|
log.Error("ToUTF8: %v", err)
|
|
ctx.Data["FileContent"] = string(buf)
|
|
} else {
|
|
ctx.Data["FileContent"] = content
|
|
}
|
|
} else {
|
|
// Append filename from query, or empty string to allow user name the new file.
|
|
treeNames = append(treeNames, fileName)
|
|
}
|
|
|
|
ctx.Data["TreeNames"] = treeNames
|
|
ctx.Data["TreePaths"] = treePaths
|
|
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
|
|
ctx.Data["commit_summary"] = ""
|
|
ctx.Data["commit_message"] = ""
|
|
if canCommit {
|
|
ctx.Data["commit_choice"] = frmCommitChoiceDirect
|
|
} else {
|
|
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
|
|
}
|
|
ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx)
|
|
ctx.Data["last_commit"] = ctx.Repo.CommitID
|
|
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
|
|
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
|
|
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, treePath)
|
|
|
|
ctx.HTML(http.StatusOK, tplRunFile)
|
|
}
|