2020-03-07 16:55:13 -08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2022-01-02 13:17:28 +02:00
|
|
|
"net/http"
|
2020-03-07 16:55:13 -08:00
|
|
|
)
|
|
|
|
|
2022-01-02 13:17:28 +02:00
|
|
|
// Storage is a file system interface to host downloaded episodes and feeds.
|
2020-03-07 16:55:13 -08:00
|
|
|
type Storage interface {
|
2022-01-02 13:17:28 +02:00
|
|
|
// FileSystem must be implemented to in order to pass Storage interface to HTTP file server.
|
|
|
|
http.FileSystem
|
|
|
|
|
2020-03-07 16:55:13 -08:00
|
|
|
// Create will create a new file from reader
|
2022-01-02 13:17:28 +02:00
|
|
|
Create(ctx context.Context, name string, reader io.Reader) (int64, error)
|
2020-03-07 16:55:13 -08:00
|
|
|
|
|
|
|
// Delete deletes the file
|
2022-01-02 13:17:28 +02:00
|
|
|
Delete(ctx context.Context, name string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns storage object's size in bytes.
|
|
|
|
func Size(storage http.FileSystem, name string) (int64, error) {
|
|
|
|
file, err := storage.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2020-03-07 16:55:13 -08:00
|
|
|
|
2022-01-02 13:17:28 +02:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-07 16:55:13 -08:00
|
|
|
|
2022-01-02 13:17:28 +02:00
|
|
|
return stat.Size(), nil
|
2020-03-07 16:55:13 -08:00
|
|
|
}
|