diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-19 21:39:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 21:39:57 +0800 |
commit | fc3d0826096943b979717ed46c9a4cfd86e06106 (patch) | |
tree | 3143882ccf4dea3a8bf2a0de9c8da9a4efec26ce /models/repo | |
parent | 7a0347315995b25bcb2dca4786504fb699b5f004 (diff) | |
download | gitea-fc3d0826096943b979717ed46c9a4cfd86e06106.tar.gz gitea-fc3d0826096943b979717ed46c9a4cfd86e06106.zip |
Move attachment into models/repo/ (#17650)
* Move attachment into models/repo/
* Fix test
* Fix bug
Diffstat (limited to 'models/repo')
-rw-r--r-- | models/repo/attachment.go | 294 | ||||
-rw-r--r-- | models/repo/attachment_test.go | 104 | ||||
-rw-r--r-- | models/repo/main_test.go | 18 |
3 files changed, 416 insertions, 0 deletions
diff --git a/models/repo/attachment.go b/models/repo/attachment.go new file mode 100644 index 0000000000..3fb331a202 --- /dev/null +++ b/models/repo/attachment.go @@ -0,0 +1,294 @@ +// 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 repo + +import ( + "context" + "fmt" + "net/url" + "path" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/storage" + "code.gitea.io/gitea/modules/timeutil" +) + +// Attachment represent a attachment of issue/comment/release. +type Attachment struct { + ID int64 `xorm:"pk autoincr"` + UUID string `xorm:"uuid UNIQUE"` + 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 + UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added + CommentID int64 + Name string + DownloadCount int64 `xorm:"DEFAULT 0"` + Size int64 `xorm:"DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` +} + +func init() { + db.RegisterModel(new(Attachment)) +} + +// IncreaseDownloadCount is update download count + 1 +func (a *Attachment) IncreaseDownloadCount() error { + // Update download count. + if _, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil { + return fmt.Errorf("increase attachment count: %v", err) + } + + return nil +} + +// AttachmentRelativePath returns the relative path +func AttachmentRelativePath(uuid string) string { + return path.Join(uuid[0:1], uuid[1:2], uuid) +} + +// RelativePath returns the relative path of the attachment +func (a *Attachment) RelativePath() string { + return AttachmentRelativePath(a.UUID) +} + +// DownloadURL returns the download url of the attached file +func (a *Attachment) DownloadURL() string { + return setting.AppURL + "attachments/" + url.PathEscape(a.UUID) +} + +// GetAttachmentByID returns attachment by given id +func GetAttachmentByID(id int64) (*Attachment, error) { + return getAttachmentByID(db.GetEngine(db.DefaultContext), id) +} + +// _____ __ __ .__ __ +// / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_ +// / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\ +// / | \ | | | / __ \\ \___| 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) +} + +func getAttachmentByID(e db.Engine, id int64) (*Attachment, error) { + attach := &Attachment{} + if has, err := e.ID(id).Get(attach); err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{ID: id, UUID: ""} + } + return attach, nil +} + +func getAttachmentByUUID(e db.Engine, uuid string) (*Attachment, error) { + attach := &Attachment{} + has, err := e.Where("uuid=?", uuid).Get(attach) + if err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{0, uuid} + } + return attach, nil +} + +// GetAttachmentsByUUIDs returns attachment by given UUID list. +func GetAttachmentsByUUIDs(ctx context.Context, uuids []string) ([]*Attachment, error) { + return getAttachmentsByUUIDs(db.GetEngine(ctx), uuids) +} + +func getAttachmentsByUUIDs(e db.Engine, uuids []string) ([]*Attachment, error) { + 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) { + return getAttachmentByUUID(db.GetEngine(db.DefaultContext), uuid) +} + +// ExistAttachmentsByUUID returns true if attachment is exist by given UUID +func ExistAttachmentsByUUID(uuid string) (bool, error) { + return db.GetEngine(db.DefaultContext).Where("`uuid`=?", uuid).Exist(new(Attachment)) +} + +// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName. +func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) { + return getAttachmentByReleaseIDFileName(db.GetEngine(db.DefaultContext), releaseID, fileName) +} + +// GetAttachmentsByIssueIDCtx returns all attachments of an issue. +func GetAttachmentsByIssueIDCtx(ctx context.Context, issueID int64) ([]*Attachment, error) { + attachments := make([]*Attachment, 0, 10) + return attachments, db.GetEngine(ctx).Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) +} + +// GetAttachmentsByIssueID returns all attachments of an issue. +func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { + return GetAttachmentsByIssueIDCtx(db.DefaultContext, issueID) +} + +// GetAttachmentsByCommentID returns all attachments if comment by given ID. +func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { + return GetAttachmentsByCommentIDCtx(db.DefaultContext, commentID) +} + +// GetAttachmentsByCommentIDCtx returns all attachments if comment by given ID. +func GetAttachmentsByCommentIDCtx(ctx context.Context, commentID int64) ([]*Attachment, error) { + attachments := make([]*Attachment, 0, 10) + return attachments, db.GetEngine(ctx).Where("comment_id=?", commentID).Find(&attachments) +} + +// getAttachmentByReleaseIDFileName return a file based on the the following infos: +func getAttachmentByReleaseIDFileName(e db.Engine, releaseID int64, fileName string) (*Attachment, error) { + 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 +} + +// DeleteAttachment deletes the given attachment and optionally the associated file. +func DeleteAttachment(a *Attachment, remove bool) error { + _, err := DeleteAttachments(db.DefaultContext, []*Attachment{a}, remove) + return err +} + +// DeleteAttachments deletes the given attachments and optionally the associated files. +func DeleteAttachments(ctx context.Context, attachments []*Attachment, remove bool) (int, error) { + if len(attachments) == 0 { + return 0, nil + } + + ids := make([]int64, 0, len(attachments)) + for _, a := range attachments { + ids = append(ids, a.ID) + } + + cnt, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0]) + if err != nil { + return 0, err + } + + if remove { + for i, a := range attachments { + if err := storage.Attachments.Delete(a.RelativePath()); err != nil { + return i, err + } + } + } + return int(cnt), nil +} + +// 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 + } + + return DeleteAttachments(db.DefaultContext, attachments, remove) +} + +// 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 + } + + return DeleteAttachments(db.DefaultContext, attachments, remove) +} + +// UpdateAttachment updates the given attachment in database +func UpdateAttachment(atta *Attachment) error { + return UpdateAttachmentCtx(db.DefaultContext, atta) +} + +// UpdateAttachmentByUUID Updates attachment via uuid +func UpdateAttachmentByUUID(ctx context.Context, attach *Attachment, cols ...string) error { + if attach.UUID == "" { + return fmt.Errorf("attachment uuid should be not blank") + } + _, err := db.GetEngine(ctx).Where("uuid=?", attach.UUID).Cols(cols...).Update(attach) + return err +} + +// 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") + if atta.ID != 0 && atta.UUID == "" { + sess = sess.ID(atta.ID) + } else { + // Use uuid only if id is not set and uuid is set + sess = sess.Where("uuid = ?", atta.UUID) + } + _, err := sess.Update(atta) + return err +} + +// DeleteAttachmentsByRelease deletes all attachments associated with the given release. +func DeleteAttachmentsByRelease(releaseID int64) error { + _, err := db.GetEngine(db.DefaultContext).Where("release_id = ?", releaseID).Delete(&Attachment{}) + return err +} + +// 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 { + attachments := make([]*Attachment, 0, batchSize) + if err := db.GetEngine(db.DefaultContext).Limit(batchSize, start).Find(&attachments); err != nil { + return err + } + if len(attachments) == 0 { + return nil + } + start += len(attachments) + + for _, attach := range attachments { + if err := f(attach); err != nil { + return err + } + } + } +} + +// CountOrphanedAttachments returns the number of bad attachments +func CountOrphanedAttachments() (int64, error) { + 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`))"). + Count(new(Attachment)) +} + +// DeleteOrphanedAttachments delete all bad attachments +func DeleteOrphanedAttachments() error { + _, 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`))"). + Delete(new(Attachment)) + return err +} diff --git a/models/repo/attachment_test.go b/models/repo/attachment_test.go new file mode 100644 index 0000000000..53c28d5324 --- /dev/null +++ b/models/repo/attachment_test.go @@ -0,0 +1,104 @@ +// 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 repo + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestIncreaseDownloadCount(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11") + assert.NoError(t, err) + assert.Equal(t, int64(0), attachment.DownloadCount) + + // increase download count + err = attachment.IncreaseDownloadCount() + assert.NoError(t, err) + + attachment, err = GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11") + assert.NoError(t, err) + assert.Equal(t, int64(1), attachment.DownloadCount) +} + +func TestGetByCommentOrIssueID(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + // count of attachments from issue ID + attachments, err := GetAttachmentsByIssueID(1) + assert.NoError(t, err) + assert.Len(t, attachments, 1) + + attachments, err = GetAttachmentsByCommentID(1) + assert.NoError(t, err) + assert.Len(t, attachments, 2) +} + +func TestDeleteAttachments(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + count, err := DeleteAttachmentsByIssue(4, false) + assert.NoError(t, err) + assert.Equal(t, 2, count) + + count, err = DeleteAttachmentsByComment(2, false) + assert.NoError(t, err) + assert.Equal(t, 2, count) + + err = DeleteAttachment(&Attachment{ID: 8}, false) + assert.NoError(t, err) + + attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18") + assert.Error(t, err) + assert.True(t, IsErrAttachmentNotExist(err)) + assert.Nil(t, attachment) +} + +func TestGetAttachmentByID(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + attach, err := GetAttachmentByID(1) + assert.NoError(t, err) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID) +} + +func TestAttachment_DownloadURL(t *testing.T) { + attach := &Attachment{ + UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", + ID: 1, + } + assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL()) +} + +func TestUpdateAttachment(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + attach, err := GetAttachmentByID(1) + assert.NoError(t, err) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID) + + attach.Name = "new_name" + assert.NoError(t, UpdateAttachment(attach)) + + unittest.AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) +} + +func TestGetAttachmentsByUUIDs(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + attachList, err := GetAttachmentsByUUIDs(db.DefaultContext, []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"}) + assert.NoError(t, err) + assert.Len(t, attachList, 2) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID) + assert.Equal(t, int64(1), attachList[0].IssueID) + assert.Equal(t, int64(5), attachList[1].IssueID) +} diff --git a/models/repo/main_test.go b/models/repo/main_test.go new file mode 100644 index 0000000000..ac62df9d9c --- /dev/null +++ b/models/repo/main_test.go @@ -0,0 +1,18 @@ +// Copyright 2020 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 repo + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, filepath.Join("..", ".."), + "attachment.yml", + ) +} |