Add filesize to file metadata
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-24 15:52:15 +01:00
parent 889894a737
commit 17a484db91
4 changed files with 11 additions and 2 deletions

View File

@ -12,6 +12,8 @@ type File struct {
ExpiresOn time.Time `json:"expires_on"` ExpiresOn time.Time `json:"expires_on"`
Body io.ReadCloser Body io.ReadCloser
FileSize int64 `json:"file_size"`
} }
type FileStore interface { type FileStore interface {

View File

@ -40,10 +40,12 @@ func (s *FSFileStore) Store(f *File) error {
} }
defer dst.Close() defer dst.Close()
if _, err := io.Copy(dst, f.Body); err != nil { n, err := io.Copy(dst, f.Body)
if err != nil {
return err return err
} }
s.metadata[f.ID] = metadata s.metadata[f.ID] = metadata
s.metadata[f.ID].FileSize = n
if err := s.writeMetadata(); err != nil { if err := s.writeMetadata(); err != nil {
delete(s.metadata, f.ID) delete(s.metadata, f.ID)
return err return err

View File

@ -14,6 +14,7 @@ type fileData struct {
MaxViews uint MaxViews uint
ExpiresOn time.Time ExpiresOn time.Time
FileSize int64
} }
type MemoryFileStore struct { type MemoryFileStore struct {
@ -35,9 +36,11 @@ func (s *MemoryFileStore) Store(f *File) error {
ExpiresOn: f.ExpiresOn, ExpiresOn: f.ExpiresOn,
} }
_, err := io.Copy(&data.Body, f.Body) n, err := io.Copy(&data.Body, f.Body)
_ = f.Body.Close() _ = f.Body.Close()
data.FileSize = n
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()

View File

@ -92,6 +92,7 @@ func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.
MaxViews: 5, MaxViews: 5,
ExpiresOn: time.Now().Add(10 * time.Minute), ExpiresOn: time.Now().Add(10 * time.Minute),
Body: io.NopCloser(strings.NewReader("cocks!")), Body: io.NopCloser(strings.NewReader("cocks!")),
FileSize: 6,
}, },
ExpectedData: "cocks!", ExpectedData: "cocks!",
}, },
@ -102,6 +103,7 @@ func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.
MaxViews: 5, MaxViews: 5,
ExpiresOn: time.Now().Add(10 * time.Minute), ExpiresOn: time.Now().Add(10 * time.Minute),
Body: io.NopCloser(strings.NewReader("derps!")), Body: io.NopCloser(strings.NewReader("derps!")),
FileSize: 6,
}, },
ExpectedData: "derps!", ExpectedData: "derps!",
}, },