Add more things for file handling
This commit is contained in:
parent
c813c4784a
commit
1fcf47bffc
14 changed files with 284 additions and 59 deletions
56
media/readFile.go
Normal file
56
media/readFile.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/config"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
)
|
||||
|
||||
var ErrFileNotFound = errors.New("file not found")
|
||||
|
||||
func (s *Server) ReadFile(userid, filename string) (io.ReadCloser, error) {
|
||||
mm := dbgen.MediaMetadata
|
||||
metadata, err := mm.Where(mm.OwnedById.Eq(sql.NullString{Valid: true, String: userid}), mm.Name.Eq(filename), mm.Remote.Is(false)).
|
||||
Select(mm.ID, mm.Location).
|
||||
First()
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return nil, ErrFileNotFound
|
||||
case nil:
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
return s.client.GetObject(
|
||||
context.TODO(),
|
||||
config.GlobalConfig.S3.BucketName,
|
||||
metadata.Location,
|
||||
minio.GetObjectOptions{},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) ReadFileId(id string) (io.ReadCloser, error) {
|
||||
mm := dbgen.MediaMetadata
|
||||
metadata, err := mm.Where(mm.ID.Eq(id), mm.Remote.Is(false)).
|
||||
Select(mm.Location).
|
||||
First()
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return nil, ErrFileNotFound
|
||||
case nil:
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
return s.client.GetObject(
|
||||
context.TODO(),
|
||||
config.GlobalConfig.S3.BucketName,
|
||||
metadata.Location,
|
||||
minio.GetObjectOptions{},
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue