-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from zweihander/plainfiles
feat(dl): add plainfiles cache
- Loading branch information
Showing
9 changed files
with
100 additions
and
130 deletions.
There are no files selected for viewing
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,10 @@ | ||
package cache | ||
|
||
import "errors" | ||
|
||
type Cacher interface { | ||
Get(key string) ([]byte, error) | ||
Set(key string, value []byte) error | ||
} | ||
|
||
var ErrNotFound = errors.New("not found") |
This file contains 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,35 @@ | ||
package cache | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type PlainFiles struct { | ||
path string | ||
} | ||
|
||
func NewFromDirectory(path string) PlainFiles { | ||
return PlainFiles{path} | ||
} | ||
|
||
func (p PlainFiles) Get(key string) ([]byte, error) { | ||
path := filepath.Join(p.path, key) | ||
if _, err := os.Stat(path); err != nil { | ||
if os.IsNotExist(err) { | ||
return nil, ErrNotFound | ||
} | ||
} | ||
|
||
return ioutil.ReadFile(path) | ||
} | ||
|
||
func (p PlainFiles) Set(key string, value []byte) error { | ||
path := filepath.Join(p.path, key) | ||
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(path, value, 0600) | ||
} |
This file contains 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 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,33 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type ZipReadonly struct { | ||
files map[string][]byte | ||
} | ||
|
||
func NewFromZip(path string) (*ZipReadonly, error) { | ||
files, err := filesFromZip(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ZipReadonly{ | ||
files: files, | ||
}, nil | ||
} | ||
|
||
func (c *ZipReadonly) Get(key string) ([]byte, error) { | ||
data, found := c.files[key] | ||
if !found { | ||
return nil, ErrNotFound | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (c *ZipReadonly) Set(key string, value []byte) error { | ||
return fmt.Errorf("unsupported operation: set") | ||
} |
This file contains 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