diff --git a/go.mod b/go.mod index cbdec82..44c1093 100644 --- a/go.mod +++ b/go.mod @@ -47,6 +47,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/go-test/deep v1.1.1 // indirect github.com/go-webauthn/x v0.1.14 // indirect @@ -69,6 +70,7 @@ require ( github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.26.0 // indirect + golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 68572d0..cfd31f8 100644 --- a/go.sum +++ b/go.sum @@ -85,6 +85,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= @@ -376,6 +378,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/storage/mediaFile.go b/storage/mediaFile.go index 5725aac..b2667f2 100644 --- a/storage/mediaFile.go +++ b/storage/mediaFile.go @@ -18,7 +18,7 @@ type MediaMetadata struct { // If not null, this entry is marked as deleted DeletedAt gorm.DeletedAt `gorm:"index"` Remote bool // whether the attachment is a remote one - // Where the media is stored. Url if remote file, + // Where the media is stored. Url if remote file, file path if local Location string Type string // What media type this is following mime types, eg image/png // Descriptive name for a media file diff --git a/storage/mediaProvider/mediaProvider.go b/storage/mediaProvider/mediaProvider.go index 62157aa..77cf2b1 100644 --- a/storage/mediaProvider/mediaProvider.go +++ b/storage/mediaProvider/mediaProvider.go @@ -1,3 +1,24 @@ package mediaprovider +import "io" + // TODO: Implement me + +type FileProvider interface { + // Return the filepath (including name) to every file in the given folder + ListFiles(folder string) (filePaths []string) + // Check whether a given filepath exists + HasFile(filePath string) (ok bool) + // Read the content of a file + ReadFile(filePath string) (data io.Reader, err error) + // Write the given data to a file. + // If it exists, overwrite it + // It it doesn't exist, create it + WriteFile(filePath string, data io.Reader) (err error) + // Delete a given file. If it doesn't exist, do nothing + DeleteFile(filePath string) +} + +type MediaStorage struct { + provider FileProvider +} diff --git a/storage/mediaProvider/preprocessor.go b/storage/mediaProvider/preprocessor.go new file mode 100644 index 0000000..e111e15 --- /dev/null +++ b/storage/mediaProvider/preprocessor.go @@ -0,0 +1,36 @@ +package mediaprovider + +import ( + "io" + "strings" + + "github.com/gabriel-vasile/mimetype" +) + +func Compress(dataReader io.Reader, mimeType *string) (io.Reader, error) { + data, err := io.ReadAll(dataReader) + if err != nil { + return nil, err + } + if mimeType == nil { + *mimeType = mimetype.Detect(data).String() + } + uberType, subType, _ := strings.Cut(*mimeType, "/") + var dataOut []byte + switch uberType { + case "text": + case "application": + case "image": + case "video": + dataOut, err = compressVideo(data, subType) + case "audio": + case "font": + default: + } + return nil, nil +} + +func compressVideo(dataIn []byte, subType string) (dataOut []byte, err error) { + // TODO: Implement me + panic("Implement me") +}