summaryrefslogtreecommitdiff
path: root/local.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2021-02-05 17:15:51 +0000
committerNick White <git@njw.name>2021-02-05 17:15:51 +0000
commit11470933e4fd379b4aefa4e2bab33662a72791c2 (patch)
tree8607e7739989ff63032b9ce10a8bf8553ecc6eb6 /local.go
parent3e7da751b3ca917adb79674eac4ef2a3267e3984 (diff)
parenta8c7481f0dc02bbda3b3a07091e9d61f6eb728b2 (diff)
Merge branch 'master' of ssh://ssh.phx.nearlyfreespeech.net/home/public/bookpipeline
Diffstat (limited to 'local.go')
-rw-r--r--local.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/local.go b/local.go
index ebc3611..31e44a9 100644
--- a/local.go
+++ b/local.go
@@ -27,7 +27,7 @@ const storageId = "storage"
type LocalConn struct {
// these should be set before running Init(), or left to defaults
TempDir string
- Logger *log.Logger
+ Logger *log.Logger
}
// MinimalInit does the bare minimum initialisation
@@ -36,7 +36,7 @@ func (a *LocalConn) MinimalInit() error {
if a.TempDir == "" {
a.TempDir = filepath.Join(os.TempDir(), "bookpipeline")
}
- err = os.Mkdir(a.TempDir, 0700)
+ err = os.MkdirAll(a.TempDir, 0700)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("Error creating temporary directory: %v", err)
}
@@ -134,6 +134,7 @@ func prefixwalker(dirpath string, prefix string, list *[]ObjMeta) filepath.WalkF
}
n := strings.TrimPrefix(path, dirpath)
n = strings.TrimPrefix(n, "/")
+ n = strings.TrimPrefix(n, "\\")
o := ObjMeta{Name: n, Date: info.ModTime()}
*list = append(*list, o)
return nil
@@ -158,6 +159,17 @@ func (a *LocalConn) ListObjectsWithMeta(bucket string, prefix string) ([]ObjMeta
return list, err
}
+func (a *LocalConn) ListObjectWithMeta(bucket string, prefix string) (ObjMeta, error) {
+ list, err := a.ListObjectsWithMeta(bucket, prefix)
+ if err != nil {
+ return ObjMeta{}, err
+ }
+ if len(list) == 0 {
+ return ObjMeta{}, fmt.Errorf("No object found for %s", prefix)
+ }
+ return list[0], nil
+}
+
// AddToQueue adds a message to a queue
func (a *LocalConn) AddToQueue(url string, msg string) error {
f, err := os.OpenFile(filepath.Join(a.TempDir, url), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -184,12 +196,12 @@ func (a *LocalConn) DelFromQueue(url string, handle string) error {
// store the joining of part before and part after handle
var complete string
- if len(s) >= len(handle) + 1 {
+ if len(s) >= len(handle)+1 {
if i > 0 {
complete = s[:i]
}
// the '+1' is for the newline character
- complete += s[i + len(handle) + 1:]
+ complete += s[i+len(handle)+1:]
}
f, err := os.Create(filepath.Join(a.TempDir, url))
@@ -221,7 +233,7 @@ func (a *LocalConn) Download(bucket string, key string, path string) error {
// Upload just copies the file from path to TempDir/bucket/key
func (a *LocalConn) Upload(bucket string, key string, path string) error {
d := filepath.Join(a.TempDir, bucket, filepath.Dir(key))
- err := os.Mkdir(d, 0700)
+ err := os.MkdirAll(d, 0700)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("Error creating temporary directory: %v", err)
}