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}}
-
+ {{$showArchives := and (not $.DisableDownloadSourceArchives) (not $release.IsDraft) ($.Permission.CanRead ctx.Consts.RepoUnitTypeCode)}} +
- {{ctx.Locale.Tr "repo.release.downloads"}} + {{ctx.Locale.Tr "repo.release.assets"}}{{if $showArchives}}{{Eval (len $release.Attachments) "+" 2}}{{else}}{{len $release.Attachments}}{{end}} - + {{end}} + {{if $showArchives}} + + + {{end}} + +
{{if not (or $release.IsTag $release.IsDraft)}} {{template "shared/reactions" dict diff --git a/templates/swagger/v1-openapi3.generated.json b/templates/swagger/v1-openapi3.generated.json index a9a3b4216bb..12b74e38844 100644 --- a/templates/swagger/v1-openapi3.generated.json +++ b/templates/swagger/v1-openapi3.generated.json @@ -2676,6 +2676,11 @@ "type": "string", "x-go-name": "Created" }, + "digest": { + "description": "Digest is the SHA-256 of the content as \"sha256:{hex}\", omitted when not recorded at upload", + "type": "string", + "x-go-name": "Digest" + }, "download_count": { "description": "DownloadCount is the number of times the attachment has been downloaded", "format": "int64", diff --git a/templates/swagger/v1-swagger.generated.json b/templates/swagger/v1-swagger.generated.json index 2e5d83b4acd..2237233c86e 100644 --- a/templates/swagger/v1-swagger.generated.json +++ b/templates/swagger/v1-swagger.generated.json @@ -25154,6 +25154,11 @@ "format": "date-time", "x-go-name": "Created" }, + "digest": { + "description": "Digest is the SHA-256 of the content as \"sha256:{hex}\", omitted when not recorded at upload", + "type": "string", + "x-go-name": "Digest" + }, "download_count": { "description": "DownloadCount is the number of times the attachment has been downloaded", "type": "integer", diff --git a/tests/integration/api_issue_attachment_test.go b/tests/integration/api_issue_attachment_test.go index 77d39894d99..f84414f3535 100644 --- a/tests/integration/api_issue_attachment_test.go +++ b/tests/integration/api_issue_attachment_test.go @@ -48,6 +48,7 @@ func TestAPIIssueAttachment(t *testing.T) { apiAttachment := DecodeJSON(t, resp, &api.Attachment{}) unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID}) + assert.Empty(t, apiAttachment.Digest) commentAttachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 3, RepoID: repo.ID}) req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets/%d", repoOwner.Name, repo.Name, unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: commentAttachment.IssueID}).Index, commentAttachment.ID)). diff --git a/tests/integration/api_releases_test.go b/tests/integration/api_releases_test.go index d3a1f75b9f4..6f71f09b04d 100644 --- a/tests/integration/api_releases_test.go +++ b/tests/integration/api_releases_test.go @@ -20,6 +20,7 @@ import ( unit_model "gitea.dev/models/unit" "gitea.dev/models/unittest" user_model "gitea.dev/models/user" + "gitea.dev/modules/base" "gitea.dev/modules/git" "gitea.dev/modules/setting" api "gitea.dev/modules/structs" @@ -424,6 +425,7 @@ func TestAPIUploadAssetRelease(t *testing.T) { attachment := DecodeJSON(t, resp, &api.Attachment{}) assert.Equal(t, filename, attachment.Name) assert.EqualValues(t, len(bufImageBytes), attachment.Size) + assert.Equal(t, "sha256:"+base.EncodeSha256(string(bufImageBytes)), attachment.Digest) }) t.Run("UploadWithName", func(t *testing.T) { resp := performUpload(t, assetURL+"?name=test-asset", bufImageBytes, http.StatusCreated) diff --git a/web_src/css/repo/release-tag.css b/web_src/css/repo/release-tag.css index 85c368f54a0..b25cd6d174b 100644 --- a/web_src/css/repo/release-tag.css +++ b/web_src/css/repo/release-tag.css @@ -59,36 +59,33 @@ margin-bottom: 2px; /* the legacy trick to align the avatar vertically, no better solution at the moment */ } -#release-list .release-entry .attachment-list { - border: 1px solid var(--color-secondary); - border-radius: var(--border-radius); +#release-list .release-asset { + display: grid; + grid-template-columns: minmax(40%, 1fr) minmax(0, 250px) minmax(60px, auto) minmax(75px, auto) minmax(100px, auto); /* fixed minimums keep columns aligned across rows */ + place-items: center end; + gap: 16px; + white-space: nowrap; + color: var(--color-text-light-2); } -#release-list .release-entry .attachment-list > .item { - display: flex; - padding: 8px; - flex-wrap: wrap; +#release-list .release-asset > a, +#release-list .release-asset-digest { + justify-self: stretch; } -#release-list .release-entry .attachment-list > .item a { - min-width: 300px; +#release-list .release-asset-digest { + font-family: var(--fonts-monospace); + font-size: 12px; } -#release-list .release-entry .attachment-list .attachment-right-info { - flex-shrink: 0; - min-width: 300px; -} - -#release-list .release-entry .detail .download[open] summary { - margin-bottom: 10px; -} - -#release-list .download-icon { - color: var(--color-text-light-1); -} - -#release-list .release-entry .detail .download .list li:last-child { - border-bottom: none; +@media (max-width: 1023.98px) { + #release-list .release-asset { + grid-template-columns: minmax(0, 1fr) repeat(3, auto); + gap: 4px 12px; + } + #release-list .release-asset > a { + grid-column: 1 / -1; + } } #tags-table .tag-list-row-title {