Skip to content

Commit

Permalink
Add Filenames hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Sep 9, 2018
1 parent 5fe8c70 commit 41c0c69
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
3 changes: 3 additions & 0 deletions file-server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type Options struct {
// Filesystem is optional and is used for opening files. In case of
// nil, os.Open will be used to open files.
Filesystem http.FileSystem
// Filenames are used to find corresponding files with hashes in them
// by just iterating through them instead doing filesystem operations.
Filenames []string

// NotFoundHandler is used when no file can be found.
NotFoundHandler http.Handler
Expand Down
46 changes: 32 additions & 14 deletions file-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,46 @@ func (s *Server) hashFromFilename(p string) (h string, cont bool, err error) {
return
}

pattern := ""
ext := filepath.Ext(p)
if ext != "" {
pattern = strings.TrimSuffix(p, ext) + ".*" + ext
fn := strings.TrimSuffix(p, ext)
var matches []string

if s.Filenames != nil {
prefix := filepath.Join(s.dir, fn)
altPrefix := filepath.Join(s.AltDir, fn)
for _, filename := range s.Filenames {
if s.AltDir != "" {
if strings.HasPrefix(filename, altPrefix) {
matches = append(matches, filename)
}
}
if strings.HasPrefix(filename, prefix) {
matches = append(matches, filename)
}
}
} else {
pattern = p + ".*"
}
pattern := ""
if ext != "" {
pattern = fn + ".*" + ext
} else {
pattern = p + ".*"
}

var matches []string
if s.AltDir != "" {
matches, err = filepath.Glob(filepath.Join(s.AltDir, pattern))
if s.AltDir != "" {
matches, err = filepath.Glob(filepath.Join(s.AltDir, pattern))
if err != nil {
cont = true
return
}
}
var m []string
m, err = filepath.Glob(filepath.Join(s.dir, pattern))
if err != nil {
cont = true
return
}
matches = append(matches, m...)
}
m, err := filepath.Glob(filepath.Join(s.dir, pattern))
if err != nil {
cont = true
return
}
matches = append(matches, m...)

for _, match := range matches {
if strings.HasSuffix(s.canonicalPath(match), p) {
Expand Down
66 changes: 66 additions & 0 deletions file-server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,69 @@ func TestServerHashedPathFromFilenameWithAltDir(t *testing.T) {
t.Errorf("expected hashed path %q, got %q", expected, got)
}
}

func TestServerHashedPathFromFilenameWithFilenames(t *testing.T) {
dir, err := ioutil.TempDir("", "file-server-test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

content := "gopher"
f, err := os.Create(filepath.Join(dir, "data.12345678.txt"))
_, err = f.WriteString(content)
if err != nil {
t.Error(err)
}
f.Close()

h := New("/assets", dir, &Options{
Hasher: MD5Hasher{8},
Filenames: []string{filepath.Join(dir, "data.abcdef01.txt")},
})

got, err := h.HashedPath("data.txt")
if err != nil {
t.Error(err)
}
expected := "/assets/data.abcdef01.txt"
if got != expected {
t.Errorf("expected hashed path %q, got %q", expected, got)
}
}

func TestServerHashedPathFromFilenameWithAltDirWithFilenames(t *testing.T) {
dir, err := ioutil.TempDir("", "file-server-test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

altDir, err := ioutil.TempDir("", "file-server-test-alt")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

content := "file alt content"

err = ioutil.WriteFile(filepath.Join(altDir, "data.12345678.txt"), []byte(content), 0666)
if err != nil {
t.Error(err)
}

h := New("/assets", dir, &Options{
Hasher: MD5Hasher{8},
AltDir: altDir,
Filenames: []string{filepath.Join(altDir, "data.abcdef01.txt")},
})

got, err := h.HashedPath("data.txt")
if err != nil {
t.Error(err)
}
expected := "/assets/data.abcdef01.txt"
if got != expected {
t.Errorf("expected hashed path %q, got %q", expected, got)
}
}

0 comments on commit 41c0c69

Please sign in to comment.