summaryrefslogtreecommitdiff
path: root/internal/storage/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage/storage.go')
-rw-r--r--internal/storage/storage.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index 55ec0d167..d05fe3519 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -24,6 +24,7 @@ import (
"io"
"mime"
"net/url"
+ "os"
"path"
"syscall"
"time"
@@ -95,6 +96,30 @@ func (d *Driver) PutStream(ctx context.Context, key string, r io.Reader) (int64,
return d.Storage.WriteStream(ctx, key, r)
}
+// PutFile moves the contents of file at path, to storage.Driver{} under given key.
+func (d *Driver) PutFile(ctx context.Context, key string, filepath string) (int64, error) {
+ // Open file at path for reading.
+ file, err := os.Open(filepath)
+ if err != nil {
+ return 0, gtserror.Newf("error opening file %s: %w", filepath, err)
+ }
+
+ // Write the file data to storage under key. Note
+ // that for disk.DiskStorage{} this should end up
+ // being a highly optimized Linux sendfile syscall.
+ sz, err := d.Storage.WriteStream(ctx, key, file)
+ if err != nil {
+ err = gtserror.Newf("error writing file %s: %w", key, err)
+ }
+
+ // Close the file: done with it.
+ if e := file.Close(); e != nil {
+ log.Errorf(ctx, "error closing file %s: %v", filepath, e)
+ }
+
+ return sz, err
+}
+
// Delete attempts to remove the supplied key (and corresponding value) from storage.
func (d *Driver) Delete(ctx context.Context, key string) error {
return d.Storage.Remove(ctx, key)