mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 04:27:37 +03:00
Refactor merge.FS
This commit is contained in:
parent
56809419c2
commit
955a9b43af
3 changed files with 11 additions and 11 deletions
|
@ -8,7 +8,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
"github.com/navidrome/navidrome/utils/merge"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -20,7 +20,7 @@ var (
|
|||
|
||||
func FS() fs.FS {
|
||||
fsOnce.Do(func() {
|
||||
fsys = utils.MergeFS{
|
||||
fsys = merge.FS{
|
||||
Base: embedFS,
|
||||
Overlay: os.DirFS(path.Join(conf.Server.DataFolder, "resources")),
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package utils
|
||||
package merge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -7,15 +7,15 @@ import (
|
|||
"sort"
|
||||
)
|
||||
|
||||
// MergeFS implements a simple merged fs.FS, that can combine a Base FS with an Overlay FS. The semantics are:
|
||||
// FS implements a simple merged fs.FS, that can combine a Base FS with an Overlay FS. The semantics are:
|
||||
// - Files from the Overlay FS will override files with the same name in the Base FS
|
||||
// - Directories are combined, with priority for the Overlay FS over the Base FS for files with matching names
|
||||
type MergeFS struct {
|
||||
type FS struct {
|
||||
Base fs.FS
|
||||
Overlay fs.FS
|
||||
}
|
||||
|
||||
func (m MergeFS) Open(name string) (fs.File, error) {
|
||||
func (m FS) Open(name string) (fs.File, error) {
|
||||
file, err := m.Overlay.Open(name)
|
||||
if err != nil {
|
||||
return m.Base.Open(name)
|
||||
|
@ -43,7 +43,7 @@ func (m MergeFS) Open(name string) (fs.File, error) {
|
|||
return m.mergeDirs(name, info, baseDirFile, overlayDirFile)
|
||||
}
|
||||
|
||||
func (m MergeFS) mergeDirs(name string, info fs.FileInfo, baseDir fs.ReadDirFile, overlayDir fs.ReadDirFile) (fs.File, error) {
|
||||
func (m FS) mergeDirs(name string, info fs.FileInfo, baseDir fs.ReadDirFile, overlayDir fs.ReadDirFile) (fs.File, error) {
|
||||
merged := map[string]fs.DirEntry{}
|
||||
|
||||
baseFiles, err := baseDir.ReadDir(-1)
|
|
@ -1,4 +1,4 @@
|
|||
package utils_test
|
||||
package merge_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
@ -6,12 +6,12 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
"github.com/navidrome/navidrome/utils/merge"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("MergeFS", func() {
|
||||
var _ = Describe("FS", func() {
|
||||
var baseName, overlayName string
|
||||
var mergedDir fs.FS
|
||||
|
||||
|
@ -20,7 +20,7 @@ var _ = Describe("MergeFS", func() {
|
|||
overlayName, _ = os.MkdirTemp("", "merge_fs_overlay_test")
|
||||
baseDir := os.DirFS(baseName)
|
||||
overlayDir := os.DirFS(overlayName)
|
||||
mergedDir = utils.MergeFS{Base: baseDir, Overlay: overlayDir}
|
||||
mergedDir = merge.FS{Base: baseDir, Overlay: overlayDir}
|
||||
})
|
||||
AfterEach(func() {
|
||||
_ = os.RemoveAll(baseName)
|
Loading…
Add table
Add a link
Reference in a new issue