working commit
This commit is contained in:
+42
-2
@@ -25,8 +25,10 @@ func NewStorage(basepath string) *Storage {
|
||||
return res
|
||||
}
|
||||
|
||||
const filesubdir = "files"
|
||||
const tmpsubdir = "tmps"
|
||||
const (
|
||||
filesubdir = "files"
|
||||
tmpsubdir = "tmps"
|
||||
)
|
||||
|
||||
func (store *Storage) makeCollecionpath(collection string) string {
|
||||
return filepath.Join(store.basepath, filesubdir, collection)
|
||||
@@ -135,3 +137,41 @@ func (store *Storage) DeleteFile(collection, filename string) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
upsubdir = "uploads"
|
||||
)
|
||||
|
||||
func (store *Storage) makeUppath(upname string) string {
|
||||
return filepath.Join(store.basepath, upsubdir, upname)
|
||||
}
|
||||
|
||||
func (store *Storage) WriteUpload(digest string, source io.Reader) (int64, error) {
|
||||
var err error
|
||||
var recsize int64
|
||||
|
||||
uploadPath := store.makeUppath(digest)
|
||||
uploadFile, err := os.OpenFile(uploadPath, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return recsize, err
|
||||
}
|
||||
defer uploadFile.Close()
|
||||
|
||||
hasher := sha256.New() // TODO: upload cheking
|
||||
streamWriter := io.MultiWriter(uploadFile, hasher)
|
||||
recsize, err = io.Copy(streamWriter, source)
|
||||
if err != nil {
|
||||
return recsize, err
|
||||
}
|
||||
return recsize, err
|
||||
}
|
||||
|
||||
func (st *Storage) RemoveUpload(digest string) error {
|
||||
var err error
|
||||
uploadPath := st.makeUppath(digest)
|
||||
err = os.Remove(uploadPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user