tusd/pkg/filestore/filestore.go

226 lines
5.8 KiB
Go
Raw Permalink Normal View History

// Package filestore provide a storage backend based on the local file system.
//
// FileStore is a storage backend used as a handler.DataStore in handler.NewHandler.
2015-02-01 15:17:56 +00:00
// 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]` files without an extension contain the raw binary data uploaded.
2015-02-01 15:17:56 +00:00
// 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.
package filestore
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/tus/tusd/internal/uid"
"github.com/tus/tusd/pkg/handler"
)
var defaultFilePerm = os.FileMode(0664)
// See the handler.DataStore interface for documentation about the different
2015-02-01 15:17:56 +00:00
// methods.
type FileStore struct {
2015-02-01 15:17:56 +00:00
// Relative or absolute path to store files in. FileStore does not check
// whether the path exists, use os.MkdirAll in this case on your own.
Path string
}
// 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.
// In addition, a locking mechanism is provided.
func New(path string) FileStore {
return FileStore{path}
}
// UseIn sets this store as the core data store in the passed composer and adds
// all possible extension to it.
func (store FileStore) UseIn(composer *handler.StoreComposer) {
2016-02-21 22:25:35 +00:00
composer.UseCore(store)
composer.UseTerminater(store)
composer.UseConcater(store)
composer.UseLengthDeferrer(store)
2016-02-21 22:25:35 +00:00
}
func (store FileStore) NewUpload(ctx context.Context, info handler.FileInfo) (handler.Upload, error) {
if info.ID == "" {
info.ID = uid.Uid()
}
binPath := store.binPath(info.ID)
info.Storage = map[string]string{
"Type": "filestore",
"Path": binPath,
}
// Create binary file with no content
file, err := os.OpenFile(binPath, os.O_CREATE|os.O_WRONLY, defaultFilePerm)
if err != nil {
if os.IsNotExist(err) {
err = fmt.Errorf("upload directory does not exist: %s", store.Path)
}
2019-08-26 09:41:52 +00:00
return nil, err
}
err = file.Close()
if err != nil {
return nil, err
}
2019-08-26 09:41:52 +00:00
upload := &fileUpload{
info: info,
infoPath: store.infoPath(info.ID),
binPath: binPath,
}
2019-08-26 09:41:52 +00:00
// writeInfo creates the file by itself if necessary
err = upload.writeInfo()
if err != nil {
return nil, err
}
2019-08-26 09:41:52 +00:00
return upload, nil
}
func (store FileStore) GetUpload(ctx context.Context, id string) (handler.Upload, error) {
info := handler.FileInfo{}
data, err := ioutil.ReadFile(store.infoPath(id))
if err != nil {
if os.IsNotExist(err) {
// Interpret os.ErrNotExist as 404 Not Found
err = handler.ErrNotFound
}
2019-08-26 09:41:52 +00:00
return nil, err
}
if err := json.Unmarshal(data, &info); err != nil {
2019-08-26 09:41:52 +00:00
return nil, err
}
binPath := store.binPath(id)
2019-08-26 09:41:52 +00:00
infoPath := store.infoPath(id)
stat, err := os.Stat(binPath)
if err != nil {
if os.IsNotExist(err) {
// Interpret os.ErrNotExist as 404 Not Found
err = handler.ErrNotFound
}
2019-08-26 09:41:52 +00:00
return nil, err
}
info.Offset = stat.Size()
2019-08-26 09:41:52 +00:00
return &fileUpload{
info: info,
binPath: binPath,
infoPath: infoPath,
}, nil
}
func (store FileStore) AsTerminatableUpload(upload handler.Upload) handler.TerminatableUpload {
return upload.(*fileUpload)
}
func (store FileStore) AsLengthDeclarableUpload(upload handler.Upload) handler.LengthDeclarableUpload {
return upload.(*fileUpload)
}
func (store FileStore) AsConcatableUpload(upload handler.Upload) handler.ConcatableUpload {
return upload.(*fileUpload)
}
2019-08-26 09:41:52 +00:00
// binPath returns the path to the file storing the binary data.
func (store FileStore) binPath(id string) string {
return filepath.Join(store.Path, id)
}
// infoPath returns the path to the .info file storing the file's info.
func (store FileStore) infoPath(id string) string {
return filepath.Join(store.Path, id+".info")
}
type fileUpload struct {
// info stores the current information about the upload
info handler.FileInfo
// infoPath is the path to the .info file
infoPath string
// binPath is the path to the binary file (which has no extension)
binPath string
}
func (upload *fileUpload) GetInfo(ctx context.Context) (handler.FileInfo, error) {
2019-08-26 09:41:52 +00:00
return upload.info, nil
}
func (upload *fileUpload) WriteChunk(ctx context.Context, offset int64, src io.Reader) (int64, error) {
2019-08-26 09:41:52 +00:00
file, err := os.OpenFile(upload.binPath, os.O_WRONLY|os.O_APPEND, defaultFilePerm)
if err != nil {
return 0, err
}
defer file.Close()
n, err := io.Copy(file, src)
upload.info.Offset += n
return n, err
}
func (upload *fileUpload) GetReader(ctx context.Context) (io.ReadCloser, error) {
2019-08-26 09:41:52 +00:00
return os.Open(upload.binPath)
2015-02-06 21:05:33 +00:00
}
func (upload *fileUpload) Terminate(ctx context.Context) error {
2019-08-26 09:41:52 +00:00
if err := os.Remove(upload.infoPath); err != nil {
2015-02-28 13:47:39 +00:00
return err
}
2019-08-26 09:41:52 +00:00
if err := os.Remove(upload.binPath); err != nil {
2015-02-28 13:47:39 +00:00
return err
}
return nil
}
func (upload *fileUpload) ConcatUploads(ctx context.Context, uploads []handler.Upload) (err error) {
file, err := os.OpenFile(upload.binPath, os.O_WRONLY|os.O_APPEND, defaultFilePerm)
if err != nil {
return err
}
defer file.Close()
for _, partialUpload := range uploads {
fileUpload := partialUpload.(*fileUpload)
src, err := os.Open(fileUpload.binPath)
if err != nil {
return err
}
if _, err := io.Copy(file, src); err != nil {
return err
}
}
return
}
func (upload *fileUpload) DeclareLength(ctx context.Context, length int64) error {
2019-08-26 09:41:52 +00:00
upload.info.Size = length
upload.info.SizeIsDeferred = false
return upload.writeInfo()
}
2019-08-26 09:41:52 +00:00
// writeInfo updates the entire information. Everything will be overwritten.
func (upload *fileUpload) writeInfo() error {
data, err := json.Marshal(upload.info)
if err != nil {
return err
}
2019-08-26 09:41:52 +00:00
return ioutil.WriteFile(upload.infoPath, data, defaultFilePerm)
}
func (upload *fileUpload) FinishUpload(ctx context.Context) error {
2019-08-26 09:41:52 +00:00
return nil
}