-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat(media): collapse singleton media containers #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d36bd99
feat(media): collapse singleton media containers
wizzomafizzo bc2d763
fix(media): satisfy lint for singleton aliases
wizzomafizzo d4eb3b0
fix(media): gate singleton aliases by platform
wizzomafizzo b4ff54f
fix(media): address singleton alias review
wizzomafizzo 8632a5b
test(media): cover singleton alias metadata
wizzomafizzo 0d89680
test(media): satisfy alias coverage lint
wizzomafizzo ecc1768
test(media): cover singleton alias lookup paths
wizzomafizzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Zaparoo Core | ||
| // Copyright (c) 2026 The Zaparoo Project Contributors. | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // | ||
| // This file is part of Zaparoo Core. | ||
| // | ||
| // Zaparoo Core is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Zaparoo Core is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Zaparoo Core. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package methods | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/api/models/requests" | ||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/database" | ||
| ) | ||
|
|
||
| func singletonMediaAliasesEnabled(env *requests.RequestEnv) bool { | ||
| return env != nil && env.Platform != nil && env.Platform.Settings().ZipsAsDirs | ||
| } | ||
|
|
||
| func resolveSingletonMediaPath( | ||
| env *requests.RequestEnv, | ||
| system database.System, | ||
| mediaPath string, | ||
| ) (*database.Media, error) { | ||
| if !singletonMediaAliasesEnabled(env) { | ||
| return nil, nil //nolint:nilnil // disabled aliasing has no singleton fallback | ||
| } | ||
|
|
||
| media, err := env.Database.MediaDB.FindSingleDescendantMedia(env.Context, system.DBID, mediaPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to resolve singleton media path: %w", err) | ||
| } | ||
| return media, nil | ||
| } | ||
|
|
||
| func equivalentMediaIDs(env *requests.RequestEnv, row *database.MediaFullRow) ([]int64, error) { | ||
| if row == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| ids := []int64{row.DBID} | ||
| if env == nil || env.Database == nil || env.Database.MediaDB == nil || !singletonMediaAliasesEnabled(env) { | ||
| return ids, nil | ||
| } | ||
| seen := map[int64]bool{row.DBID: true} | ||
| add := func(media *database.Media) { | ||
| if media == nil || seen[media.DBID] { | ||
| return | ||
| } | ||
| seen[media.DBID] = true | ||
| ids = append(ids, media.DBID) | ||
| } | ||
|
|
||
| child, err := env.Database.MediaDB.FindSingleDescendantMedia(env.Context, row.System.DBID, row.Path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to find child alias media: %w", err) | ||
| } | ||
| add(child) | ||
|
|
||
| parentPath := strings.TrimSuffix(row.ParentDir, "/") | ||
| if parentPath == "" || parentPath == row.Path { | ||
| return ids, nil | ||
| } | ||
|
|
||
| onlyChild, err := env.Database.MediaDB.FindSingleDescendantMedia(env.Context, row.System.DBID, parentPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to verify parent alias media: %w", err) | ||
| } | ||
| if onlyChild == nil || onlyChild.DBID != row.DBID { | ||
| return ids, nil | ||
| } | ||
|
|
||
| parent, err := env.Database.MediaDB.FindMediaBySystemAndPath(env.Context, row.System.DBID, parentPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to find parent alias media: %w", err) | ||
| } | ||
| add(parent) | ||
|
|
||
| return ids, nil | ||
| } | ||
|
|
||
| func mergeMediaTags(primary []database.TagInfo, aliases ...[]database.TagInfo) []database.TagInfo { | ||
| if len(aliases) == 0 { | ||
| return primary | ||
| } | ||
| merged := make([]database.TagInfo, 0, len(primary)) | ||
| seen := make(map[string]bool) | ||
| appendUnique := func(tags []database.TagInfo) { | ||
| for _, tag := range tags { | ||
| key := tag.Type + "\x00" + tag.Tag | ||
| if seen[key] { | ||
| continue | ||
| } | ||
| seen[key] = true | ||
| merged = append(merged, tag) | ||
| } | ||
| } | ||
| appendUnique(primary) | ||
| for _, tags := range aliases { | ||
| appendUnique(tags) | ||
| } | ||
| return merged | ||
| } | ||
|
|
||
| func mergeMediaProperties( | ||
| primary []database.MediaProperty, | ||
| aliases ...[]database.MediaProperty, | ||
| ) []database.MediaProperty { | ||
| if len(aliases) == 0 { | ||
| return primary | ||
| } | ||
| merged := make([]database.MediaProperty, 0, len(primary)) | ||
| seen := make(map[string]bool) | ||
| appendUnique := func(props []database.MediaProperty) { | ||
| for _, prop := range props { | ||
| if seen[prop.TypeTag] { | ||
| continue | ||
| } | ||
| seen[prop.TypeTag] = true | ||
| merged = append(merged, prop) | ||
| } | ||
| } | ||
| appendUnique(primary) | ||
| for _, props := range aliases { | ||
| appendUnique(props) | ||
| } | ||
| return merged | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Zaparoo Core | ||
| // Copyright (c) 2026 The Zaparoo Project Contributors. | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // | ||
| // This file is part of Zaparoo Core. | ||
| // | ||
| // Zaparoo Core is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Zaparoo Core is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Zaparoo Core. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package methods | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/api/models/requests" | ||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/database" | ||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/platforms" | ||
| testhelpers "github.com/ZaparooProject/zaparoo-core/v2/pkg/testing/helpers" | ||
| "github.com/ZaparooProject/zaparoo-core/v2/pkg/testing/mocks" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEquivalentMediaIDsNilAndDisabledGuards(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| row := &database.MediaFullRow{Media: database.Media{DBID: 10}} | ||
|
|
||
| ids, err := equivalentMediaIDs(nil, nil) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, ids) | ||
|
|
||
| ids, err = equivalentMediaIDs(nil, row) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []int64{10}, ids) | ||
|
|
||
| ids, err = equivalentMediaIDs(&requests.RequestEnv{}, row) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []int64{10}, ids) | ||
|
|
||
| ids, err = equivalentMediaIDs(&requests.RequestEnv{Database: &database.Database{}}, row) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []int64{10}, ids) | ||
| } | ||
|
|
||
| func TestEquivalentMediaIDsParentChildAliases(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockDB := testhelpers.NewMockMediaDBI() | ||
| platform := mocks.NewMockPlatform() | ||
| platform.On("Settings").Return(platforms.Settings{ZipsAsDirs: true}) | ||
|
|
||
| row := &database.MediaFullRow{ | ||
| Media: database.Media{ | ||
| DBID: 20, | ||
| Path: "roms/Game.zip/Game.nes", | ||
| ParentDir: "roms/Game.zip/", | ||
| }, | ||
| System: database.System{DBID: 1, SystemID: "NES"}, | ||
| } | ||
| parent := &database.Media{DBID: 10, Path: "roms/Game.zip"} | ||
|
|
||
| mockDB.On("FindSingleDescendantMedia", mock.Anything, int64(1), row.Path). | ||
| Return((*database.Media)(nil), nil) | ||
| mockDB.On("FindSingleDescendantMedia", mock.Anything, int64(1), "roms/Game.zip"). | ||
| Return(&database.Media{DBID: 20, Path: row.Path}, nil) | ||
| mockDB.On("FindMediaBySystemAndPath", mock.Anything, int64(1), "roms/Game.zip").Return(parent, nil) | ||
|
|
||
| env := &requests.RequestEnv{ | ||
| Context: context.Background(), | ||
| Database: &database.Database{MediaDB: mockDB}, | ||
| Platform: platform, | ||
| } | ||
| ids, err := equivalentMediaIDs(env, row) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []int64{20, 10}, ids) | ||
| mockDB.AssertExpectations(t) | ||
| platform.AssertExpectations(t) | ||
| } | ||
|
|
||
| func TestEquivalentMediaIDsSkipsEmptyOrSelfParent(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockDB := testhelpers.NewMockMediaDBI() | ||
| platform := mocks.NewMockPlatform() | ||
| platform.On("Settings").Return(platforms.Settings{ZipsAsDirs: true}).Twice() | ||
| env := &requests.RequestEnv{ | ||
| Context: context.Background(), | ||
| Database: &database.Database{MediaDB: mockDB}, | ||
| Platform: platform, | ||
| } | ||
|
|
||
| for _, row := range []*database.MediaFullRow{ | ||
| { | ||
| Media: database.Media{DBID: 20, Path: "roms/Game.nes"}, | ||
| System: database.System{DBID: 1}, | ||
| }, | ||
| { | ||
| Media: database.Media{DBID: 21, Path: "roms/Game.zip", ParentDir: "roms/Game.zip/"}, | ||
| System: database.System{DBID: 1}, | ||
| }, | ||
| } { | ||
| mockDB.On("FindSingleDescendantMedia", mock.Anything, row.System.DBID, row.Path). | ||
| Return((*database.Media)(nil), nil).Once() | ||
| ids, err := equivalentMediaIDs(env, row) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []int64{row.DBID}, ids) | ||
| } | ||
|
|
||
| mockDB.AssertExpectations(t) | ||
| platform.AssertExpectations(t) | ||
| } | ||
|
|
||
| func TestMergeMediaTagsDedupesAndPreservesPrimary(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| primary := []database.TagInfo{ | ||
| {Type: "region", Tag: "us"}, | ||
| {Type: "lang", Tag: "en"}, | ||
| } | ||
| alias := []database.TagInfo{ | ||
| {Type: "region", Tag: "us"}, | ||
| {Type: "region", Tag: "jp"}, | ||
| } | ||
|
|
||
| assert.Equal(t, []database.TagInfo{ | ||
| {Type: "region", Tag: "us"}, | ||
| {Type: "lang", Tag: "en"}, | ||
| {Type: "region", Tag: "jp"}, | ||
| }, mergeMediaTags(primary, alias)) | ||
| } | ||
|
|
||
| func TestMergeMediaPropertiesDedupesAndPreservesPrimary(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| primary := []database.MediaProperty{ | ||
| {TypeTag: "property:image-boxart", Text: "primary.png"}, | ||
| {TypeTag: "property:description", Text: "primary desc"}, | ||
| } | ||
| alias := []database.MediaProperty{ | ||
| {TypeTag: "property:image-boxart", Text: "alias.png"}, | ||
| {TypeTag: "property:image-screenshot", Text: "shot.png"}, | ||
| } | ||
|
|
||
| assert.Equal(t, []database.MediaProperty{ | ||
| {TypeTag: "property:image-boxart", Text: "primary.png"}, | ||
| {TypeTag: "property:description", Text: "primary desc"}, | ||
| {TypeTag: "property:image-screenshot", Text: "shot.png"}, | ||
| }, mergeMediaProperties(primary, alias)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ZaparooProject/zaparoo-core
Length of output: 1022
Use filepath.Join for test path literals in pkg/api/methods/media_alias_test.go
Replace the hardcoded slash-delimited strings (e.g.,
"roms/Game.zip/Game.nes","roms/Game.zip/","roms/Game.zip") at lines 68-69, 73, 77-79, 107, and 111 withfilepath.Join-constructed paths, keepingParentDir’s trailing-separator semantics consistent with the existing expectations.🤖 Prompt for AI Agents