Create files package
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
22
files/filestore.go
Normal file
22
files/filestore.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
ID string `json:"id"`
|
||||
OriginalFilename string `json:"original_filename"`
|
||||
MaxViews uint `json:"max_views"`
|
||||
ExpiresOn time.Time `json:"expires_on"`
|
||||
|
||||
Body io.ReadCloser
|
||||
}
|
||||
|
||||
type FileStore interface {
|
||||
Store(f *File) error
|
||||
Get(id string) (*File, error)
|
||||
Delete(id string) error
|
||||
List() ([]string, error)
|
||||
}
|
115
files/filestore_fs.go
Normal file
115
files/filestore_fs.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FSFileStore struct {
|
||||
dir string
|
||||
metadata map[string]*File
|
||||
}
|
||||
|
||||
func NewFSFileStore(dir string) (*FSFileStore, error) {
|
||||
s := &FSFileStore{
|
||||
dir: dir,
|
||||
metadata: make(map[string]*File),
|
||||
}
|
||||
|
||||
err := s.readMetadata()
|
||||
|
||||
return s, err
|
||||
}
|
||||
func (s *FSFileStore) Store(f *File) error {
|
||||
defer f.Body.Close()
|
||||
|
||||
metadata := &File{
|
||||
ID: f.ID,
|
||||
OriginalFilename: f.OriginalFilename,
|
||||
MaxViews: f.MaxViews,
|
||||
ExpiresOn: f.ExpiresOn,
|
||||
}
|
||||
|
||||
path := filepath.Join(s.dir, f.ID)
|
||||
dst, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, f.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
s.metadata[f.ID] = metadata
|
||||
if err := s.writeMetadata(); err != nil {
|
||||
delete(s.metadata, f.ID)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FSFileStore) Get(id string) (*File, error) {
|
||||
metadata, ok := s.metadata[id]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no such item")
|
||||
}
|
||||
|
||||
path := filepath.Join(s.dir, id)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metadata.Body = f
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func (s *FSFileStore) Delete(id string) error {
|
||||
path := filepath.Join(s.dir, id)
|
||||
if err := os.Remove(path); err != nil {
|
||||
return err
|
||||
}
|
||||
delete(s.metadata, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FSFileStore) List() ([]string, error) {
|
||||
var results []string
|
||||
for k := range s.metadata {
|
||||
results = append(results, k)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s *FSFileStore) writeMetadata() error {
|
||||
path := filepath.Join(s.dir, "metadata.json")
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
encoder := json.NewEncoder(f)
|
||||
if err := encoder.Encode(s.metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FSFileStore) readMetadata() error {
|
||||
path := filepath.Join(s.dir, "metadata.json")
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
// TODO: Handle errors better
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
decoder := json.NewDecoder(f)
|
||||
if err := decoder.Decode(&s.metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
26
files/filestore_fs_test.go
Normal file
26
files/filestore_fs_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package files_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste/files"
|
||||
)
|
||||
|
||||
func TestFSFileStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s, err := files.NewFSFileStore(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating store: %s", err)
|
||||
}
|
||||
|
||||
RunFilestoreTest(s, t)
|
||||
persistentDir := t.TempDir()
|
||||
newFunc := func() files.FileStore {
|
||||
s, err := files.NewFSFileStore(persistentDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating store: %s", err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
RunPersistentFilestoreTest(newFunc, t)
|
||||
}
|
82
files/filestore_memory.go
Normal file
82
files/filestore_memory.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fileData struct {
|
||||
ID string
|
||||
Body bytes.Buffer
|
||||
|
||||
MaxViews uint
|
||||
ExpiresOn time.Time
|
||||
}
|
||||
|
||||
type MemoryFileStore struct {
|
||||
lock sync.RWMutex
|
||||
data map[string]*fileData
|
||||
}
|
||||
|
||||
func NewMemoryFileStore() *MemoryFileStore {
|
||||
return &MemoryFileStore{
|
||||
data: make(map[string]*fileData),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryFileStore) Store(f *File) error {
|
||||
|
||||
data := &fileData{
|
||||
ID: f.ID,
|
||||
MaxViews: f.MaxViews,
|
||||
ExpiresOn: f.ExpiresOn,
|
||||
}
|
||||
|
||||
_, err := io.Copy(&data.Body, f.Body)
|
||||
_ = f.Body.Close()
|
||||
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.data[f.ID] = data
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *MemoryFileStore) Get(id string) (*File, error) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
fd, ok := s.data[id]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no such item")
|
||||
}
|
||||
f := &File{
|
||||
ID: fd.ID,
|
||||
MaxViews: fd.MaxViews,
|
||||
ExpiresOn: fd.ExpiresOn,
|
||||
Body: io.NopCloser(&fd.Body),
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (s *MemoryFileStore) Delete(id string) error {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
delete(s.data, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryFileStore) List() ([]string, error) {
|
||||
var ids []string
|
||||
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
for id := range s.data {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
13
files/filestore_memory_test.go
Normal file
13
files/filestore_memory_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package files_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste/files"
|
||||
)
|
||||
|
||||
func TestMemoryFileStore(t *testing.T) {
|
||||
s := files.NewMemoryFileStore()
|
||||
|
||||
RunFilestoreTest(s, t)
|
||||
}
|
157
files/filestore_test.go
Normal file
157
files/filestore_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package files_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste/files"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func RunFilestoreTest(s files.FileStore, t *testing.T) {
|
||||
t.Run("Basic", func(t *testing.T) {
|
||||
// Create
|
||||
dataString := "TEST_LOL_OMG"
|
||||
id := uuid.Must(uuid.NewRandom()).String()
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyBuf.Write([]byte(dataString))
|
||||
body := io.NopCloser(bodyBuf)
|
||||
f := &files.File{
|
||||
ID: id,
|
||||
MaxViews: 0,
|
||||
Body: body,
|
||||
}
|
||||
|
||||
err := s.Store(f)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to store file: %s", err)
|
||||
}
|
||||
|
||||
// Retrieve
|
||||
retrieved, err := s.Get(id)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to retrieve file: %s", err)
|
||||
}
|
||||
|
||||
retrievedBuf := &bytes.Buffer{}
|
||||
_, err = retrievedBuf.ReadFrom(retrieved.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to read retrieved body: %s", err)
|
||||
}
|
||||
_ = retrieved.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading from retrieved file: %s", err)
|
||||
}
|
||||
|
||||
if retrievedBuf.String() != dataString {
|
||||
t.Fatalf("Data from retrieved body mismatch. Got %s want %s", retrievedBuf.String(), dataString)
|
||||
}
|
||||
|
||||
// List
|
||||
ids, err := s.List()
|
||||
if err != nil {
|
||||
t.Fatalf("Error doing list: %s", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("List has wrong length: %d", len(ids))
|
||||
}
|
||||
if ids[0] != id {
|
||||
t.Fatalf("ID is wrong. Got %s want %s", ids[0], id)
|
||||
}
|
||||
|
||||
// Delete
|
||||
if err := s.Delete(id); err != nil {
|
||||
t.Fatalf("Error deleting file: %s", err)
|
||||
}
|
||||
|
||||
ids, err = s.List()
|
||||
if err != nil {
|
||||
t.Fatalf("Error listing after delete: %s", err)
|
||||
}
|
||||
if len(ids) != 0 {
|
||||
t.Fatalf("List after delete has wrong length: %d", len(ids))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) {
|
||||
s := newStoreFunc()
|
||||
|
||||
files := []struct {
|
||||
File *files.File
|
||||
ExpectedData string
|
||||
}{
|
||||
{
|
||||
File: &files.File{
|
||||
ID: uuid.NewString(),
|
||||
OriginalFilename: "testfile.txt",
|
||||
MaxViews: 5,
|
||||
ExpiresOn: time.Now().Add(10 * time.Minute),
|
||||
Body: io.NopCloser(strings.NewReader("cocks!")),
|
||||
},
|
||||
ExpectedData: "cocks!",
|
||||
},
|
||||
{
|
||||
File: &files.File{
|
||||
ID: uuid.NewString(),
|
||||
OriginalFilename: "testfile2.txt",
|
||||
MaxViews: 5,
|
||||
ExpiresOn: time.Now().Add(10 * time.Minute),
|
||||
Body: io.NopCloser(strings.NewReader("derps!")),
|
||||
},
|
||||
ExpectedData: "derps!",
|
||||
},
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
err := s.Store(f.File)
|
||||
if err != nil {
|
||||
t.Fatalf("Error storing file: %s", err)
|
||||
}
|
||||
}
|
||||
for _, f := range files {
|
||||
retrieved, err := s.Get(f.File.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to retrieve file: %s", err)
|
||||
}
|
||||
|
||||
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
|
||||
if !cmp.Equal(retrieved, f.File, ignoreBody) {
|
||||
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
|
||||
}
|
||||
buf := new(strings.Builder)
|
||||
if _, err := io.Copy(buf, retrieved.Body); err != nil {
|
||||
t.Fatalf("Error reading from body: %s", err)
|
||||
}
|
||||
retrieved.Body.Close()
|
||||
if buf.String() != f.ExpectedData {
|
||||
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
|
||||
}
|
||||
}
|
||||
|
||||
// Reopen store, and fetch again
|
||||
s = newStoreFunc()
|
||||
for _, f := range files {
|
||||
retrieved, err := s.Get(f.File.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to retrieve file: %s", err)
|
||||
}
|
||||
|
||||
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
|
||||
if !cmp.Equal(retrieved, f.File, ignoreBody) {
|
||||
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
|
||||
}
|
||||
buf := new(strings.Builder)
|
||||
if _, err := io.Copy(buf, retrieved.Body); err != nil {
|
||||
t.Fatalf("Error reading from body: %s", err)
|
||||
}
|
||||
retrieved.Body.Close()
|
||||
if buf.String() != f.ExpectedData {
|
||||
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user