Browse Source

Defined v1 filesystem interface, started implementation of local filesystem

main
root 2 months ago
parent
commit
1fe78cb677
  1. 1
      go.mod
  2. 4
      go.sum
  3. 39
      internal/storage/core.go
  4. 81
      internal/storage/filesystem.go

1
go.mod

@ -4,6 +4,7 @@ go 1.16
require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/gabriel-vasile/mimetype v1.4.0 // indirect
github.com/google/uuid v1.3.0
github.com/kkyr/fig v0.3.0
github.com/uptrace/bun v1.0.12

4
go.sum

@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0FnbhiOsEro=
github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
@ -89,6 +91,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

39
internal/storage/core.go

@ -1,6 +1,41 @@
package storage
import "github.com/google/uuid"
var allowedMimetypes = []string{
"image/tiff", // TODO: Support?
"image/jpeg",
"image/png",
"image/svg+xml",
"image/webp",
"image/gif",
"image/bmp",
}
// Checksum is a SHA256 checksum
type Checksum = [32]byte
type Storage interface {
// TODO: Interface or struct? Depends on my laziness. Probably a struct for the main selector and
// then interface/structs. Because I def want to support S3 for myself
// PendingUploads returns the list of temporary files that are awaiting upload
PendingUploads() ([]MediaFile, error)
// Upload takes a temporary file, uploads it, potentially generates the associated thumbnail, generates its UUID,
// and returns a new MediaFile object with the updated info
Upload(file MediaFile) (MediaFile, error)
// UploadedImages returns the list of all uploaded files
UploadedImages() ([]MediaFile, 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
// 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)
}

81
internal/storage/filesystem.go

@ -0,0 +1,81 @@
package storage
import (
"github.com/gabriel-vasile/mimetype"
"github.com/google/uuid"
"io/ioutil"
"path"
)
type FilesystemStorage struct {
temporaryUploadsFolder string
mediaFolder string
thumbnailFolder string
}
type LocalMediaFile struct {
temporaryFilename string
id uuid.UUID
mimetype string
//checksum
}
func (f FilesystemStorage) PendingUploads() ([]MediaFile, error) {
files, err := ioutil.ReadDir(f.temporaryUploadsFolder)
if err != nil {
return nil, err
}
var medias []MediaFile
for _, file := range files {
mtype, err := mimetype.DetectFile(path.Join(f.temporaryUploadsFolder, file.Name()))
if err != nil {
return medias, err
}
if !mimetype.EqualsAny(mtype.String(), allowedMimetypes...) {
continue
}
id, _ := uuid.NewUUID()
medias = append(medias, LocalMediaFile{
temporaryFilename: file.Name(),
id: id,
mimetype: mtype.String(),
})
}
return medias, nil
}
func (f FilesystemStorage) Upload(file MediaFile) (MediaFile, error) {
panic("implement me")
}
func (f FilesystemStorage) UploadedImages() ([]MediaFile, error) {
panic("implement me")
}
func (l LocalMediaFile) IsTemporary() bool {
panic("implement me")
}
func (l LocalMediaFile) TemporaryName() string {
panic("implement me")
}
func (l LocalMediaFile) HasThumbnail() bool {
panic("implement me")
}
func (l LocalMediaFile) Name() uuid.UUID {
panic("implement me")
}
func (l LocalMediaFile) Mimetype() (string, error) {
panic("implement me")
}
func (l LocalMediaFile) Checksum() (Checksum, error) {
panic("implement me")
}
Loading…
Cancel
Save