|
|
@@ -5,9 +5,11 @@ |
|
|
|
package repository |
|
|
|
|
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/models" |
|
|
@@ -172,3 +174,60 @@ func RecommendFromPromote(url string) ([]string, error) { |
|
|
|
} |
|
|
|
return result, nil |
|
|
|
} |
|
|
|
|
|
|
|
func CheckRepoSizeLimit(repo *models.Repository, fileIds []string) error { |
|
|
|
if err := CheckRepoNumOnceLimit(len(fileIds)); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
totalSize, err := CountUploadFileSizeByIds(fileIds) |
|
|
|
if err != nil { |
|
|
|
return errors.New("upload files are invalid") |
|
|
|
} |
|
|
|
if err := CheckRepoTotalSizeLimit(repo, totalSize); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func CheckRepoTotalSizeLimit(repo *models.Repository, uploadFileSize int64) error { |
|
|
|
if repo.Size+uploadFileSize > setting.Repository.RepoMaxSize*1024*1024 { |
|
|
|
return errors.New(fmt.Sprintf("repository can not exceed %d MB", setting.Repository.RepoMaxSize)) |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func CheckRepoNumOnceLimit(uploadFileNum int) error { |
|
|
|
if uploadFileNum > setting.Repository.Upload.MaxFiles { |
|
|
|
return errors.New(fmt.Sprintf("can not upload more than %d files at a time", setting.Repository.Upload.MaxFiles)) |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func CountUploadFileSizeByIds(fileIds []string) (int64, error) { |
|
|
|
if len(fileIds) == 0 { |
|
|
|
return 0, nil |
|
|
|
} |
|
|
|
uploads, err := models.GetUploadsByUUIDs(fileIds) |
|
|
|
if err != nil { |
|
|
|
return 0, fmt.Errorf("CountUploadFileSizeByIds error [uuids: %v]: %v", fileIds, err) |
|
|
|
} |
|
|
|
var totalSize int64 |
|
|
|
for _, upload := range uploads { |
|
|
|
size, err := GetUploadFileSize(upload) |
|
|
|
if err != nil { |
|
|
|
return 0, err |
|
|
|
} |
|
|
|
totalSize += size |
|
|
|
} |
|
|
|
return totalSize, nil |
|
|
|
} |
|
|
|
|
|
|
|
func GetUploadFileSize(upload *models.Upload) (int64, error) { |
|
|
|
info, err := os.Lstat(upload.LocalPath()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return 0, err |
|
|
|
} |
|
|
|
return info.Size(), nil |
|
|
|
|
|
|
|
} |