2024-05-17 07:59:51 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2024-07-28 15:16:46 +00:00
|
|
|
"os"
|
2024-05-17 07:59:51 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2024-11-02 02:41:31 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
2024-12-28 13:41:13 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/fs"
|
2024-11-02 02:41:31 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
|
2024-05-17 07:59:51 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
func UploadFileToTemporary(ctx *fiber.Ctx, file *multipart.FileHeader, meta models.Attachment) error {
|
2024-11-09 16:50:47 +00:00
|
|
|
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", meta.Destination))
|
2024-07-28 13:03:56 +00:00
|
|
|
|
|
|
|
var dest models.BaseDestination
|
|
|
|
rawDest, _ := jsoniter.Marshal(destMap)
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &dest)
|
|
|
|
|
|
|
|
switch dest.Type {
|
|
|
|
case models.DestinationTypeLocal:
|
|
|
|
var destConfigured models.LocalDestination
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
2024-08-20 09:08:40 +00:00
|
|
|
return ctx.SaveFile(file, filepath.Join(destConfigured.Path, meta.Uuid))
|
2024-07-28 13:03:56 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)
|
2024-05-17 07:59:51 +00:00
|
|
|
}
|
2024-07-28 13:03:56 +00:00
|
|
|
}
|
|
|
|
|
2024-12-28 13:41:13 +00:00
|
|
|
func ReUploadFile(meta models.Attachment, dst int) error {
|
2024-11-09 16:59:20 +00:00
|
|
|
if dst == models.AttachmentDstTemporary || meta.Destination == dst {
|
2024-11-06 15:01:29 +00:00
|
|
|
return fmt.Errorf("destnation cannot be reversed temporary or the same as the original")
|
|
|
|
}
|
2024-07-28 15:16:46 +00:00
|
|
|
if meta.Destination != models.AttachmentDstTemporary {
|
|
|
|
return fmt.Errorf("attachment isn't in temporary storage, unable to process")
|
|
|
|
}
|
|
|
|
|
2024-12-28 13:41:13 +00:00
|
|
|
prevDst := meta.Destination
|
|
|
|
inDst, err := fs.DownloadFileToLocal(meta, prevDst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to retrieve file content: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupDst := func() {
|
|
|
|
if prevDst == models.AttachmentDstTemporary {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Remove(inDst)
|
|
|
|
}
|
|
|
|
|
2024-11-09 17:04:07 +00:00
|
|
|
meta.Destination = dst
|
2024-11-09 17:00:54 +00:00
|
|
|
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", dst))
|
2024-05-17 07:59:51 +00:00
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
var dest models.BaseDestination
|
|
|
|
rawDest, _ := jsoniter.Marshal(destMap)
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &dest)
|
2024-05-17 07:59:51 +00:00
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
switch dest.Type {
|
2024-05-17 07:59:51 +00:00
|
|
|
case models.DestinationTypeLocal:
|
|
|
|
var destConfigured models.LocalDestination
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
2024-07-28 15:16:46 +00:00
|
|
|
|
2024-07-28 17:47:33 +00:00
|
|
|
in, err := os.Open(inDst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to open file in temporary storage: %v", err)
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
2024-07-28 15:16:46 +00:00
|
|
|
out, err := os.Create(filepath.Join(destConfigured.Path, meta.Uuid))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to open dest file: %v", err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to copy data to dest file: %v", err)
|
|
|
|
}
|
2024-07-29 05:22:57 +00:00
|
|
|
|
|
|
|
database.C.Save(&meta)
|
2024-08-21 13:32:06 +00:00
|
|
|
CacheAttachment(meta)
|
2024-12-28 13:41:13 +00:00
|
|
|
cleanupDst()
|
2024-07-28 15:16:46 +00:00
|
|
|
return nil
|
2024-05-17 07:59:51 +00:00
|
|
|
case models.DestinationTypeS3:
|
|
|
|
var destConfigured models.S3Destination
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
2024-07-28 15:16:46 +00:00
|
|
|
|
|
|
|
client, err := minio.New(destConfigured.Endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(destConfigured.SecretID, destConfigured.SecretKey, ""),
|
|
|
|
Secure: destConfigured.EnableSSL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to configure s3 client: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-07-28 17:47:33 +00:00
|
|
|
_, err = client.FPutObject(context.Background(), destConfigured.Bucket, filepath.Join(destConfigured.Path, meta.Uuid), inDst, minio.PutObjectOptions{
|
|
|
|
ContentType: meta.MimeType,
|
|
|
|
SendContentMd5: false,
|
|
|
|
DisableContentSha256: true,
|
2024-07-28 15:16:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to upload file to s3: %v", err)
|
|
|
|
}
|
2024-07-29 05:22:57 +00:00
|
|
|
|
|
|
|
database.C.Save(&meta)
|
2024-08-21 13:32:06 +00:00
|
|
|
CacheAttachment(meta)
|
2024-12-28 13:41:13 +00:00
|
|
|
cleanupDst()
|
2024-07-28 15:16:46 +00:00
|
|
|
return nil
|
2024-05-17 07:59:51 +00:00
|
|
|
default:
|
2024-07-28 13:03:56 +00:00
|
|
|
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)
|
2024-05-17 07:59:51 +00:00
|
|
|
}
|
|
|
|
}
|