diff --git a/modelmigration/migrations.go b/modelmigration/migrations.go index 33704320b79..6b1d0c34b47 100644 --- a/modelmigration/migrations.go +++ b/modelmigration/migrations.go @@ -447,6 +447,7 @@ func prepareMigrationTasks() []*migration { newMigration(368, "Add unique index for team name", v29.AddUniqueTeamNameIndex), newMigration(369, "Add runner_name to action_task", v29.AddRunnerNameToActionTask), newMigration(370, "Change raw_concurrency of action_run and action_run_job to TEXT on MSSQL", v29.ChangeActionsRawConcurrencyToText), + newMigration(371, "Add hash_sha256 column to attachment", v29.AddHashSHA256ToAttachment), } return preparedMigrations } diff --git a/modelmigration/v29/v371.go b/modelmigration/v29/v371.go new file mode 100644 index 00000000000..05471f152a5 --- /dev/null +++ b/modelmigration/v29/v371.go @@ -0,0 +1,23 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "context" + + "gitea.dev/modelmigration/base" + + "xorm.io/xorm" +) + +func AddHashSHA256ToAttachment(_ctx context.Context, x base.EngineMigration) error { + type Attachment struct { + HashSHA256 string `xorm:"hash_sha256 VARCHAR(64) NOT NULL DEFAULT ''"` + } + _, err := x.SyncWithOptions(xorm.SyncOptions{ + IgnoreConstrains: true, + IgnoreDropIndices: true, + }, new(Attachment)) + return err +} diff --git a/models/repo/attachment.go b/models/repo/attachment.go index c16e9c98866..c03a9f92a4c 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -34,6 +34,7 @@ type Attachment struct { Name string DownloadCount int64 `xorm:"DEFAULT 0"` Size int64 `xorm:"DEFAULT 0"` + HashSHA256 string `xorm:"hash_sha256 VARCHAR(64) NOT NULL DEFAULT ''"` // empty for attachments uploaded before it was recorded CreatedUnix timeutil.TimeStamp `xorm:"created"` CustomDownloadURL string `xorm:"-"` } diff --git a/modules/structs/attachment.go b/modules/structs/attachment.go index b2ace3e6cfa..76004d25bee 100644 --- a/modules/structs/attachment.go +++ b/modules/structs/attachment.go @@ -16,6 +16,8 @@ type Attachment struct { Name string `json:"name"` // Size is the file size in bytes Size int64 `json:"size"` + // Digest is the SHA-256 of the content as "sha256:{hex}", omitted when not recorded at upload + Digest string `json:"digest,omitempty"` // DownloadCount is the number of times the attachment has been downloaded DownloadCount int64 `json:"download_count"` // swagger:strfmt date-time diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index d2b01bb1418..71fbbeddaae 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -2727,7 +2727,7 @@ "repo.release.tag_name_invalid": "The tag name is not valid.", "repo.release.tag_name_protected": "The tag name is protected.", "repo.release.tag_already_exist": "This tag name already exists.", - "repo.release.downloads": "Downloads", + "repo.release.assets": "Assets", "repo.release.download_count": "Downloads: %s", "repo.release.add_tag_msg": "Use the title and content of release as tag message.", "repo.release.add_tag": "Create Tag Only", diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index 61b0d7d4435..f04110a2875 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -6,6 +6,8 @@ package attachment import ( "bytes" "context" + "crypto/sha256" + "encoding/hex" "errors" "fmt" "io" @@ -28,11 +30,13 @@ func NewAttachment(ctx context.Context, attach *repo_model.Attachment, file io.R err := db.WithTx(ctx, func(ctx context.Context) error { attach.UUID = uuid.New().String() - size, err := storage.Attachments.Save(attach.RelativePath(), file, size) + hasher := sha256.New() + size, err := storage.Attachments.Save(attach.RelativePath(), io.TeeReader(file, hasher), size) if err != nil { return fmt.Errorf("Attachments.Save: %w", err) } attach.Size = size + attach.HashSHA256 = hex.EncodeToString(hasher.Sum(nil)) return db.Insert(ctx, attach) }) diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index 7df200769b9..f41e2dfb8d0 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -4,13 +4,13 @@ package attachment import ( - "os" - "path/filepath" + "strings" "testing" repo_model "gitea.dev/models/repo" "gitea.dev/models/unittest" user_model "gitea.dev/models/user" + "gitea.dev/modules/base" _ "gitea.dev/models/actions" @@ -26,20 +26,17 @@ func TestUploadAttachment(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) - fPath := "./attachment_test.go" - f, err := os.Open(fPath) - assert.NoError(t, err) - defer f.Close() - + content := "attachment content" attach, err := NewAttachment(t.Context(), &repo_model.Attachment{ RepoID: 1, UploaderID: user.ID, - Name: filepath.Base(fPath), - }, f, -1) + Name: "attachment.txt", + }, strings.NewReader(content), -1) assert.NoError(t, err) attachment, err := repo_model.GetAttachmentByUUID(t.Context(), attach.UUID) assert.NoError(t, err) assert.Equal(t, user.ID, attachment.UploaderID) assert.Equal(t, int64(0), attachment.DownloadCount) + assert.Equal(t, base.EncodeSha256(content), attachment.HashSHA256) } diff --git a/services/convert/attachment.go b/services/convert/attachment.go index a6f46e1823e..a9b3900fe9d 100644 --- a/services/convert/attachment.go +++ b/services/convert/attachment.go @@ -8,6 +8,7 @@ import ( repo_model "gitea.dev/models/repo" api "gitea.dev/modules/structs" + "gitea.dev/modules/util" ) func WebAssetDownloadURL(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string { @@ -36,6 +37,7 @@ func toAttachment(ctx context.Context, repo *repo_model.Repository, a *repo_mode Created: a.CreatedUnix.AsTime(), DownloadCount: a.DownloadCount, Size: a.Size, + Digest: util.Iif(a.HashSHA256 != "", "sha256:"+a.HashSHA256, ""), UUID: a.UUID, DownloadURL: getDownloadURL(ctx, repo, a), // for web/api requests, return different download URLs } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index c70f393c2b3..17b35a4b80d 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -7,6 +7,8 @@ package migrations import ( "cmp" "context" + "crypto/sha256" + "encoding/hex" "errors" "fmt" "io" @@ -356,11 +358,13 @@ func (g *GiteaLocalUploader) CreateReleases(ctx context.Context, releases ...*ba continue } if rc != nil { - attach.Size, err = storage.Attachments.Save(attach.RelativePath(), rc, util.Iif(*asset.Size > 0, int64(*asset.Size), -1)) // GitLab links and Forgejo external links report no size + hasher := sha256.New() + attach.Size, err = storage.Attachments.Save(attach.RelativePath(), io.TeeReader(rc, hasher), util.Iif(*asset.Size > 0, int64(*asset.Size), -1)) // GitLab links and Forgejo external links report no size rc.Close() if err != nil { return fmt.Errorf("release %s asset %s: %w", release.TagName, asset.Name, err) } + attach.HashSHA256 = hex.EncodeToString(hasher.Sum(nil)) } rel.Attachments = append(rel.Attachments, &attach) diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index 3c0d2bd676d..85369ed0a74 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -73,39 +73,43 @@ {{$release.RenderedNote}}
-