|
-
-
-
-
-
- package repo
-
- import (
- "encoding/base64"
- "net/http"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/git"
- "code.gitea.io/gitea/modules/repofiles"
- api "code.gitea.io/gitea/modules/structs"
- "code.gitea.io/gitea/routers/repo"
- )
-
-
- func GetRawFile(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if ctx.Repo.Repository.IsEmpty {
- ctx.NotFound()
- return
- }
-
- blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
- if err != nil {
- if git.IsErrNotExist(err) {
- ctx.NotFound()
- } else {
- ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
- }
- return
- }
- if err = repo.ServeBlob(ctx.Context, blob); err != nil {
- ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
- }
- }
-
-
- func GetArchive(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
- gitRepo, err := git.OpenRepository(repoPath)
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
- return
- }
- ctx.Repo.GitRepo = gitRepo
-
- repo.Download(ctx.Context)
- }
-
-
- func GetEditorconfig(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ec, err := ctx.Repo.GetEditorconfig()
- if err != nil {
- if git.IsErrNotExist(err) {
- ctx.NotFound(err)
- } else {
- ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
- }
- return
- }
-
- fileName := ctx.Params("filename")
- def, err := ec.GetDefinitionForFilename(fileName)
- if def == nil {
- ctx.NotFound(err)
- return
- }
- ctx.JSON(http.StatusOK, def)
- }
-
-
- func CanWriteFiles(r *context.Repository) bool {
- return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
- }
-
-
- func CanReadFiles(r *context.Repository) bool {
- return r.Permission.CanRead(models.UnitTypeCode)
- }
-
-
- func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- opts := &repofiles.UpdateRepoFileOptions{
- Content: apiOpts.Content,
- IsNewFile: true,
- Message: apiOpts.Message,
- TreePath: ctx.Params("*"),
- OldBranch: apiOpts.BranchName,
- NewBranch: apiOpts.NewBranchName,
- Committer: &repofiles.IdentityOptions{
- Name: apiOpts.Committer.Name,
- Email: apiOpts.Committer.Email,
- },
- Author: &repofiles.IdentityOptions{
- Name: apiOpts.Author.Name,
- Email: apiOpts.Author.Email,
- },
- }
-
- if opts.Message == "" {
- opts.Message = ctx.Tr("repo.editor.add", opts.TreePath)
- }
-
- if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
- ctx.Error(http.StatusInternalServerError, "CreateFile", err)
- } else {
- ctx.JSON(http.StatusCreated, fileResponse)
- }
- }
-
-
- func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- opts := &repofiles.UpdateRepoFileOptions{
- Content: apiOpts.Content,
- SHA: apiOpts.SHA,
- IsNewFile: false,
- Message: apiOpts.Message,
- FromTreePath: apiOpts.FromPath,
- TreePath: ctx.Params("*"),
- OldBranch: apiOpts.BranchName,
- NewBranch: apiOpts.NewBranchName,
- Committer: &repofiles.IdentityOptions{
- Name: apiOpts.Committer.Name,
- Email: apiOpts.Committer.Email,
- },
- Author: &repofiles.IdentityOptions{
- Name: apiOpts.Author.Name,
- Email: apiOpts.Author.Email,
- },
- }
-
- if opts.Message == "" {
- opts.Message = ctx.Tr("repo.editor.update", opts.TreePath)
- }
-
- if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
- ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
- } else {
- ctx.JSON(http.StatusOK, fileResponse)
- }
- }
-
-
- func createOrUpdateFile(ctx *context.APIContext, opts *repofiles.UpdateRepoFileOptions) (*api.FileResponse, error) {
- if !CanWriteFiles(ctx.Repo) {
- return nil, models.ErrUserDoesNotHaveAccessToRepo{
- UserID: ctx.User.ID,
- RepoName: ctx.Repo.Repository.LowerName,
- }
- }
-
- content, err := base64.StdEncoding.DecodeString(opts.Content)
- if err != nil {
- return nil, err
- }
- opts.Content = string(content)
-
- return repofiles.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, opts)
- }
-
-
- func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if !CanWriteFiles(ctx.Repo) {
- ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
- UserID: ctx.User.ID,
- RepoName: ctx.Repo.Repository.LowerName,
- })
- return
- }
-
- opts := &repofiles.DeleteRepoFileOptions{
- Message: apiOpts.Message,
- OldBranch: apiOpts.BranchName,
- NewBranch: apiOpts.NewBranchName,
- SHA: apiOpts.SHA,
- TreePath: ctx.Params("*"),
- Committer: &repofiles.IdentityOptions{
- Name: apiOpts.Committer.Name,
- Email: apiOpts.Committer.Email,
- },
- Author: &repofiles.IdentityOptions{
- Name: apiOpts.Author.Name,
- Email: apiOpts.Author.Email,
- },
- }
-
- if opts.Message == "" {
- opts.Message = ctx.Tr("repo.editor.delete", opts.TreePath)
- }
-
- if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
- ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
- } else {
- ctx.JSON(http.StatusOK, fileResponse)
- }
- }
-
-
- func GetContents(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if !CanReadFiles(ctx.Repo) {
- ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
- UserID: ctx.User.ID,
- RepoName: ctx.Repo.Repository.LowerName,
- })
- return
- }
-
- treePath := ctx.Params("*")
- ref := ctx.QueryTrim("ref")
-
- if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
- ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
- } else {
- ctx.JSON(http.StatusOK, fileList)
- }
- }
-
-
- func GetContentsList(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- GetContents(ctx)
- }
|