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.
295 lines
9.8 KiB
295 lines
9.8 KiB
8 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.
|
||
|
|
||
3 years ago
|
package repo
|
||
8 years ago
|
|
||
|
import (
|
||
3 years ago
|
"context"
|
||
8 years ago
|
"fmt"
|
||
3 years ago
|
"net/url"
|
||
8 years ago
|
"path"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
8 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/storage"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
8 years ago
|
)
|
||
|
|
||
|
// Attachment represent a attachment of issue/comment/release.
|
||
|
type Attachment struct {
|
||
8 years ago
|
ID int64 `xorm:"pk autoincr"`
|
||
|
UUID string `xorm:"uuid UNIQUE"`
|
||
3 years ago
|
RepoID int64 `xorm:"INDEX"` // this should not be zero
|
||
|
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
|
||
|
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
|
||
6 years ago
|
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
|
||
8 years ago
|
CommentID int64
|
||
|
Name string
|
||
5 years ago
|
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||
|
Size int64 `xorm:"DEFAULT 0"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(Attachment))
|
||
|
}
|
||
|
|
||
8 years ago
|
// IncreaseDownloadCount is update download count + 1
|
||
|
func (a *Attachment) IncreaseDownloadCount() error {
|
||
|
// Update download count.
|
||
3 years ago
|
if _, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
|
||
8 years ago
|
return fmt.Errorf("increase attachment count: %v", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
// AttachmentRelativePath returns the relative path
|
||
|
func AttachmentRelativePath(uuid string) string {
|
||
|
return path.Join(uuid[0:1], uuid[1:2], uuid)
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
// RelativePath returns the relative path of the attachment
|
||
|
func (a *Attachment) RelativePath() string {
|
||
|
return AttachmentRelativePath(a.UUID)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// DownloadURL returns the download url of the attached file
|
||
|
func (a *Attachment) DownloadURL() string {
|
||
3 years ago
|
return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
|
||
7 years ago
|
}
|
||
|
|
||
|
// GetAttachmentByID returns attachment by given id
|
||
|
func GetAttachmentByID(id int64) (*Attachment, error) {
|
||
3 years ago
|
return getAttachmentByID(db.GetEngine(db.DefaultContext), id)
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
// _____ __ __ .__ __
|
||
|
// / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_
|
||
|
// / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\
|
||
|
// / | \ | | | / __ \\ \___| Y \ Y Y \ ___/| | \ |
|
||
|
// \____|__ /__| |__| (____ /\___ >___| /__|_| /\___ >___| /__|
|
||
|
// \/ \/ \/ \/ \/ \/ \/
|
||
|
|
||
|
// ErrAttachmentNotExist represents a "AttachmentNotExist" kind of error.
|
||
|
type ErrAttachmentNotExist struct {
|
||
|
ID int64
|
||
|
UUID string
|
||
|
}
|
||
|
|
||
|
// IsErrAttachmentNotExist checks if an error is a ErrAttachmentNotExist.
|
||
|
func IsErrAttachmentNotExist(err error) bool {
|
||
|
_, ok := err.(ErrAttachmentNotExist)
|
||
|
return ok
|
||
|
}
|
||
|
|
||
|
func (err ErrAttachmentNotExist) Error() string {
|
||
|
return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
|
||
|
}
|
||
|
|
||
3 years ago
|
func getAttachmentByID(e db.Engine, id int64) (*Attachment, error) {
|
||
5 years ago
|
attach := &Attachment{}
|
||
|
if has, err := e.ID(id).Get(attach); err != nil {
|
||
7 years ago
|
return nil, err
|
||
|
} else if !has {
|
||
|
return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
|
||
|
}
|
||
|
return attach, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func getAttachmentByUUID(e db.Engine, uuid string) (*Attachment, error) {
|
||
5 years ago
|
attach := &Attachment{}
|
||
|
has, err := e.Where("uuid=?", uuid).Get(attach)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
} else if !has {
|
||
|
return nil, ErrAttachmentNotExist{0, uuid}
|
||
|
}
|
||
|
return attach, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// GetAttachmentsByUUIDs returns attachment by given UUID list.
|
||
3 years ago
|
func GetAttachmentsByUUIDs(ctx context.Context, uuids []string) ([]*Attachment, error) {
|
||
|
return getAttachmentsByUUIDs(db.GetEngine(ctx), uuids)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
func getAttachmentsByUUIDs(e db.Engine, uuids []string) ([]*Attachment, error) {
|
||
8 years ago
|
if len(uuids) == 0 {
|
||
|
return []*Attachment{}, nil
|
||
|
}
|
||
|
|
||
|
// Silently drop invalid uuids.
|
||
|
attachments := make([]*Attachment, 0, len(uuids))
|
||
|
return attachments, e.In("uuid", uuids).Find(&attachments)
|
||
|
}
|
||
|
|
||
|
// GetAttachmentByUUID returns attachment by given UUID.
|
||
|
func GetAttachmentByUUID(uuid string) (*Attachment, error) {
|
||
3 years ago
|
return getAttachmentByUUID(db.GetEngine(db.DefaultContext), uuid)
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
// ExistAttachmentsByUUID returns true if attachment is exist by given UUID
|
||
|
func ExistAttachmentsByUUID(uuid string) (bool, error) {
|
||
3 years ago
|
return db.GetEngine(db.DefaultContext).Where("`uuid`=?", uuid).Exist(new(Attachment))
|
||
3 years ago
|
}
|
||
|
|
||
6 years ago
|
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
|
||
|
func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) {
|
||
3 years ago
|
return getAttachmentByReleaseIDFileName(db.GetEngine(db.DefaultContext), releaseID, fileName)
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
// GetAttachmentsByIssueIDCtx returns all attachments of an issue.
|
||
|
func GetAttachmentsByIssueIDCtx(ctx context.Context, issueID int64) ([]*Attachment, error) {
|
||
8 years ago
|
attachments := make([]*Attachment, 0, 10)
|
||
3 years ago
|
return attachments, db.GetEngine(ctx).Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
|
||
8 years ago
|
}
|
||
|
|
||
|
// GetAttachmentsByIssueID returns all attachments of an issue.
|
||
|
func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
|
||
3 years ago
|
return GetAttachmentsByIssueIDCtx(db.DefaultContext, issueID)
|
||
8 years ago
|
}
|
||
|
|
||
|
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
|
||
|
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
|
||
3 years ago
|
return GetAttachmentsByCommentIDCtx(db.DefaultContext, commentID)
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
// GetAttachmentsByCommentIDCtx returns all attachments if comment by given ID.
|
||
|
func GetAttachmentsByCommentIDCtx(ctx context.Context, commentID int64) ([]*Attachment, error) {
|
||
8 years ago
|
attachments := make([]*Attachment, 0, 10)
|
||
3 years ago
|
return attachments, db.GetEngine(ctx).Where("comment_id=?", commentID).Find(&attachments)
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
// getAttachmentByReleaseIDFileName return a file based on the the following infos:
|
||
3 years ago
|
func getAttachmentByReleaseIDFileName(e db.Engine, releaseID int64, fileName string) (*Attachment, error) {
|
||
6 years ago
|
attach := &Attachment{ReleaseID: releaseID, Name: fileName}
|
||
|
has, err := e.Get(attach)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
} else if !has {
|
||
|
return nil, err
|
||
|
}
|
||
|
return attach, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// DeleteAttachment deletes the given attachment and optionally the associated file.
|
||
|
func DeleteAttachment(a *Attachment, remove bool) error {
|
||
3 years ago
|
_, err := DeleteAttachments(db.DefaultContext, []*Attachment{a}, remove)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// DeleteAttachments deletes the given attachments and optionally the associated files.
|
||
3 years ago
|
func DeleteAttachments(ctx context.Context, attachments []*Attachment, remove bool) (int, error) {
|
||
7 years ago
|
if len(attachments) == 0 {
|
||
|
return 0, nil
|
||
|
}
|
||
|
|
||
4 years ago
|
ids := make([]int64, 0, len(attachments))
|
||
7 years ago
|
for _, a := range attachments {
|
||
|
ids = append(ids, a.ID)
|
||
|
}
|
||
|
|
||
3 years ago
|
cnt, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0])
|
||
7 years ago
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if remove {
|
||
|
for i, a := range attachments {
|
||
4 years ago
|
if err := storage.Attachments.Delete(a.RelativePath()); err != nil {
|
||
8 years ago
|
return i, err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
7 years ago
|
return int(cnt), nil
|
||
8 years ago
|
}
|
||
|
|
||
|
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
|
||
|
func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
|
||
|
attachments, err := GetAttachmentsByIssueID(issueID)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
3 years ago
|
return DeleteAttachments(db.DefaultContext, attachments, remove)
|
||
8 years ago
|
}
|
||
|
|
||
|
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
|
||
|
func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
|
||
|
attachments, err := GetAttachmentsByCommentID(commentID)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
3 years ago
|
return DeleteAttachments(db.DefaultContext, attachments, remove)
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
// UpdateAttachment updates the given attachment in database
|
||
|
func UpdateAttachment(atta *Attachment) error {
|
||
3 years ago
|
return UpdateAttachmentCtx(db.DefaultContext, atta)
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
// UpdateAttachmentByUUID Updates attachment via uuid
|
||
3 years ago
|
func UpdateAttachmentByUUID(ctx context.Context, attach *Attachment, cols ...string) error {
|
||
4 years ago
|
if attach.UUID == "" {
|
||
3 years ago
|
return fmt.Errorf("attachment uuid should be not blank")
|
||
4 years ago
|
}
|
||
3 years ago
|
_, err := db.GetEngine(ctx).Where("uuid=?", attach.UUID).Cols(cols...).Update(attach)
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
// UpdateAttachmentCtx updates the given attachment in database
|
||
|
func UpdateAttachmentCtx(ctx context.Context, atta *Attachment) error {
|
||
|
var sess = db.GetEngine(ctx).Cols("name", "issue_id", "release_id", "comment_id", "download_count")
|
||
7 years ago
|
if atta.ID != 0 && atta.UUID == "" {
|
||
3 years ago
|
sess = sess.ID(atta.ID)
|
||
7 years ago
|
} else {
|
||
|
// Use uuid only if id is not set and uuid is set
|
||
3 years ago
|
sess = sess.Where("uuid = ?", atta.UUID)
|
||
7 years ago
|
}
|
||
3 years ago
|
_, err := sess.Update(atta)
|
||
7 years ago
|
return err
|
||
|
}
|
||
5 years ago
|
|
||
|
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
|
||
|
func DeleteAttachmentsByRelease(releaseID int64) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Where("release_id = ?", releaseID).Delete(&Attachment{})
|
||
5 years ago
|
return err
|
||
|
}
|
||
4 years ago
|
|
||
|
// IterateAttachment iterates attachments; it should not be used when Gitea is servicing users.
|
||
|
func IterateAttachment(f func(attach *Attachment) error) error {
|
||
|
var start int
|
||
|
const batchSize = 100
|
||
|
for {
|
||
4 years ago
|
attachments := make([]*Attachment, 0, batchSize)
|
||
3 years ago
|
if err := db.GetEngine(db.DefaultContext).Limit(batchSize, start).Find(&attachments); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
if len(attachments) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
start += len(attachments)
|
||
|
|
||
|
for _, attach := range attachments {
|
||
|
if err := f(attach); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
// CountOrphanedAttachments returns the number of bad attachments
|
||
|
func CountOrphanedAttachments() (int64, error) {
|
||
3 years ago
|
return db.GetEngine(db.DefaultContext).Where("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))").
|
||
3 years ago
|
Count(new(Attachment))
|
||
|
}
|
||
|
|
||
|
// DeleteOrphanedAttachments delete all bad attachments
|
||
|
func DeleteOrphanedAttachments() error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Where("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))").
|
||
3 years ago
|
Delete(new(Attachment))
|
||
|
return err
|
||
|
}
|