2020-07-20 19:48:31 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-07-20 19:48:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Duplicates []Duplicate
|
|
|
|
type DuplicatesMap map[string]Duplicate
|
|
|
|
|
|
|
|
// Duplicate represents an exact file duplicate.
|
|
|
|
type Duplicate struct {
|
2020-12-15 20:14:06 +01:00
|
|
|
FileName string `gorm:"type:VARBINARY(755);primary_key;" json:"Name" yaml:"Name"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FileRoot string `gorm:"type:VARBINARY(16);primary_key;default:'/';" json:"Root" yaml:"Root,omitempty"`
|
|
|
|
FileHash string `gorm:"type:VARBINARY(128);default:'';index" json:"Hash" yaml:"Hash,omitempty"`
|
2020-07-20 19:48:31 +02:00
|
|
|
FileSize int64 `json:"Size" yaml:"Size,omitempty"`
|
|
|
|
ModTime int64 `json:"ModTime" yaml:"-"`
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:11:08 +01:00
|
|
|
// AddDuplicate adds a duplicate.
|
2020-07-20 19:48:31 +02:00
|
|
|
func AddDuplicate(fileName, fileRoot, fileHash string, fileSize, modTime int64) error {
|
|
|
|
if fileName == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate name must not be empty (add)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if fileHash == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate hash must not be empty (add)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if modTime == 0 {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate mod time must not be empty (add)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if fileRoot == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate root must not be empty (add)")
|
2020-07-20 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
duplicate := &Duplicate{
|
|
|
|
FileName: fileName,
|
|
|
|
FileRoot: fileRoot,
|
|
|
|
FileHash: fileHash,
|
|
|
|
FileSize: fileSize,
|
|
|
|
ModTime: modTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := duplicate.Create(); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if err := duplicate.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:11:08 +01:00
|
|
|
// PurgeDuplicate deletes a duplicate.
|
|
|
|
func PurgeDuplicate(fileName, fileRoot string) error {
|
2020-11-15 10:31:46 +01:00
|
|
|
if fileName == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate name must not be empty (purge)")
|
2020-11-15 10:31:46 +01:00
|
|
|
} else if fileRoot == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate root must not be empty (purge)")
|
2020-11-15 10:31:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(Duplicate{}, "file_name = ? AND file_root = ?", fileName, fileRoot).Error; err != nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("duplicate: %s in %s (purge)", err, sanitize.Log(fileName))
|
2020-11-15 10:31:46 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:11:08 +01:00
|
|
|
// Purge deletes a duplicate.
|
|
|
|
func (m *Duplicate) Purge() error {
|
|
|
|
return PurgeDuplicate(m.FileName, m.FileRoot)
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:31:46 +01:00
|
|
|
// Find returns a duplicate from the database.
|
2020-07-20 19:48:31 +02:00
|
|
|
func (m *Duplicate) Find() error {
|
2020-11-15 10:31:46 +01:00
|
|
|
return UnscopedDb().First(m, "file_name = ? AND file_root = ?", m.FileName, m.FileRoot).Error
|
2020-07-20 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Duplicate) Create() error {
|
|
|
|
if m.FileName == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate name must not be empty (create)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.FileHash == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate hash must not be empty (create)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.ModTime == 0 {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate mod time must not be empty (create)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.FileRoot == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate root must not be empty (create)")
|
2020-07-20 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return UnscopedDb().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Saves the duplicates in the database.
|
|
|
|
func (m *Duplicate) Save() error {
|
|
|
|
if m.FileName == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate name must not be empty (save)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.FileHash == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate hash must not be empty (save)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.ModTime == 0 {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate mod time must not be empty (save)")
|
2020-07-20 19:48:31 +02:00
|
|
|
} else if m.FileRoot == "" {
|
2020-12-27 13:11:08 +01:00
|
|
|
return fmt.Errorf("duplicate root must not be empty (save)")
|
2020-07-20 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Save(m).Error; err != nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("duplicate: %s in %s (save)", err, sanitize.Log(m.FileName))
|
2020-07-20 19:48:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|