You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
350 lines
10 KiB
350 lines
10 KiB
7 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package lfs
|
||
|
|
||
|
import (
|
||
4 years ago
|
"net/http"
|
||
7 years ago
|
"strconv"
|
||
7 years ago
|
"strings"
|
||
7 years ago
|
|
||
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/context"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/convert"
|
||
4 years ago
|
lfs_module "code.gitea.io/gitea/modules/lfs"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
6 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
4 years ago
|
jsoniter "github.com/json-iterator/go"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
//checkIsValidRequest check if it a valid request in case of bad request it write the response to ctx.
|
||
6 years ago
|
func checkIsValidRequest(ctx *context.Context) bool {
|
||
7 years ago
|
if !setting.LFS.StartServer {
|
||
5 years ago
|
log.Debug("Attempt to access LFS server but LFS server is disabled")
|
||
4 years ago
|
writeStatus(ctx, http.StatusNotFound)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
if !MetaMatcher(ctx.Req) {
|
||
4 years ago
|
log.Info("Attempt access LOCKs without accepting the correct media type: %s", lfs_module.MediaType)
|
||
4 years ago
|
writeStatus(ctx, http.StatusBadRequest)
|
||
7 years ago
|
return false
|
||
7 years ago
|
}
|
||
7 years ago
|
if !ctx.IsSigned {
|
||
|
user, _, _, err := parseToken(ctx.Req.Header.Get("Authorization"))
|
||
|
if err != nil {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
writeStatus(ctx, http.StatusUnauthorized)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
ctx.User = user
|
||
7 years ago
|
}
|
||
7 years ago
|
return true
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) {
|
||
7 years ago
|
if err != nil {
|
||
|
if models.IsErrLFSLockNotExist(err) {
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockList{
|
||
7 years ago
|
Locks: []*api.LFSLock{},
|
||
|
})
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
|
||
5 years ago
|
Message: "unable to list locks : Internal Server Error",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
6 years ago
|
if repo.ID != lock.RepoID {
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockList{
|
||
7 years ago
|
Locks: []*api.LFSLock{},
|
||
|
})
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockList{
|
||
4 years ago
|
Locks: []*api.LFSLock{convert.ToLFSLock(lock)},
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
|
// GetListLockHandler list locks
|
||
|
func GetListLockHandler(ctx *context.Context) {
|
||
6 years ago
|
if !checkIsValidRequest(ctx) {
|
||
5 years ago
|
// Status is written in checkIsValidRequest
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
||
7 years ago
|
|
||
4 years ago
|
rv, _ := unpack(ctx)
|
||
6 years ago
|
|
||
|
repository, err := models.GetRepositoryByOwnerAndName(rv.User, rv.Repo)
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
log.Debug("Could not find repository: %s/%s - %s", rv.User, rv.Repo, err)
|
||
|
writeStatus(ctx, 404)
|
||
|
return
|
||
|
}
|
||
|
repository.MustOwner()
|
||
|
|
||
|
authenticated := authenticate(ctx, repository, rv.Authorization, false)
|
||
|
if !authenticated {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
6 years ago
|
Message: "You must have pull access to list locks",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
5 years ago
|
|
||
|
cursor := ctx.QueryInt("cursor")
|
||
|
if cursor < 0 {
|
||
|
cursor = 0
|
||
|
}
|
||
|
limit := ctx.QueryInt("limit")
|
||
|
if limit > setting.LFS.LocksPagingNum && setting.LFS.LocksPagingNum > 0 {
|
||
|
limit = setting.LFS.LocksPagingNum
|
||
|
} else if limit < 0 {
|
||
|
limit = 0
|
||
|
}
|
||
7 years ago
|
id := ctx.Query("id")
|
||
|
if id != "" { //Case where we request a specific id
|
||
|
v, err := strconv.ParseInt(id, 10, 64)
|
||
|
if err != nil {
|
||
4 years ago
|
ctx.JSON(http.StatusBadRequest, api.LFSLockError{
|
||
7 years ago
|
Message: "bad request : " + err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
5 years ago
|
lock, err := models.GetLFSLockByID(v)
|
||
5 years ago
|
if err != nil && !models.IsErrLFSLockNotExist(err) {
|
||
|
log.Error("Unable to get lock with ID[%s]: Error: %v", v, err)
|
||
|
}
|
||
6 years ago
|
handleLockListOut(ctx, repository, lock, err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
path := ctx.Query("path")
|
||
|
if path != "" { //Case where we request a specific id
|
||
6 years ago
|
lock, err := models.GetLFSLock(repository, path)
|
||
5 years ago
|
if err != nil && !models.IsErrLFSLockNotExist(err) {
|
||
|
log.Error("Unable to get lock for repository %-v with path %s: Error: %v", repository, path, err)
|
||
|
}
|
||
6 years ago
|
handleLockListOut(ctx, repository, lock, err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
//If no query params path or id
|
||
5 years ago
|
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
|
||
4 years ago
|
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
|
||
5 years ago
|
Message: "unable to list locks : Internal Server Error",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
|
lockListAPI := make([]*api.LFSLock, len(lockList))
|
||
5 years ago
|
next := ""
|
||
7 years ago
|
for i, l := range lockList {
|
||
4 years ago
|
lockListAPI[i] = convert.ToLFSLock(l)
|
||
7 years ago
|
}
|
||
5 years ago
|
if limit > 0 && len(lockList) == limit {
|
||
|
next = strconv.Itoa(cursor + 1)
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockList{
|
||
7 years ago
|
Locks: lockListAPI,
|
||
5 years ago
|
Next: next,
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
|
// PostLockHandler create lock
|
||
|
func PostLockHandler(ctx *context.Context) {
|
||
6 years ago
|
if !checkIsValidRequest(ctx) {
|
||
5 years ago
|
// Status is written in checkIsValidRequest
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
||
7 years ago
|
|
||
6 years ago
|
userName := ctx.Params("username")
|
||
|
repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git")
|
||
|
authorization := ctx.Req.Header.Get("Authorization")
|
||
|
|
||
|
repository, err := models.GetRepositoryByOwnerAndName(userName, repoName)
|
||
|
if err != nil {
|
||
5 years ago
|
log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
|
||
6 years ago
|
writeStatus(ctx, 404)
|
||
|
return
|
||
|
}
|
||
|
repository.MustOwner()
|
||
|
|
||
|
authenticated := authenticate(ctx, repository, authorization, true)
|
||
|
if !authenticated {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
6 years ago
|
Message: "You must have push access to create locks",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
var req api.LFSLockRequest
|
||
4 years ago
|
bodyReader := ctx.Req.Body
|
||
5 years ago
|
defer bodyReader.Close()
|
||
4 years ago
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||
5 years ago
|
dec := json.NewDecoder(bodyReader)
|
||
6 years ago
|
if err := dec.Decode(&req); err != nil {
|
||
5 years ago
|
log.Warn("Failed to decode lock request as json. Error: %v", err)
|
||
7 years ago
|
writeStatus(ctx, 400)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
lock, err := models.CreateLFSLock(&models.LFSLock{
|
||
6 years ago
|
Repo: repository,
|
||
7 years ago
|
Path: req.Path,
|
||
|
Owner: ctx.User,
|
||
7 years ago
|
})
|
||
|
if err != nil {
|
||
|
if models.IsErrLFSLockAlreadyExist(err) {
|
||
4 years ago
|
ctx.JSON(http.StatusConflict, api.LFSLockError{
|
||
4 years ago
|
Lock: convert.ToLFSLock(lock),
|
||
7 years ago
|
Message: "already created lock",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
7 years ago
|
if models.IsErrLFSUnauthorizedAction(err) {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
7 years ago
|
Message: "You must have push access to create locks : " + err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
5 years ago
|
log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.User, err)
|
||
4 years ago
|
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
|
||
5 years ago
|
Message: "internal server error : Internal Server Error",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
|
||
7 years ago
|
}
|
||
|
|
||
|
// VerifyLockHandler list locks for verification
|
||
|
func VerifyLockHandler(ctx *context.Context) {
|
||
6 years ago
|
if !checkIsValidRequest(ctx) {
|
||
5 years ago
|
// Status is written in checkIsValidRequest
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
||
7 years ago
|
|
||
6 years ago
|
userName := ctx.Params("username")
|
||
|
repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git")
|
||
|
authorization := ctx.Req.Header.Get("Authorization")
|
||
|
|
||
|
repository, err := models.GetRepositoryByOwnerAndName(userName, repoName)
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
|
||
6 years ago
|
writeStatus(ctx, 404)
|
||
|
return
|
||
|
}
|
||
|
repository.MustOwner()
|
||
|
|
||
|
authenticated := authenticate(ctx, repository, authorization, true)
|
||
|
if !authenticated {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
6 years ago
|
Message: "You must have push access to verify locks",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
5 years ago
|
cursor := ctx.QueryInt("cursor")
|
||
|
if cursor < 0 {
|
||
|
cursor = 0
|
||
|
}
|
||
|
limit := ctx.QueryInt("limit")
|
||
|
if limit > setting.LFS.LocksPagingNum && setting.LFS.LocksPagingNum > 0 {
|
||
|
limit = setting.LFS.LocksPagingNum
|
||
|
} else if limit < 0 {
|
||
|
limit = 0
|
||
|
}
|
||
|
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
|
||
4 years ago
|
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
|
||
5 years ago
|
Message: "unable to list locks : Internal Server Error",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
5 years ago
|
next := ""
|
||
|
if limit > 0 && len(lockList) == limit {
|
||
|
next = strconv.Itoa(cursor + 1)
|
||
|
}
|
||
7 years ago
|
lockOursListAPI := make([]*api.LFSLock, 0, len(lockList))
|
||
|
lockTheirsListAPI := make([]*api.LFSLock, 0, len(lockList))
|
||
|
for _, l := range lockList {
|
||
|
if l.Owner.ID == ctx.User.ID {
|
||
4 years ago
|
lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(l))
|
||
7 years ago
|
} else {
|
||
4 years ago
|
lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l))
|
||
7 years ago
|
}
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockListVerify{
|
||
7 years ago
|
Ours: lockOursListAPI,
|
||
|
Theirs: lockTheirsListAPI,
|
||
5 years ago
|
Next: next,
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
|
// UnLockHandler delete locks
|
||
|
func UnLockHandler(ctx *context.Context) {
|
||
6 years ago
|
if !checkIsValidRequest(ctx) {
|
||
5 years ago
|
// Status is written in checkIsValidRequest
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
||
7 years ago
|
|
||
6 years ago
|
userName := ctx.Params("username")
|
||
|
repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git")
|
||
|
authorization := ctx.Req.Header.Get("Authorization")
|
||
|
|
||
|
repository, err := models.GetRepositoryByOwnerAndName(userName, repoName)
|
||
|
if err != nil {
|
||
5 years ago
|
log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
|
||
6 years ago
|
writeStatus(ctx, 404)
|
||
|
return
|
||
|
}
|
||
|
repository.MustOwner()
|
||
|
|
||
|
authenticated := authenticate(ctx, repository, authorization, true)
|
||
|
if !authenticated {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
6 years ago
|
Message: "You must have push access to delete locks",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
var req api.LFSLockDeleteRequest
|
||
4 years ago
|
bodyReader := ctx.Req.Body
|
||
5 years ago
|
defer bodyReader.Close()
|
||
4 years ago
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||
5 years ago
|
dec := json.NewDecoder(bodyReader)
|
||
6 years ago
|
if err := dec.Decode(&req); err != nil {
|
||
5 years ago
|
log.Warn("Failed to decode lock request as json. Error: %v", err)
|
||
7 years ago
|
writeStatus(ctx, 400)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
lock, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.User, req.Force)
|
||
|
if err != nil {
|
||
7 years ago
|
if models.IsErrLFSUnauthorizedAction(err) {
|
||
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
||
4 years ago
|
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
|
||
7 years ago
|
Message: "You must have push access to delete locks : " + err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
5 years ago
|
log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.ParamsInt64("lid"), ctx.User, req.Force, err)
|
||
4 years ago
|
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
|
||
5 years ago
|
Message: "unable to delete lock : Internal Server Error",
|
||
7 years ago
|
})
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
|
||
7 years ago
|
}
|