This commit is contained in:
parent
8e6f814ba4
commit
e317da1b66
6 changed files with 106 additions and 18 deletions
58
media/addFile.go
Normal file
58
media/addFile.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/config"
|
||||
"git.mstar.dev/mstar/linstrom/shared"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
)
|
||||
|
||||
func (s *Server) AddFile(fileReader io.Reader, filename, userId string) error {
|
||||
transcoderInDir := config.GlobalConfig.Transcoder.InDir()
|
||||
filePath := path.Join(transcoderInDir, filename)
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = io.Copy(file, fileReader); err != nil {
|
||||
_ = file.Close()
|
||||
return err
|
||||
}
|
||||
_ = file.Close()
|
||||
if s.transcoderClient == nil {
|
||||
return s.addFileAsIs(filename, userId, filePath)
|
||||
} else {
|
||||
return s.addFileWithTranscoder(filename, userId, filePath)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) addFileWithTranscoder(filename, userId, filepath string) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (s *Server) addFileAsIs(filename, userId, filepath string) error {
|
||||
_, err := s.client.FPutObject(
|
||||
context.TODO(),
|
||||
config.GlobalConfig.S3.BucketName,
|
||||
UsernameFilename(userId, filename),
|
||||
filepath,
|
||||
minio.PutObjectOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileMetadata := models.MediaMetadata{
|
||||
ID: shared.NewId(),
|
||||
OwnedById: sql.NullString{Valid: true, String: userId},
|
||||
Remote: false,
|
||||
// Location: string, // TODO: Figure this out
|
||||
}
|
||||
panic("not implemented")
|
||||
}
|
|
@ -3,6 +3,8 @@ package media
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/rpc"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
|
@ -11,17 +13,10 @@ import (
|
|||
)
|
||||
|
||||
type Server struct {
|
||||
client *minio.Client
|
||||
client *minio.Client
|
||||
transcoderClient *rpc.Client
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: Figure out an api for a microservice for transcoding media, see https://tech.lgbt/@lina/114682780787448797
|
||||
- Read endpoint from config
|
||||
- Try to reach transcoder
|
||||
- If transcoder is alive, use it for transcoding
|
||||
- If not alive, store files as is
|
||||
*/
|
||||
|
||||
var (
|
||||
ErrNoBucketAccess = errors.New("can't access configured bucket")
|
||||
)
|
||||
|
@ -55,7 +50,15 @@ func NewServer() (*Server, error) {
|
|||
return nil, ErrNoBucketAccess
|
||||
}
|
||||
}
|
||||
return &Server{client: client}, 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
|
||||
}
|
||||
|
||||
// UsernameFilename converts a userId and filename into a proper filepath for s3.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue