🚀 Fix everything and ready to launch!

This commit is contained in:
2024-07-29 01:47:33 +08:00
parent 82cb45ec53
commit 8f08d85fb1
5 changed files with 31 additions and 31 deletions

View File

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
@ -30,8 +31,11 @@ func PublishAnalyzeTask(file models.Attachment) {
func StartConsumeAnalyzeTask() {
for {
task := <-fileAnalyzeQueue
start := time.Now()
if err := AnalyzeAttachment(task); err != nil {
log.Error().Err(err).Any("task", task).Msg("A file analyze task failed...")
} else {
log.Info().Dur("elapsed", time.Since(start)).Any("task", task).Msg("A file analyze task was completed.")
}
}
}
@ -48,8 +52,8 @@ func AnalyzeAttachment(file models.Attachment) error {
_ = jsoniter.Unmarshal(rawDest, &dest)
dst := filepath.Join(dest.Path, file.Uuid)
if _, err := os.Stat(dst); !os.IsExist(err) {
return fmt.Errorf("attachment doesn't exists in temporary storage")
if _, err := os.Stat(dst); os.IsNotExist(err) {
return fmt.Errorf("attachment doesn't exists in temporary storage: %v", err)
}
if t := strings.SplitN(file.MimeType, "/", 2)[0]; t == "image" {
@ -85,7 +89,7 @@ func AnalyzeAttachment(file models.Attachment) error {
if linked && err != nil {
return fmt.Errorf("unable to link file record: %v", err)
} else if !linked {
if err := tx.Save(&file); err != nil {
if err := tx.Save(&file).Error; err != nil {
tx.Rollback()
return fmt.Errorf("unable to save file record: %v", err)
}
@ -116,13 +120,12 @@ func HashAttachment(file models.Attachment) (hash string, err error) {
_ = jsoniter.Unmarshal(rawDest, &dest)
dst := filepath.Join(dest.Path, file.Uuid)
if _, err = os.Stat(dst); !os.IsExist(err) {
err = fmt.Errorf("attachment doesn't exists in temporary storage")
if _, err = os.Stat(dst); os.IsNotExist(err) {
err = fmt.Errorf("attachment doesn't exists in temporary storage: %v", err)
return
}
var in *os.File
in, err = os.Open("file.txt")
in, err = os.Open(dst)
if err != nil {
err = fmt.Errorf("unable to open file: %v", err)
return

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
jsoniter "github.com/json-iterator/go"
@ -23,8 +24,11 @@ func PublishDeleteFileTask(file models.Attachment) {
func StartConsumeDeletionTask() {
for {
task := <-fileDeletionQueue
start := time.Now()
if err := DeleteFile(task); err != nil {
log.Error().Err(err).Any("task", task).Msg("A file deletion task failed...")
} else {
log.Info().Dur("elapsed", time.Since(start)).Any("task", task).Msg("A file deletion task was completed.")
}
}
}

View File

@ -53,17 +53,19 @@ func ReUploadFileToPermanent(meta models.Attachment) error {
prevRawDest, _ := jsoniter.Marshal(prevDestMap)
_ = jsoniter.Unmarshal(prevRawDest, &prevDest)
in, err := os.Open(filepath.Join(prevDest.Path, meta.Uuid))
if err != nil {
return fmt.Errorf("unable to open file in temporary storage: %v", err)
}
defer in.Close()
inDst := filepath.Join(prevDest.Path, meta.Uuid)
switch dest.Type {
case models.DestinationTypeLocal:
var destConfigured models.LocalDestination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
in, err := os.Open(inDst)
if err != nil {
return fmt.Errorf("unable to open file in temporary storage: %v", err)
}
defer in.Close()
out, err := os.Create(filepath.Join(destConfigured.Path, meta.Uuid))
if err != nil {
return fmt.Errorf("unable to open dest file: %v", err)
@ -79,11 +81,6 @@ func ReUploadFileToPermanent(meta models.Attachment) error {
var destConfigured models.S3Destination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
buffer := bytes.NewBuffer(nil)
if _, err := io.Copy(buffer, in); err != nil {
return fmt.Errorf("create io reader for upload file: %v", err)
}
client, err := minio.New(destConfigured.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(destConfigured.SecretID, destConfigured.SecretKey, ""),
Secure: destConfigured.EnableSSL,
@ -92,8 +89,10 @@ func ReUploadFileToPermanent(meta models.Attachment) error {
return fmt.Errorf("unable to configure s3 client: %v", err)
}
_, err = client.PutObject(context.Background(), destConfigured.Bucket, filepath.Join(destConfigured.Path, meta.Uuid), buffer, -1, minio.PutObjectOptions{
ContentType: meta.MimeType,
_, err = client.FPutObject(context.Background(), destConfigured.Bucket, filepath.Join(destConfigured.Path, meta.Uuid), inDst, minio.PutObjectOptions{
ContentType: meta.MimeType,
SendContentMd5: false,
DisableContentSha256: true,
})
if err != nil {
return fmt.Errorf("unable to upload file to s3: %v", err)
@ -128,8 +127,10 @@ func UploadFileToS3(config models.S3Destination, file *multipart.FileHeader, met
return fmt.Errorf("unable to configure s3 client: %v", err)
}
_, err = client.PutObject(context.Background(), config.Bucket, filepath.Join(config.Path, meta.Uuid), buffer, -1, minio.PutObjectOptions{
ContentType: meta.MimeType,
_, err = client.PutObject(context.Background(), config.Bucket, filepath.Join(config.Path, meta.Uuid), buffer, file.Size, minio.PutObjectOptions{
ContentType: meta.MimeType,
SendContentMd5: false,
DisableContentSha256: true,
})
if err != nil {
return fmt.Errorf("unable to upload file to s3: %v", err)