portal/metadata/metadata.go

175 lines
3.6 KiB
Go
Raw Permalink Normal View History

2024-02-17 02:56:38 +00:00
package metadata
import (
"context"
"errors"
"time"
"git.lumeweb.com/LumeWeb/portal/db/models"
"go.uber.org/fx"
"gorm.io/gorm"
)
var ErrNotFound = gorm.ErrRecordNotFound
2024-03-22 19:43:02 +00:00
var _ MetadataService = (*MetadataServiceDefault)(nil)
2024-02-17 02:56:38 +00:00
type UploadMetadata struct {
ID uint `json:"upload_id"`
UserID uint `json:"user_id"`
2024-02-17 02:56:38 +00:00
Hash []byte `json:"hash"`
MimeType string `json:"mime_type"`
2024-02-17 02:56:38 +00:00
Protocol string `json:"protocol"`
UploaderIP string `json:"uploader_ip"`
2024-02-17 02:56:38 +00:00
Size uint64 `json:"size"`
Created time.Time `json:"created"`
}
func (u UploadMetadata) IsEmpty() bool {
if u.UserID != 0 || u.MimeType != "" || u.Protocol != "" || u.UploaderIP != "" || u.Size != 0 {
return false
}
if !u.Created.IsZero() {
return false
}
if len(u.Hash) != 0 {
return false
}
return true
}
var Module = fx.Module("metadata",
fx.Provide(
2024-02-17 08:21:27 +00:00
fx.Annotate(
NewMetadataService,
fx.As(new(MetadataService)),
),
2024-02-17 02:56:38 +00:00
),
)
type MetadataService interface {
SaveUpload(ctx context.Context, metadata UploadMetadata, skipExisting bool) error
2024-02-17 02:56:38 +00:00
GetUpload(ctx context.Context, objectHash []byte) (UploadMetadata, error)
DeleteUpload(ctx context.Context, objectHash []byte) error
}
type MetadataServiceDefault struct {
db *gorm.DB
}
type MetadataServiceParams struct {
fx.In
Db *gorm.DB
}
func NewMetadataService(params MetadataServiceParams) *MetadataServiceDefault {
return &MetadataServiceDefault{
db: params.Db,
}
}
func (m *MetadataServiceDefault) SaveUpload(ctx context.Context, metadata UploadMetadata, skipExisting bool) error {
2024-02-17 02:56:38 +00:00
var upload models.Upload
upload.Hash = metadata.Hash
2024-02-17 02:56:38 +00:00
ret := m.db.WithContext(ctx).Model(&models.Upload{}).Where(&upload).First(&upload)
if ret.Error != nil {
2024-02-18 04:42:32 +00:00
if errors.Is(ret.Error, gorm.ErrRecordNotFound) {
return m.createUpload(ctx, metadata)
2024-02-17 02:56:38 +00:00
}
return ret.Error
}
if skipExisting {
return nil
}
changed := false
if upload.UserID != metadata.UserID {
upload.UserID = metadata.UserID
changed = true
}
if upload.MimeType != metadata.MimeType {
upload.MimeType = metadata.MimeType
changed = true
}
if upload.Protocol != metadata.Protocol {
upload.Protocol = metadata.Protocol
changed = true
2024-02-17 02:56:38 +00:00
}
if upload.UploaderIP != metadata.UploaderIP {
upload.UploaderIP = metadata.UploaderIP
changed = true
2024-02-17 02:56:38 +00:00
}
if upload.Size != metadata.Size {
upload.Size = metadata.Size
changed = true
}
if changed {
return m.db.Updates(&upload).Error
}
return nil
}
func (m *MetadataServiceDefault) createUpload(ctx context.Context, metadata UploadMetadata) error {
upload := models.Upload{
UserID: metadata.UserID,
Hash: metadata.Hash,
MimeType: metadata.MimeType,
Protocol: metadata.Protocol,
UploaderIP: metadata.UploaderIP,
Size: metadata.Size,
}
2024-02-17 02:56:38 +00:00
return m.db.WithContext(ctx).Create(&upload).Error
2024-02-17 02:56:38 +00:00
}
func (m *MetadataServiceDefault) GetUpload(ctx context.Context, objectHash []byte) (UploadMetadata, error) {
var upload models.Upload
upload.Hash = objectHash
2024-02-17 02:56:38 +00:00
ret := m.db.WithContext(ctx).Model(&models.Upload{}).Where(&upload).First(&upload)
if ret.Error != nil {
2024-02-29 14:55:07 +00:00
return UploadMetadata{}, ret.Error
2024-02-17 02:56:38 +00:00
}
return UploadMetadata{
ID: upload.ID,
2024-02-17 02:56:38 +00:00
UserID: upload.UserID,
Hash: upload.Hash,
2024-02-17 02:56:38 +00:00
MimeType: upload.MimeType,
Protocol: upload.Protocol,
UploaderIP: upload.UploaderIP,
Size: upload.Size,
}, nil
}
func (m *MetadataServiceDefault) DeleteUpload(ctx context.Context, objectHash []byte) error {
var upload models.Upload
upload.Hash = objectHash
2024-02-17 02:56:38 +00:00
ret := m.db.WithContext(ctx).Model(&models.Upload{}).Where(&upload).First(&upload)
if ret.Error != nil {
return ret.Error
}
return m.db.Delete(&upload).Error
}