Browse Source

Rewrote most of the implementation

main
root 2 months ago
parent
commit
af6ab4553d
  1. 32
      internal/storage/core.go
  2. 77
      internal/storage/filesystem.go

32
internal/storage/core.go

@ -1,6 +1,9 @@
package storage
import "github.com/google/uuid"
import (
"errors"
"github.com/google/uuid"
)
var allowedMimetypes = []string{
"image/tiff", // TODO: Support?
@ -14,6 +17,17 @@ var allowedMimetypes = []string{
// Checksum is a SHA256 checksum
type Checksum = [32]byte
type Driver = uint
const (
DriverFilesystem Driver = iota
DriverS3
)
var (
WrongDriverError = errors.New("wrong driver for this file")
NotYetUploadedError = errors.New("file hasn't been uploaded yet")
)
type Storage interface {
// PendingUploads returns the list of temporary files that are awaiting upload
@ -23,19 +37,23 @@ type Storage interface {
Upload(file MediaFile) (MediaFile, error)
// UploadedImages returns the list of all uploaded files
UploadedImages() ([]MediaFile, error)
// IsTemporary returns true if there's a temporary version of this file (as "this file appears in the temporary storage)
IsTemporary(file MediaFile) (bool, error)
// IsUploaded returns true if there's an uploaded version of this file
IsUploaded(file MediaFile) (bool, error)
// HasThumbnail returns true if there's a thumbnail that has been generated for this file
HasThumbnail(file MediaFile) (bool, error)
// Checksum returns the uploaded file's checksum (full size), errors if the file isn't uploaded
Checksum(file MediaFile) (*Checksum, error)
}
type MediaFile interface {
// IsTemporary returns true if there's a temporary version of this file (as "this file appears in the temporary storage)
IsTemporary() bool
// StorageDriver returns the driver used as storage backend to handle this file
StorageDriver() Driver
// TemporaryName returns the raw file name as found in the temporary storage
TemporaryName() string
// HasThumbnail returns true if there's a thumbnail that has been generated for this file
HasThumbnail() bool
// Name returns the unique identifier used as common name for this file, once it is uploaded
Name() uuid.UUID
// Mimetype returns the normalized mimetype of the file
Mimetype() (string, error)
// Checksum returns the uploaded file's checksum (full size)
Checksum() (Checksum, error)
}

77
internal/storage/filesystem.go

@ -1,9 +1,12 @@
package storage
import (
"crypto/sha256"
"github.com/gabriel-vasile/mimetype"
"github.com/google/uuid"
"io"
"io/ioutil"
"os"
"path"
)
@ -17,7 +20,6 @@ type LocalMediaFile struct {
temporaryFilename string
id uuid.UUID
mimetype string
//checksum
}
func (f FilesystemStorage) PendingUploads() ([]MediaFile, error) {
@ -56,26 +58,79 @@ func (f FilesystemStorage) UploadedImages() ([]MediaFile, error) {
panic("implement me")
}
func (l LocalMediaFile) IsTemporary() bool {
panic("implement me")
func (f FilesystemStorage) IsTemporary(file MediaFile) (bool, error) {
if file.StorageDriver() != DriverFilesystem {
return false, WrongDriverError
}
stat, err := os.Stat(path.Join(f.thumbnailFolder, file.TemporaryName()))
if err != nil {
return false, err
}
return !stat.IsDir(), nil
}
func (l LocalMediaFile) TemporaryName() string {
panic("implement me")
func (f FilesystemStorage) IsUploaded(file MediaFile) (bool, error) {
if file.StorageDriver() != DriverFilesystem {
return false, WrongDriverError
}
stat, err := os.Stat(path.Join(f.mediaFolder, file.TemporaryName()))
if err != nil {
return false, err
}
return !stat.IsDir(), nil
}
func (l LocalMediaFile) HasThumbnail() bool {
panic("implement me")
func (f FilesystemStorage) HasThumbnail(file MediaFile) (bool, error) {
if file.StorageDriver() != DriverFilesystem {
return false, WrongDriverError
}
stat, err := os.Stat(path.Join(f.thumbnailFolder, file.TemporaryName()))
if err != nil {
return false, err
}
return !stat.IsDir(), nil
}
func (f FilesystemStorage) Checksum(file MediaFile) (*Checksum, error) {
if file.StorageDriver() != DriverFilesystem {
return nil, WrongDriverError
}
if res, err := f.IsUploaded(file); err != nil || !res {
return nil, NotYetUploadedError
}
fullPath := path.Join(f.mediaFolder, file.Name().String())
fd, err := os.Open(fullPath)
if err != nil {
return nil, err
}
defer fd.Close()
h := sha256.New()
if _, err := io.Copy(h, fd); err != nil {
return nil, err
}
var checksum Checksum
copy(checksum[:], h.Sum(nil))
return &checksum, nil
}
func (l LocalMediaFile) TemporaryName() string {
return l.temporaryFilename
}
func (l LocalMediaFile) Name() uuid.UUID {
panic("implement me")
return l.id
}
func (l LocalMediaFile) Mimetype() (string, error) {
panic("implement me")
return l.mimetype
}
func (l LocalMediaFile) Checksum() (Checksum, error) {
panic("implement me")
func (l LocalMediaFile) StorageDriver() Driver {
return DriverFilesystem
}
Loading…
Cancel
Save