Add media upload functionality
All checks were successful
/ docker (push) Successful in 1m54s

This commit is contained in:
Melody Becker 2025-06-17 16:48:24 +02:00
parent 67be27aebe
commit c813c4784a
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
5 changed files with 145 additions and 12 deletions

View file

@ -3,6 +3,9 @@ package media
import (
"context"
"database/sql"
"git.mstar.dev/mstar/goutils/other"
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
"github.com/gabriel-vasile/mimetype"
"io"
"os"
"path"
@ -27,18 +30,23 @@ func (s *Server) AddFile(fileReader io.Reader, filename, userId string) error {
}
_ = file.Close()
if s.transcoderClient == nil {
return s.addFileAsIs(filename, userId, filePath)
return s.addFileAsIs(filename, userId, filePath, nil)
} 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(
// adFileAsIs uploads the given file. If mtype (short for mimetype, shortened because of module naming conflict)
// is not nil, use that as the file's mimetype. Otherwise, the mimetype will be detected manually
func (s *Server) addFileAsIs(filename, userId, filepath string, mtype *string) error {
if mtype == nil {
mType, err := mimetype.DetectFile(filepath)
if err != nil {
return err
}
mtype = other.IntoPointer(mType.String())
}
s3Result, err := s.client.FPutObject(
context.TODO(),
config.GlobalConfig.S3.BucketName,
UsernameFilename(userId, filename),
@ -52,7 +60,15 @@ func (s *Server) addFileAsIs(filename, userId, filepath string) error {
ID: shared.NewId(),
OwnedById: sql.NullString{Valid: true, String: userId},
Remote: false,
// Location: string, // TODO: Figure this out
Location: s3Result.Location,
Type: *mtype,
Name: UsernameFilename(userId, filename),
AltText: "",
Blurred: false,
}
panic("not implemented")
err = dbgen.MediaMetadata.Create(&fileMetadata)
if err != nil {
return err
}
return nil
}