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 /services | |
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 'services')
-rw-r--r-- | services/attachment/attachment.go | 8 | ||||
-rw-r--r-- | services/attachment/attachment_test.go | 5 | ||||
-rw-r--r-- | services/migrations/gitea_uploader.go | 3 | ||||
-rw-r--r-- | services/release/release.go | 13 | ||||
-rw-r--r-- | services/release/release_test.go | 5 |
5 files changed, 19 insertions, 15 deletions
diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index f747ccec3e..e3b65a239b 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -10,8 +10,8 @@ import ( "fmt" "io" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/util" @@ -20,7 +20,7 @@ import ( ) // NewAttachment creates a new attachment object, but do not verify. -func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachment, error) { +func NewAttachment(attach *repo_model.Attachment, file io.Reader) (*repo_model.Attachment, error) { if attach.RepoID == 0 { return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name) } @@ -40,7 +40,7 @@ func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachmen } // UploadAttachment upload new attachment into storage and update database -func UploadAttachment(file io.Reader, actorID, repoID, releaseID int64, fileName string, allowedTypes string) (*models.Attachment, error) { +func UploadAttachment(file io.Reader, actorID, repoID, releaseID int64, fileName string, allowedTypes string) (*repo_model.Attachment, error) { buf := make([]byte, 1024) n, _ := util.ReadAtMost(file, buf) buf = buf[:n] @@ -49,7 +49,7 @@ func UploadAttachment(file io.Reader, actorID, repoID, releaseID int64, fileName return nil, err } - return NewAttachment(&models.Attachment{ + return NewAttachment(&repo_model.Attachment{ RepoID: repoID, UploaderID: actorID, ReleaseID: releaseID, diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index 5bb5db11ec..a992fbf151 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -10,6 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -29,14 +30,14 @@ func TestUploadAttachment(t *testing.T) { assert.NoError(t, err) defer f.Close() - attach, err := NewAttachment(&models.Attachment{ + attach, err := NewAttachment(&repo_model.Attachment{ RepoID: 1, UploaderID: user.ID, Name: filepath.Base(fPath), }, f) assert.NoError(t, err) - attachment, err := models.GetAttachmentByUUID(attach.UUID) + attachment, err := repo_model.GetAttachmentByUUID(attach.UUID) assert.NoError(t, err) assert.EqualValues(t, user.ID, attachment.UploaderID) assert.Equal(t, int64(0), attachment.DownloadCount) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 0f215e05d6..2394c63d76 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" base "code.gitea.io/gitea/modules/migration" @@ -295,7 +296,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { asset.Created = release.Created } } - var attach = models.Attachment{ + var attach = repo_model.Attachment{ UUID: gouuid.New().String(), Name: asset.Name, DownloadCount: int64(*asset.DownloadCount), diff --git a/services/release/release.go b/services/release/release.go index f6f456e8fa..30274f93ef 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" @@ -206,7 +207,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea var deletedUUIDsMap = make(map[string]bool) if len(delAttachmentUUIDs) > 0 { // Check attachments - attachments, err := models.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs) + attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs) if err != nil { return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", delAttachmentUUIDs, err) } @@ -217,7 +218,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea deletedUUIDsMap[attach.UUID] = true } - if _, err := models.DeleteAttachments(ctx, attachments, false); err != nil { + if _, err := repo_model.DeleteAttachments(ctx, attachments, false); err != nil { return fmt.Errorf("DeleteAttachments [uuids: %v]: %v", delAttachmentUUIDs, err) } } @@ -228,7 +229,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea updateAttachmentsList = append(updateAttachmentsList, k) } // Check attachments - attachments, err := models.GetAttachmentsByUUIDs(ctx, updateAttachmentsList) + attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, updateAttachmentsList) if err != nil { return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", updateAttachmentsList, err) } @@ -240,7 +241,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea for uuid, newName := range editAttachments { if !deletedUUIDsMap[uuid] { - if err = models.UpdateAttachmentByUUID(ctx, &models.Attachment{ + if err = repo_model.UpdateAttachmentByUUID(ctx, &repo_model.Attachment{ UUID: uuid, Name: newName, }, "name"); err != nil { @@ -255,7 +256,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea } for _, uuid := range delAttachmentUUIDs { - if err := storage.Attachments.Delete(models.AttachmentRelativePath(uuid)); err != nil { + if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil { // Even delete files failed, but the attachments has been removed from database, so we // should not return error but only record the error on logs. // users have to delete this attachments manually or we should have a @@ -321,7 +322,7 @@ func DeleteReleaseByID(id int64, doer *models.User, delTag bool) error { return fmt.Errorf("LoadAttributes: %v", err) } - if err := models.DeleteAttachmentsByRelease(rel.ID); err != nil { + if err := repo_model.DeleteAttachmentsByRelease(rel.ID); err != nil { return fmt.Errorf("DeleteAttachments: %v", err) } diff --git a/services/release/release_test.go b/services/release/release_test.go index d720bf996b..92eb128f71 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/services/attachment" @@ -103,7 +104,7 @@ func TestRelease_Create(t *testing.T) { IsTag: false, }, nil, "")) - attach, err := attachment.NewAttachment(&models.Attachment{ + attach, err := attachment.NewAttachment(&repo_model.Attachment{ RepoID: repo.ID, UploaderID: user.ID, Name: "test.txt", @@ -236,7 +237,7 @@ func TestRelease_Update(t *testing.T) { assert.Equal(t, tagName, release.TagName) // Add new attachments - attach, err := attachment.NewAttachment(&models.Attachment{ + attach, err := attachment.NewAttachment(&repo_model.Attachment{ RepoID: repo.ID, UploaderID: user.ID, Name: "test.txt", |