Start work on media storage

This commit is contained in:
Melody Becker 2024-11-21 14:45:25 +01:00
parent bf8d272122
commit 9acecfd7f6
10 changed files with 110 additions and 48 deletions

View file

@ -1,24 +0,0 @@
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
}

View file

@ -25,7 +25,8 @@ func Compress(dataReader io.Reader, mimeType *string) (io.Reader, error) {
return nil, err
}
if mimeType == nil {
*mimeType = mimetype.Detect(data).String()
tmp := mimetype.Detect(data).String()
mimeType = &tmp
}
uberType, subType, _ := strings.Cut(*mimeType, "/")
var dataOut []byte

View file

@ -0,0 +1,59 @@
package mediaprovider
import (
"context"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"gitlab.com/mstarongitlab/linstrom/config"
)
type Provider struct {
client *minio.Client
}
// Create a new storage provider using the values from the global config
// It also sets up the bucket in the s3 provider if it doesn't exist yet
func NewProviderFromConfig() (*Provider, error) {
client, err := minio.New(config.GlobalConfig.S3.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(
config.GlobalConfig.S3.KeyId,
config.GlobalConfig.S3.Secret,
"",
),
Region: config.GlobalConfig.S3.Region,
Secure: config.GlobalConfig.S3.UseSSL,
})
if err != nil {
return nil, err
}
if err = setupBucket(client, config.GlobalConfig.S3.BucketName); err != nil {
return nil, err
}
return &Provider{
client: client,
}, nil
}
func setupBucket(client *minio.Client, bucketName string) error {
ctx := context.Background()
// Give it half a minute tops to check if a bucket with the given name already exists
existsContext, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
ok, err := client.BucketExists(existsContext, bucketName)
if err != nil {
return err
}
if ok {
return nil
}
// Same timeout for creating the new bucket if it doesn't exist
createContext, createCancel := context.WithTimeout(ctx, time.Second*30)
defer createCancel()
err = client.MakeBucket(createContext, bucketName, minio.MakeBucketOptions{
Region: config.GlobalConfig.S3.Region,
})
return err
}

View file

@ -1,11 +0,0 @@
package mediaprovider
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
type ProviderAws struct {
awsSession *session.Session
s3Service *s3.S3
}