67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package media
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/rpc"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"git.mstar.dev/mstar/linstrom/config"
|
|
)
|
|
|
|
type Server struct {
|
|
client *minio.Client
|
|
transcoderClient *rpc.Client
|
|
}
|
|
|
|
var (
|
|
ErrNoBucketAccess = errors.New("can't access configured bucket")
|
|
)
|
|
|
|
var GlobalServer *Server
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
if config.GlobalConfig.Transcoder.IgnoreTranscoder {
|
|
return &Server{client: client, transcoderClient: nil}, nil
|
|
}
|
|
transcoderClient, err := rpc.DialHTTP("tcp", config.GlobalConfig.Transcoder.Address())
|
|
if err != nil {
|
|
log.Warn().Err(err).
|
|
Str("address", config.GlobalConfig.Transcoder.Address()).
|
|
Msg("failed to dial transcoder, various media related features won't be available")
|
|
transcoderClient = nil
|
|
}
|
|
return &Server{client: client, transcoderClient: transcoderClient}, nil
|
|
}
|