linstrom/storage/mediaFile.go

95 lines
3 KiB
Go
Raw Permalink Normal View History

2024-05-31 09:54:39 +00:00
package storage
import (
"time"
"github.com/rs/zerolog/log"
2024-12-18 14:24:56 +00:00
"git.mstar.dev/mstar/linstrom/util"
2024-05-31 09:54:39 +00:00
"gorm.io/gorm"
)
2024-09-13 07:18:05 +00:00
// MediaMetadata contains metadata about some media
// Metadata includes whether it's a remote file or not, what the name is,
// the MIME type, and an identifier pointing to its location
type MediaMetadata struct {
2024-08-28 15:20:38 +00:00
ID string `gorm:"primarykey"` // The unique ID of this media file
CreatedAt time.Time // When this entry was created
UpdatedAt time.Time // When this entry was last updated
2024-06-06 11:54:50 +00:00
// When this entry was deleted (for soft deletions)
// Soft delete means that this entry still exists in the db, but gorm won't include it anymore unless specifically told to
// If not null, this entry is marked as deleted
2024-05-31 09:54:39 +00:00
DeletedAt gorm.DeletedAt `gorm:"index"`
OwnedBy string // Account id this media belongs to
2024-05-31 09:54:39 +00:00
Remote bool // whether the attachment is a remote one
2024-11-21 13:45:25 +00:00
// Where the media is stored. Url
2024-09-13 07:18:05 +00:00
Location string
Type string // What media type this is following mime types, eg image/png
2024-11-21 13:45:25 +00:00
// Name of the file
// Could be <emote-name>.png, <server-name>.webp for example. Or the name the file was uploaded with
2024-09-12 14:57:53 +00:00
Name string
// Alternative description of the media file's content
AltText string
// Whether the media is to be blurred by default
Blurred bool
2024-05-31 09:54:39 +00:00
}
2024-09-13 07:18:05 +00:00
func (s *Storage) NewMediaMetadata(
ownerId, location, mediaType, name string,
) (*MediaMetadata, error) {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 07:18:05 +00:00
newMedia := MediaMetadata{
OwnedBy: ownerId,
2024-09-13 09:58:29 +00:00
Location: location,
2024-09-13 07:18:05 +00:00
Name: name,
2024-09-13 09:58:29 +00:00
Type: mediaType,
2024-09-13 07:18:05 +00:00
}
s.db.Create(&newMedia)
return nil, nil
}
2024-09-13 09:58:29 +00:00
func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, error) {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
notes := []MediaMetadata{}
err := s.db.Where("name LIKE %?%", name).Find(notes).Error
if err != nil {
return nil, err
}
return notes, nil
}
func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
media := MediaMetadata{ID: id}
err := s.db.First(&media).Error
if err != nil {
return nil, err
}
return &media, nil
}
func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMetadata, error) {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
data := []MediaMetadata{}
if err := s.db.Where("location LIKE %?%", location).Find(data).Error; err != nil {
return nil, err
}
return data, nil
}
func (s *Storage) DeleteMediaMetadataById(id string) error {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
return s.db.Delete(MediaMetadata{ID: id}).Error
}
func (s *Storage) DeleteMediaMetadataByFuzzyLocation(location string) error {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
var tmp MediaMetadata
return s.db.Where("location LIKE %?%", location).Delete(&tmp).Error
}
func (s *Storage) DeleteMediaMetadataByFuzzyName(name string) error {
defer util.Untrace(util.Trace(&log.Logger))
2024-09-13 09:58:29 +00:00
var tmp MediaMetadata
return s.db.Where("name LIKE %?%", name).Delete(&tmp).Error
}