go fmt
This commit is contained in:
@ -1,26 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"code.google.com/p/goconf/conf"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"errors"
|
||||
"code.google.com/p/goconf/conf"
|
||||
)
|
||||
|
||||
type LocalStorage struct {
|
||||
storageDir string
|
||||
tmpSubdir string
|
||||
tmpSubdir string
|
||||
}
|
||||
|
||||
func ensureDirExists(dir string) error {
|
||||
_, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
fi, err := os.Lstat(path.Dir(dir))
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Mkdir(dir, fi.Mode().Perm())
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -37,36 +41,50 @@ func NewLocalStorage(config *conf.ConfigFile) (*LocalStorage, error) {
|
||||
|
||||
//make sure the base directory and tmp subdir exist
|
||||
err = ensureDirExists(ls.storageDir)
|
||||
if err != nil { return nil, err}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = ensureDirExists(ls.tmpSubdir)
|
||||
if err != nil { return nil, err}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
func (ls *LocalStorage) copyToTmp(src string) (string, error) {
|
||||
infile, err := os.Open(src)
|
||||
if err != nil { return "", err }
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer infile.Close()
|
||||
|
||||
|
||||
outfile, err := ioutil.TempFile(ls.tmpSubdir, "asink")
|
||||
if err != nil { return "", err }
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer outfile.Close()
|
||||
|
||||
_, err = io.Copy(outfile, infile)
|
||||
if err != nil { return "", err }
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return outfile.Name(), nil
|
||||
}
|
||||
|
||||
func (ls *LocalStorage) Put(filename string, hash string) (e error) {
|
||||
tmpfile, err := ls.copyToTmp(filename)
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.Rename(tmpfile, path.Join(ls.storageDir, hash))
|
||||
if err != nil {
|
||||
err := os.Remove(tmpfile)
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -74,11 +92,15 @@ func (ls *LocalStorage) Put(filename string, hash string) (e error) {
|
||||
|
||||
func (ls *LocalStorage) Get(filename string, hash string) error {
|
||||
infile, err := os.Open(path.Join(ls.storageDir, hash))
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer infile.Close()
|
||||
|
||||
outfile, err := os.Open(filename)
|
||||
if err != nil { return err }
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outfile.Close()
|
||||
|
||||
_, err = io.Copy(outfile, infile)
|
||||
|
Reference in New Issue
Block a user