2015-12-18 22:24:12 +00:00
|
|
|
// Package filestore provide a storage backend based on the local file system.
|
|
|
|
//
|
2015-02-01 15:17:56 +00:00
|
|
|
// FileStore is a storage backend used as a tusd.DataStore in tusd.NewHandler.
|
|
|
|
// It stores the uploads in a directory specified in two different files: The
|
|
|
|
// `[id].info` files are used to store the fileinfo in JSON format. The
|
|
|
|
// `[id].bin` files contain the raw binary data uploaded.
|
|
|
|
// No cleanup is performed so you may want to run a cronjob to ensure your disk
|
|
|
|
// is not filled up with old and finished uploads.
|
2015-12-26 20:23:09 +00:00
|
|
|
//
|
2016-09-27 20:10:16 +00:00
|
|
|
// In addition, it provides an exclusive upload locking mechanism using lock files
|
2015-12-26 20:23:09 +00:00
|
|
|
// which are stored on disk. Each of them stores the PID of the process which
|
2016-09-27 20:10:16 +00:00
|
|
|
// acquired the lock. This allows locks to be automatically freed when a process
|
2015-12-26 20:23:09 +00:00
|
|
|
// is unable to release it on its own because the process is not alive anymore.
|
|
|
|
// For more information, consult the documentation for tusd.LockerDataStore
|
|
|
|
// interface, which is implemented by FileStore
|
2015-02-01 13:57:57 +00:00
|
|
|
package filestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-01-26 20:46:06 +00:00
|
|
|
"fmt"
|
2015-02-01 13:57:57 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-12-26 20:23:09 +00:00
|
|
|
"path/filepath"
|
2015-02-01 13:57:57 +00:00
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
|
|
|
"github.com/tus/tusd/uid"
|
2015-12-26 20:23:09 +00:00
|
|
|
|
2016-09-30 10:03:49 +00:00
|
|
|
"gopkg.in/Acconut/lockfile.v1"
|
2015-02-01 13:57:57 +00:00
|
|
|
)
|
|
|
|
|
2016-07-25 18:59:01 +00:00
|
|
|
var defaultFilePerm = os.FileMode(0664)
|
2015-02-01 13:57:57 +00:00
|
|
|
|
2015-02-01 15:17:56 +00:00
|
|
|
// See the tusd.DataStore interface for documentation about the different
|
|
|
|
// methods.
|
2015-02-01 13:57:57 +00:00
|
|
|
type FileStore struct {
|
2015-02-01 15:17:56 +00:00
|
|
|
// Relative or absolute path to store files in. FileStore does not check
|
2015-12-18 23:21:34 +00:00
|
|
|
// whether the path exists, use os.MkdirAll in this case on your own.
|
2015-02-01 13:57:57 +00:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2015-12-18 23:21:34 +00:00
|
|
|
// New creates a new file based storage backend. The directory specified will
|
|
|
|
// be used as the only storage entry. This method does not check
|
|
|
|
// whether the path exists, use os.MkdirAll to ensure.
|
2015-12-26 20:23:09 +00:00
|
|
|
// In addition, a locking mechanism is provided.
|
|
|
|
func New(path string) FileStore {
|
|
|
|
return FileStore{path}
|
2015-12-18 23:21:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// UseIn sets this store as the core data store in the passed composer and adds
|
|
|
|
// all possible extension to it.
|
2016-02-21 22:25:35 +00:00
|
|
|
func (store FileStore) UseIn(composer *tusd.StoreComposer) {
|
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseGetReader(store)
|
|
|
|
composer.UseTerminater(store)
|
|
|
|
composer.UseLocker(store)
|
|
|
|
}
|
|
|
|
|
2015-02-16 16:53:50 +00:00
|
|
|
func (store FileStore) NewUpload(info tusd.FileInfo) (id string, err error) {
|
2015-02-01 13:57:57 +00:00
|
|
|
id = uid.Uid()
|
2015-03-23 16:58:13 +00:00
|
|
|
info.ID = id
|
2015-02-01 13:57:57 +00:00
|
|
|
|
|
|
|
// Create .bin file with no content
|
|
|
|
file, err := os.OpenFile(store.binPath(id), os.O_CREATE|os.O_WRONLY, defaultFilePerm)
|
|
|
|
if err != nil {
|
2017-01-26 20:46:06 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = fmt.Errorf("upload directory does not exist: %s", store.Path)
|
|
|
|
}
|
|
|
|
return "", err
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// writeInfo creates the file by itself if necessary
|
|
|
|
err = store.writeInfo(id, info)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:02:12 +00:00
|
|
|
func (store FileStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
|
2015-02-01 13:57:57 +00:00
|
|
|
file, err := os.OpenFile(store.binPath(id), os.O_WRONLY|os.O_APPEND, defaultFilePerm)
|
|
|
|
if err != nil {
|
2015-03-23 18:02:12 +00:00
|
|
|
return 0, err
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
n, err := io.Copy(file, src)
|
2015-03-23 18:02:12 +00:00
|
|
|
return n, err
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store FileStore) GetInfo(id string) (tusd.FileInfo, error) {
|
|
|
|
info := tusd.FileInfo{}
|
|
|
|
data, err := ioutil.ReadFile(store.infoPath(id))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
2016-02-21 22:38:38 +00:00
|
|
|
if err := json.Unmarshal(data, &info); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Stat(store.binPath(id))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Offset = stat.Size()
|
|
|
|
|
|
|
|
return info, nil
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 21:05:33 +00:00
|
|
|
func (store FileStore) GetReader(id string) (io.Reader, error) {
|
|
|
|
return os.Open(store.binPath(id))
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:47:39 +00:00
|
|
|
func (store FileStore) Terminate(id string) error {
|
|
|
|
if err := os.Remove(store.infoPath(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Remove(store.binPath(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:33:17 +00:00
|
|
|
func (store FileStore) ConcatUploads(dest string, uploads []string) (err error) {
|
|
|
|
file, err := os.OpenFile(store.binPath(dest), os.O_WRONLY|os.O_APPEND, defaultFilePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
for _, id := range uploads {
|
|
|
|
src, err := store.GetReader(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-21 22:38:38 +00:00
|
|
|
if _, err := io.Copy(file, src); err != nil {
|
2016-01-20 14:33:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-26 20:23:09 +00:00
|
|
|
func (store FileStore) LockUpload(id string) error {
|
|
|
|
lock, err := store.newLock(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lock.TryLock()
|
|
|
|
if err == lockfile.ErrBusy {
|
|
|
|
return tusd.ErrFileLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store FileStore) UnlockUpload(id string) error {
|
|
|
|
lock, err := store.newLock(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lock.Unlock()
|
|
|
|
|
|
|
|
// A "no such file or directory" will be returned if no lockfile was found.
|
|
|
|
// Since this means that the file has never been locked, we drop the error
|
2016-09-27 20:10:16 +00:00
|
|
|
// and continue as if nothing happened.
|
2015-12-26 20:23:09 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:51:33 +00:00
|
|
|
return err
|
2015-12-26 20:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newLock contructs a new Lockfile instance.
|
|
|
|
func (store FileStore) newLock(id string) (lockfile.Lockfile, error) {
|
2017-01-04 19:25:08 +00:00
|
|
|
path, err := filepath.Abs(filepath.Join(store.Path, id+".lock"))
|
2015-12-26 20:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return lockfile.Lockfile(""), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use Lockfile directly instead of lockfile.New to bypass the unnecessary
|
|
|
|
// check whether the provided path is absolute since we just resolved it
|
|
|
|
// on our own.
|
|
|
|
return lockfile.Lockfile(path), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// binPath returns the path to the .bin storing the binary data.
|
2015-02-01 13:57:57 +00:00
|
|
|
func (store FileStore) binPath(id string) string {
|
2017-01-04 19:25:08 +00:00
|
|
|
return filepath.Join(store.Path, id+".bin")
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 20:23:09 +00:00
|
|
|
// infoPath returns the path to the .info file storing the file's info.
|
2015-02-01 13:57:57 +00:00
|
|
|
func (store FileStore) infoPath(id string) string {
|
2017-01-04 19:25:08 +00:00
|
|
|
return filepath.Join(store.Path, id+".info")
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 20:23:09 +00:00
|
|
|
// writeInfo updates the entire information. Everything will be overwritten.
|
2015-02-01 13:57:57 +00:00
|
|
|
func (store FileStore) writeInfo(id string, info tusd.FileInfo) error {
|
|
|
|
data, err := json.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(store.infoPath(id), data, defaultFilePerm)
|
|
|
|
}
|