linstrom/media/media.go
mstar 29eda04330
Some checks failed
/ docker (push) Failing after 4m43s
Start work on files/s3 integration
2025-06-11 15:35:14 +02:00

70 lines
1.6 KiB
Go

package media
import (
"context"
"errors"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"git.mstar.dev/mstar/linstrom/config"
)
type Server struct {
client *minio.Client
}
var (
ErrNoBucketAccess = errors.New("can't access configured bucket")
)
func NewServer() (*Server, error) {
client, err := minio.New(config.GlobalConfig.S3.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(
config.GlobalConfig.S3.KeyId,
config.GlobalConfig.S3.Secret,
"",
),
Secure: config.GlobalConfig.S3.UseSSL,
Region: config.GlobalConfig.S3.Region,
})
if err != nil {
return nil, err
}
ctx := context.Background()
// Ensure we can access the bucket
err = client.MakeBucket(
ctx,
config.GlobalConfig.S3.BucketName,
minio.MakeBucketOptions{Region: config.GlobalConfig.S3.Region},
)
if err != nil {
exists, err := client.BucketExists(ctx, config.GlobalConfig.S3.BucketName)
if err != nil {
return nil, err
}
if !exists {
return nil, ErrNoBucketAccess
}
}
return &Server{client: client}, nil
}
// Convert a userId and filename into a proper filepath for s3.
// Reason for this is that the userId for external users is a valid url which needs to be encoded
func UsernameFilename(userId, filename string) string {
return userId + "//" + filename
}
func (s *Server) HasFileScoped(userId, filename string) (bool, error) {
info, err := s.client.StatObject(
context.Background(),
config.GlobalConfig.S3.BucketName,
UsernameFilename(userId, filename),
minio.GetObjectOptions{},
)
if err != nil {
return false, err
}
return info.IsDeleteMarker, nil
}