1
Fork 0

Improve database compatibility

- Make MediaType case insensitive when saved to database
- Null check exif migrations, this might solve #300
This commit is contained in:
viktorstrate 2021-04-13 21:39:07 +02:00
parent 7f546e122e
commit f8cd804c3d
No known key found for this signature in database
GPG Key ID: 3F855605109C1E8A
3 changed files with 44 additions and 1 deletions

View File

@ -72,6 +72,16 @@ func migrate_exif_fields_exposure(db *gorm.DB) error {
return tx.Model(&exifModel{}).Table("media_exif").Where("exposure LIKE '%/%'").FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error {
for _, result := range results {
if result.Exposure == nil {
continue
}
if *result.Exposure == "" {
result.Exposure = nil
continue
}
frac := strings.Split(*result.Exposure, "/")
if len(frac) != 2 {
return errors.Errorf("failed to convert exposure value (%s) expected format x/y", frac)
@ -147,6 +157,16 @@ func migrate_exif_fields_flash(db *gorm.DB) error {
return tx.Model(&exifModel{}).Table("media_exif").Where("flash IS NOT NULL").FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error {
for _, result := range results {
if result.Flash == nil {
continue
}
if *result.Flash == "" {
result.Flash = nil
continue
}
for index, name := range flashDescriptions {
if *result.Flash == name {
*result.Flash = fmt.Sprintf("%d", index)

View File

@ -42,6 +42,29 @@ func (m *Media) BeforeSave(tx *gorm.DB) error {
// Update path hash
m.PathHash = MD5Hash(m.Path)
// Save media type as lowercase for better compatibility
m.Type = MediaType(strings.ToLower(string(m.Type)))
return nil
}
func (m *Media) AfterFind(tx *gorm.DB) error {
// Convert lowercased media type back
lowercasedType := strings.ToLower(string(m.Type))
foundType := false
for _, t := range AllMediaType {
if strings.ToLower(string(m.Type)) == lowercasedType {
m.Type = t
foundType = true
break
}
}
if foundType == false {
return errors.New(fmt.Sprintf("Failed to parse media from DB: Invalid media type: %s", m.Type))
}
return nil
}

View File

@ -129,7 +129,7 @@ const PreviewMedia = ({ media, previewImage }: PreviewMediaProps) => {
return <PreviewVideo media={media} />
}
throw new Error('Unknown media type')
return <div>ERROR: Unknown media type: {media.type}</div>
}
const Name = styled.div`