Import: Save the original filenames of related files (#2623)
This commit is contained in:
parent
5888824095
commit
90567072cd
2 changed files with 84 additions and 1 deletions
|
@ -23,6 +23,7 @@ type ImportJob struct {
|
|||
func ImportWorker(jobs <-chan ImportJob) {
|
||||
for job := range jobs {
|
||||
var destMainFileName string
|
||||
relatedOriginalNames := make(map[string]string)
|
||||
|
||||
o := job.IndexOpt
|
||||
imp := job.Imp
|
||||
|
@ -60,6 +61,9 @@ func ImportWorker(jobs <-chan ImportJob) {
|
|||
if destFileName, err := imp.DestinationFilename(related.Main, f); err == nil {
|
||||
destDir := filepath.Dir(destFileName)
|
||||
|
||||
// Keep original name of related files after they are renamed, so they are indexed with the original name.
|
||||
relatedOriginalNames[destFileName] = relFileName
|
||||
|
||||
if fs.PathExists(destDir) {
|
||||
// Do nothing.
|
||||
} else if err := os.MkdirAll(destDir, os.ModePerm); err != nil {
|
||||
|
@ -232,7 +236,7 @@ func ImportWorker(jobs <-chan ImportJob) {
|
|||
}
|
||||
|
||||
// Index related MediaFile.
|
||||
res := ind.MediaFile(f, o, "", photoUID)
|
||||
res := ind.MediaFile(f, o, relatedOriginalNames[f.FileName()], photoUID)
|
||||
|
||||
// Save file error.
|
||||
if fileUid, err := res.FileError(); err != nil {
|
||||
|
|
79
internal/photoprism/import_worker_test.go
Normal file
79
internal/photoprism/import_worker_test.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
package photoprism
|
||||
|
||||
import (
|
||||
"github.com/photoprism/photoprism/internal/classify"
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
"github.com/photoprism/photoprism/internal/face"
|
||||
"github.com/photoprism/photoprism/internal/nsfw"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImportWorker_OriginalFileNames(t *testing.T) {
|
||||
conf := config.TestConfig()
|
||||
|
||||
conf.InitializeTestData(t)
|
||||
|
||||
tf := classify.New(conf.AssetsPath(), conf.DisableTensorFlow())
|
||||
nd := nsfw.New(conf.NSFWModelPath())
|
||||
fn := face.NewNet(conf.FaceNetModelPath(), "", conf.DisableTensorFlow())
|
||||
convert := NewConvert(conf)
|
||||
ind := NewIndex(conf, tf, nd, fn, convert, NewFiles(), NewPhotos())
|
||||
imp := &Import{conf, ind, convert}
|
||||
|
||||
mediaFileName := conf.ExamplesPath() + "/beach_sand.jpg"
|
||||
mediaFile, err := NewMediaFile(mediaFileName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mediaFileName2 := conf.ExamplesPath() + "/beach_wood.jpg"
|
||||
mediaFile2, err2 := NewMediaFile(mediaFileName2)
|
||||
if err2 != nil {
|
||||
t.Fatal(err2)
|
||||
}
|
||||
mediaFileName3 := conf.ExamplesPath() + "/beach_colorfilter.jpg"
|
||||
mediaFile3, err3 := NewMediaFile(mediaFileName3)
|
||||
if err3 != nil {
|
||||
t.Fatal(err3)
|
||||
}
|
||||
relatedFiles := RelatedFiles{
|
||||
Files: MediaFiles{mediaFile, mediaFile2, mediaFile3},
|
||||
Main: mediaFile,
|
||||
}
|
||||
|
||||
jobs := make(chan ImportJob)
|
||||
done := make(chan bool)
|
||||
|
||||
go func() {
|
||||
ImportWorker(jobs)
|
||||
done <- true
|
||||
}()
|
||||
|
||||
jobs <- ImportJob{
|
||||
FileName: mediaFile.FileName(),
|
||||
Related: relatedFiles,
|
||||
IndexOpt: IndexOptionsAll(),
|
||||
ImportOpt: ImportOptionsCopy(conf.ImportPath()),
|
||||
Imp: imp,
|
||||
}
|
||||
|
||||
// Wait for job to finish.
|
||||
close(jobs)
|
||||
<-done
|
||||
|
||||
var file entity.File
|
||||
res := entity.UnscopedDb().First(&file, "original_name = ?", mediaFileName)
|
||||
assert.Nil(t, res.Error)
|
||||
assert.Equal(t, file.OriginalName, mediaFileName)
|
||||
|
||||
var file2 entity.File
|
||||
res = entity.UnscopedDb().First(&file2, "original_name = ?", mediaFileName2)
|
||||
assert.Nil(t, res.Error)
|
||||
assert.Equal(t, file2.OriginalName, mediaFileName2)
|
||||
|
||||
var file3 entity.File
|
||||
res = entity.UnscopedDb().First(&file3, "original_name = ?", mediaFileName3)
|
||||
assert.Nil(t, res.Error)
|
||||
assert.Equal(t, file3.OriginalName, mediaFileName3)
|
||||
}
|
Loading…
Reference in a new issue