Paperclip/pkg/internal/services/merger.go

77 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-08-20 09:08:40 +00:00
package services
import (
"fmt"
"io"
"os"
"path/filepath"
2024-09-22 07:39:36 +00:00
2024-11-02 02:41:31 +00:00
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
2024-09-22 07:39:36 +00:00
"github.com/spf13/viper"
2024-08-20 09:08:40 +00:00
)
func MergeFileChunks(meta models.Attachment, arrange []string) (models.Attachment, error) {
2024-09-22 07:39:36 +00:00
// Fetch destination from config
destMap := viper.GetStringMapString("destinations.0")
2024-08-20 09:08:40 +00:00
var dest models.LocalDestination
2024-09-22 07:39:36 +00:00
dest.Path = destMap["path"]
2024-08-20 09:08:40 +00:00
2024-09-22 07:39:36 +00:00
// Create the destination file
2024-08-20 09:08:40 +00:00
destPath := filepath.Join(dest.Path, meta.Uuid)
destFile, err := os.Create(destPath)
if err != nil {
return meta, err
}
defer destFile.Close()
2024-09-22 07:39:36 +00:00
// 32KB buffer
buf := make([]byte, 32*1024)
// Merge the chunks into the destination file
2024-08-20 09:08:40 +00:00
for _, chunk := range arrange {
chunkPath := filepath.Join(dest.Path, fmt.Sprintf("%s.part%s", meta.Uuid, chunk))
2024-08-20 09:08:40 +00:00
chunkFile, err := os.Open(chunkPath)
if err != nil {
return meta, err
}
2024-09-22 07:39:36 +00:00
defer chunkFile.Close() // Ensure the file is closed after reading
2024-08-20 09:08:40 +00:00
2024-09-22 07:39:36 +00:00
for {
n, err := chunkFile.Read(buf)
if err != nil && err != io.EOF {
return meta, err
}
if n == 0 {
break
}
if _, err := destFile.Write(buf[:n]); err != nil {
return meta, err
}
}
2024-08-20 09:08:40 +00:00
}
2024-09-22 07:39:36 +00:00
// Post-upload tasks
2024-08-20 09:08:40 +00:00
meta.IsUploaded = true
meta.FileChunks = nil
2024-09-22 07:39:36 +00:00
if err := database.C.Save(&meta).Error; err != nil {
return meta, err
}
2024-08-20 09:08:40 +00:00
CacheAttachment(meta)
2024-08-20 11:17:43 +00:00
PublishAnalyzeTask(meta)
2024-09-22 07:39:36 +00:00
// Clean up: remove chunk files
2024-08-20 14:55:58 +00:00
for _, chunk := range arrange {
chunkPath := filepath.Join(dest.Path, fmt.Sprintf("%s.part%s", meta.Uuid, chunk))
2024-09-22 07:39:36 +00:00
if err := os.Remove(chunkPath); err != nil {
return meta, err
}
2024-08-20 14:55:58 +00:00
}
2024-08-20 09:08:40 +00:00
return meta, nil
}