64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package files
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"mime/multipart"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/spf13/viper"
|
|
"github.com/google/uuid"
|
|
|
|
"git.jmbit.de/filegate/filegate/db"
|
|
"git.jmbit.de/filegate/filegate/files/localfiles"
|
|
)
|
|
|
|
func UploadFile(file *multipart.FileHeader, name string, url string, comment string) (uint, error) {
|
|
ctx := context.Background()
|
|
blob := uuid.NewString()
|
|
fileReader, err := file.Open()
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return 0, err
|
|
}
|
|
contentType := file.Header.Get("ContentType")
|
|
fileID, err := db.CreateFile(name, url, comment, blob)
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return fileID, err
|
|
}
|
|
db.UpdateOriginalName(fileID, file.Filename)
|
|
if viper.GetString("localfs") == "" {
|
|
objectInfo, err := MinioClient.PutObject(ctx, viper.GetString("minio.bucket"), blob, fileReader, file.Size,
|
|
minio.PutObjectOptions{ContentType: contentType})
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return 0, err
|
|
}
|
|
log.Printf("Successfully uploaded %s of size %d\n", blob, objectInfo.Size)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, err
|
|
}
|
|
log.Printf("Successfully uploaded %s of size %d\n", blob, objectInfo.Size)
|
|
} else {
|
|
err := localfiles.PutObject(viper.GetString("localfs"), blob, fileReader)
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
// go service.RunStaticAnalysis(fileID)
|
|
return fileID, nil
|
|
}
|
|
|
|
// GetFile returns a pointer to a File stored in Minio by passing it the UUID ("Blob"-ID) of the file
|
|
func GetFile(uuid string) (*minio.Object, error) {
|
|
ctx := context.Background()
|
|
object, err := MinioClient.GetObject(ctx, viper.GetString("mino.bucket"), uuid, minio.GetObjectOptions{})
|
|
|
|
return object, err
|
|
}
|
|
|