Add filesystem store

This commit is contained in:
Torjus Håkestad 2021-12-03 23:51:48 +01:00
parent 85fd8de5cb
commit d53235d8ef
4 changed files with 180 additions and 0 deletions

94
store/filesystem.go Normal file
View File

@ -0,0 +1,94 @@
package store
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gitea.benny.dog/torjus/ezshare/pb"
"github.com/google/uuid"
)
var _ FileStore = &FileSystemStore{}
type FileSystemStore struct {
dir string
}
func NewFileSystemStore(dir string) *FileSystemStore {
return &FileSystemStore{dir: dir}
}
func (s *FileSystemStore) GetFile(id string) (*pb.File, error) {
path := filepath.Join(s.dir, id)
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(os.ErrNotExist) {
return nil, ErrNoSuchFile
}
return nil, fmt.Errorf("unable to open file: %w", err)
}
defer f.Close()
fm, err := os.Open(fmt.Sprintf("%s.metadata", path))
if err != nil {
// TODO: Different error if file not found
return nil, fmt.Errorf("unable to open file: %w", err)
}
defer fm.Close()
var file pb.File
if file.Data, err = ioutil.ReadAll(f); err != nil {
return nil, fmt.Errorf("unable to read file: %w", err)
}
decoder := json.NewDecoder(fm)
if err := decoder.Decode(&file.Metadata); err != nil {
return nil, fmt.Errorf("unable to read file metadata: %w", err)
}
return &file, nil
}
func (s *FileSystemStore) StoreFile(file *pb.File) (string, error) {
id := uuid.Must(uuid.NewRandom()).String()
file.FileId = id
path := filepath.Join(s.dir, file.FileId)
f, err := os.Create(path)
if err != nil {
return "", fmt.Errorf("unable to store file: %w", err)
}
defer f.Close()
fm, err := os.Create(fmt.Sprintf("%s.metadata", path))
if err != nil {
return "", fmt.Errorf("unable to store metadata: %w", err)
}
defer fm.Close()
if _, err := f.Write(file.Data); err != nil {
return "", fmt.Errorf("unable to write file to store: %w", err)
}
// TODO: Write metadata
encoder := json.NewEncoder(fm)
if err := encoder.Encode(file.Metadata); err != nil {
return "", fmt.Errorf("unable to write file to store: %w", err)
}
return file.FileId, nil
}
func (s *FileSystemStore) DeleteFile(id string) error {
path := filepath.Join(s.dir, id)
metadataPath := fmt.Sprintf("%s.metadata", path)
if err := os.Remove(path); err != nil {
return fmt.Errorf("error deleting file: %w", err)
}
if err := os.Remove(metadataPath); err != nil {
return fmt.Errorf("error deleting file metadata: %w", err)
}
return nil
}

22
store/filesystem_test.go Normal file
View File

@ -0,0 +1,22 @@
package store_test
import (
"io/ioutil"
"os"
"testing"
"gitea.benny.dog/torjus/ezshare/store"
)
func TestFileStore(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), "ezshare-test")
if err != nil {
t.Fatalf("unable to create temp-dir")
}
defer func() {
_ = os.RemoveAll(dir)
}()
s := store.NewFileSystemStore(dir)
doFileStoreTest(s, t)
}

12
store/memory_test.go Normal file
View File

@ -0,0 +1,12 @@
package store_test
import (
"testing"
"gitea.benny.dog/torjus/ezshare/store"
)
func TestMemoryStore(t *testing.T) {
s := store.NewMemoryFileStore()
doFileStoreTest(s, t)
}

52
store/store_test.go Normal file
View File

@ -0,0 +1,52 @@
package store_test
import (
"testing"
"time"
"gitea.benny.dog/torjus/ezshare/pb"
"gitea.benny.dog/torjus/ezshare/store"
"google.golang.org/protobuf/types/known/timestamppb"
)
func doFileStoreTest(s store.FileStore, t *testing.T) {
t.Run("Basics", func(t *testing.T) {
// Create
file := &pb.File{
Data: []byte("testdata lol!"),
Metadata: &pb.File_Metadata{
UploadedOn: timestamppb.New(time.Now()),
ExpiresOn: timestamppb.New(time.Now().Add(24 * time.Hour)),
OriginalFilename: "data.txt",
},
}
id, err := s.StoreFile(file)
if err != nil {
t.Fatalf("Unable to store file: %s", err)
}
retrieved, err := s.GetFile(id)
if err != nil {
t.Fatalf("Unable to get file: %s", err)
}
if len(file.Data) != len(retrieved.Data) {
t.Fatalf("Mismatch in size between stored and retrieved. Got %d want %d", len(retrieved.Data), len(file.Data))
}
for i := range file.Data {
if file.Data[i] != retrieved.Data[i] {
t.Fatalf("Mismatch at %d", i)
}
}
if err := s.DeleteFile(id); err != nil {
t.Fatalf("Unable to delete file: %s", err)
}
if _, err := s.GetFile(id); err != store.ErrNoSuchFile {
t.Fatalf("Getting deleted file returned wrong error: %s", err)
}
})
}