Paperclip/pkg/internal/services/attachments.go

145 lines
3.5 KiB
Go
Raw Normal View History

2024-05-17 07:59:51 +00:00
package services
import (
"fmt"
"mime"
"mime/multipart"
"net/http"
"path/filepath"
2024-07-25 14:02:26 +00:00
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
2024-06-22 04:18:54 +00:00
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
2024-05-17 07:59:51 +00:00
"github.com/google/uuid"
"gorm.io/gorm"
)
2024-06-29 13:16:11 +00:00
const metadataCacheLimit = 512
var metadataCache = make(map[uint]models.Attachment)
2024-05-17 07:59:51 +00:00
func GetAttachmentByID(id uint) (models.Attachment, error) {
2024-06-29 13:16:11 +00:00
if val, ok := metadataCache[id]; ok {
return val, nil
2024-05-17 07:59:51 +00:00
}
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
2024-06-29 13:16:11 +00:00
BaseModel: models.BaseModel{ID: id},
2024-05-17 07:59:51 +00:00
}).First(&attachment).Error; err != nil {
return attachment, err
2024-06-29 13:16:11 +00:00
} else {
if len(metadataCache) > metadataCacheLimit {
clear(metadataCache)
}
metadataCache[id] = attachment
2024-05-17 07:59:51 +00:00
}
2024-06-29 13:16:11 +00:00
2024-05-17 07:59:51 +00:00
return attachment, nil
}
func GetAttachmentByHash(hash string) (models.Attachment, error) {
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
HashCode: hash,
}).First(&attachment).Error; err != nil {
return attachment, err
}
return attachment, nil
}
2024-07-25 14:02:26 +00:00
func NewAttachmentMetadata(tx *gorm.DB, user *models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, bool, error) {
2024-05-17 07:59:51 +00:00
linked := false
exists, pickupErr := GetAttachmentByHash(attachment.HashCode)
if pickupErr == nil {
linked = true
2024-05-20 14:31:55 +00:00
exists.Alternative = attachment.Alternative
exists.Usage = attachment.Usage
exists.Metadata = attachment.Metadata
2024-05-17 07:59:51 +00:00
attachment = exists
attachment.ID = 0
2024-07-25 14:02:26 +00:00
if user != nil {
attachment.AccountID = &user.ID
}
2024-05-17 07:59:51 +00:00
} else {
// Upload the new file
attachment.Uuid = uuid.NewString()
attachment.Size = file.Size
attachment.Name = file.Filename
2024-07-25 14:02:26 +00:00
if user != nil {
attachment.AccountID = &user.ID
}
2024-05-17 07:59:51 +00:00
2024-06-29 13:16:11 +00:00
// If the user didn't provide file mimetype manually, we have to detect it
2024-05-17 07:59:51 +00:00
if len(attachment.MimeType) == 0 {
if ext := filepath.Ext(attachment.Name); len(ext) > 0 {
// Detect mimetype by file extensions
attachment.MimeType = mime.TypeByExtension(ext)
} else {
// Detect mimetype by file header
// This method as a fallback method, because this isn't pretty accurate
header, err := file.Open()
if err != nil {
return attachment, false, fmt.Errorf("failed to read file header: %v", err)
}
defer header.Close()
fileHeader := make([]byte, 512)
_, err = header.Read(fileHeader)
if err != nil {
return attachment, false, err
}
attachment.MimeType = http.DetectContentType(fileHeader)
}
}
}
if err := tx.Save(&attachment).Error; err != nil {
return attachment, linked, fmt.Errorf("failed to save attachment record: %v", err)
2024-06-29 13:16:11 +00:00
} else {
if len(metadataCache) > metadataCacheLimit {
clear(metadataCache)
}
metadataCache[attachment.ID] = attachment
2024-05-17 07:59:51 +00:00
}
return attachment, linked, nil
}
2024-06-29 13:16:11 +00:00
func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
if err := database.C.Save(&item).Error; err != nil {
return item, err
} else {
if len(metadataCache) > metadataCacheLimit {
clear(metadataCache)
}
metadataCache[item.ID] = item
}
return item, nil
}
2024-05-17 07:59:51 +00:00
func DeleteAttachment(item models.Attachment) error {
var dupeCount int64
if err := database.C.
Where(&models.Attachment{HashCode: item.HashCode}).
Model(&models.Attachment{}).
Count(&dupeCount).Error; err != nil {
dupeCount = -1
}
if err := database.C.Delete(&item).Error; err != nil {
return err
2024-06-29 13:16:11 +00:00
} else {
delete(metadataCache, item.ID)
2024-05-17 07:59:51 +00:00
}
if dupeCount != -1 && dupeCount <= 1 {
return DeleteFile(item)
}
return nil
}