2019-02-12 18:37:31 +05:30
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-12 18:37:31 +05:30
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
package repo
|
2019-02-12 18:37:31 +05:30
|
|
|
|
|
|
|
import (
|
2023-09-15 11:43:19 +05:30
|
|
|
"context"
|
2019-02-12 18:37:31 +05:30
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2020-11-28 08:12:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-02-12 18:37:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-08-23 22:10:30 +05:30
|
|
|
|
2020-06-18 14:48:44 +05:30
|
|
|
gouuid "github.com/google/uuid"
|
2019-02-12 18:37:31 +05:30
|
|
|
)
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
// ErrUploadNotExist represents a "UploadNotExist" kind of error.
|
|
|
|
type ErrUploadNotExist struct {
|
|
|
|
ID int64
|
|
|
|
UUID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrUploadNotExist checks if an error is a ErrUploadNotExist.
|
|
|
|
func IsErrUploadNotExist(err error) bool {
|
|
|
|
_, ok := err.(ErrUploadNotExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrUploadNotExist) Error() string {
|
|
|
|
return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
|
|
|
|
}
|
2019-02-12 18:37:31 +05:30
|
|
|
|
2022-10-18 11:20:37 +05:30
|
|
|
func (err ErrUploadNotExist) Unwrap() error {
|
|
|
|
return util.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:37:31 +05:30
|
|
|
// Upload represent a uploaded file to a repo to be deleted when moved
|
|
|
|
type Upload struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UUID string `xorm:"uuid UNIQUE"`
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Upload))
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:37:31 +05:30
|
|
|
// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
|
|
|
|
func UploadLocalPath(uuid string) string {
|
|
|
|
return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalPath returns where uploads are temporarily stored in local file system.
|
|
|
|
func (upload *Upload) LocalPath() string {
|
|
|
|
return UploadLocalPath(upload.UUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpload creates a new upload object.
|
2023-09-15 11:43:19 +05:30
|
|
|
func NewUpload(ctx context.Context, name string, buf []byte, file multipart.File) (_ *Upload, err error) {
|
2019-02-12 18:37:31 +05:30
|
|
|
upload := &Upload{
|
2020-06-18 14:48:44 +05:30
|
|
|
UUID: gouuid.New().String(),
|
2019-02-12 18:37:31 +05:30
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
localPath := upload.LocalPath()
|
|
|
|
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("MkdirAll: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fw, err := os.Create(localPath)
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Create: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
defer fw.Close()
|
|
|
|
|
|
|
|
if _, err = fw.Write(buf); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Write: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
} else if _, err = io.Copy(fw, file); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Copy: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
if _, err := db.GetEngine(ctx).Insert(upload); err != nil {
|
2019-02-12 18:37:31 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUploadByUUID returns the Upload by UUID
|
2023-09-15 11:43:19 +05:30
|
|
|
func GetUploadByUUID(ctx context.Context, uuid string) (*Upload, error) {
|
2020-06-17 23:20:11 +05:30
|
|
|
upload := &Upload{}
|
2023-09-15 11:43:19 +05:30
|
|
|
has, err := db.GetEngine(ctx).Where("uuid=?", uuid).Get(upload)
|
2019-02-12 18:37:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrUploadNotExist{0, uuid}
|
|
|
|
}
|
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUploadsByUUIDs returns multiple uploads by UUIDS
|
2023-09-15 11:43:19 +05:30
|
|
|
func GetUploadsByUUIDs(ctx context.Context, uuids []string) ([]*Upload, error) {
|
2019-02-12 18:37:31 +05:30
|
|
|
if len(uuids) == 0 {
|
|
|
|
return []*Upload{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silently drop invalid uuids.
|
|
|
|
uploads := make([]*Upload, 0, len(uuids))
|
2023-09-15 11:43:19 +05:30
|
|
|
return uploads, db.GetEngine(ctx).In("uuid", uuids).Find(&uploads)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUploads deletes multiple uploads
|
2023-09-15 11:43:19 +05:30
|
|
|
func DeleteUploads(ctx context.Context, uploads ...*Upload) (err error) {
|
2019-02-12 18:37:31 +05:30
|
|
|
if len(uploads) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2021-11-21 21:11:00 +05:30
|
|
|
if err != nil {
|
2019-02-12 18:37:31 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
defer committer.Close()
|
2019-02-12 18:37:31 +05:30
|
|
|
|
|
|
|
ids := make([]int64, len(uploads))
|
|
|
|
for i := 0; i < len(uploads); i++ {
|
|
|
|
ids[i] = uploads[i].ID
|
|
|
|
}
|
2023-12-26 01:55:29 +05:30
|
|
|
if err = db.DeleteByIDs[Upload](ctx, ids...); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("delete uploads: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
if err = committer.Commit(); err != nil {
|
2019-02-12 18:37:31 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, upload := range uploads {
|
|
|
|
localPath := upload.LocalPath()
|
2020-11-28 08:12:08 +05:30
|
|
|
isFile, err := util.IsFile(localPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
|
|
|
|
}
|
|
|
|
if !isFile {
|
2019-02-12 18:37:31 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-12 01:35:34 +05:30
|
|
|
if err := util.Remove(localPath); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("remove upload: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUploadByUUID deletes a upload by UUID
|
2023-09-15 11:43:19 +05:30
|
|
|
func DeleteUploadByUUID(ctx context.Context, uuid string) error {
|
|
|
|
upload, err := GetUploadByUUID(ctx, uuid)
|
2019-02-12 18:37:31 +05:30
|
|
|
if err != nil {
|
|
|
|
if IsErrUploadNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("GetUploadByUUID: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
if err := DeleteUploads(ctx, upload); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("DeleteUpload: %w", err)
|
2019-02-12 18:37:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|