Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewApi(ready chan<- bool) *Api {
Thumbnails: thumbCache,
Tiers: collections.NewSyncStrSet(),
Etags: etags,
Router: mux.NewRouter().StrictSlash(true),
Router: mux.NewRouter().StrictSlash(true).SkipClean(true),
}
go api.initCacheLoader(ready)
api.initCacheManager()
Expand Down
3 changes: 2 additions & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (api *Api) serveThumbs() http.HandlerFunc {
vars["resizeOp"],
vars["options"])
path := vars["path"]
thumbPath := resizeTier + "/" + path
thumbPath, _ := api.Originals.RemoteToLocalPath(path)
thumbPath = resizeTier + "/" + thumbPath
api.Tiers.Add(resizeTier)
thumbBuf, _ := api.Thumbnails.Get(thumbPath)
if thumbBuf == nil {
Expand Down
45 changes: 43 additions & 2 deletions store/twotier.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package store

import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
)

type TwoTier struct {
Store Store
Cache Cache
}

func (s *TwoTier) Get(filename string) ([]byte, error) {
func (s *TwoTier) Get(aurl string) ([]byte, error) {
var buf []byte
var err error
filename, isRemote := s.RemoteToLocalPath(aurl)

if s.Cache != nil {
buf, _ = s.Cache.Get(filename)
}
if buf == nil {
buf, err = s.Store.Get(filename)
if err != nil {
return nil, err
if !isRemote {
return nil, err
} else {
response, err := http.Get(aurl)
if err != nil {
return nil, err
}
if response.StatusCode >= 400 {
return nil, errors.New("(" + response.Request.URL.String() + ") HTTP Error: " + response.Status)
}
defer response.Body.Close()
buf, err = ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
err = s.Store.Put(filename, buf)
}
}
if s.Cache != nil {
go s.Cache.Put(filename, buf)
Expand Down Expand Up @@ -54,3 +80,18 @@ func (s *TwoTier) LoadCache(walkFn func(item interface{}) error) error {
}
return s.Cache.LoadCache(walkFn)
}

func (s *TwoTier) RemoteToLocalPath(path string) (string, bool) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s *TwoTier) RemoteToLocalPath(path string) (string, bool) {
func (s *TwoTier) remoteToLocalPath(path string) (string, bool) {

Unexport this function.

var localPath string
var isRemote bool

aurl, _ := url.Parse(path)
if aurl.Scheme != "" {
isRemote = true
localPath = filepath.Join(aurl.Hostname(), aurl.EscapedPath())
} else {
isRemote = false
localPath = path
}
return localPath, isRemote
}