photoprism/importer.go

119 lines
2.7 KiB
Go
Raw Normal View History

2018-02-04 17:34:07 +01:00
package photoprism
import (
"path/filepath"
"os"
"log"
"fmt"
"github.com/pkg/errors"
"path"
"sort"
"strings"
2018-02-04 17:34:07 +01:00
)
type Importer struct {
originalsPath string
removeDotFiles bool
removeExistingFiles bool
removeEmptyDirectories bool
2018-02-04 17:34:07 +01:00
}
func NewImporter(originalsPath string) *Importer {
2018-02-04 17:34:07 +01:00
instance := &Importer{
originalsPath: originalsPath,
removeDotFiles: true,
removeExistingFiles: true,
removeEmptyDirectories: true,
2018-02-04 17:34:07 +01:00
}
return instance
}
func (i *Importer) ImportPhotosFromDirectory(importPath string) {
var directories []string
2018-02-04 17:34:07 +01:00
err := filepath.Walk(importPath, func(filename string, fileInfo os.FileInfo, err error) error {
2018-02-04 17:34:07 +01:00
if err != nil {
// log.Print(err.Error())
2018-02-04 17:34:07 +01:00
return nil
}
if fileInfo.IsDir() {
if filename != importPath {
directories = append(directories, filename)
}
2018-02-04 17:34:07 +01:00
return nil
}
if i.removeDotFiles && strings.HasPrefix(filepath.Base(filename), ".") {
os.Remove(filename)
2018-02-04 17:34:07 +01:00
return nil
}
mediaFile := NewMediaFile(filename)
2018-02-04 17:34:07 +01:00
if !mediaFile.Exists() || !mediaFile.IsPhoto() {
2018-02-04 17:34:07 +01:00
return nil
}
relatedFiles, masterFile, _ := mediaFile.GetRelatedFiles()
2018-02-04 17:34:07 +01:00
for _, relatedMediaFile := range relatedFiles {
if destinationFilename, err := i.GetDestinationFilename(masterFile, relatedMediaFile); err == nil {
2018-02-04 17:34:07 +01:00
os.MkdirAll(path.Dir(destinationFilename), os.ModePerm)
log.Printf("Moving file %s to %s", relatedMediaFile.GetFilename(), destinationFilename)
2018-02-04 17:34:07 +01:00
relatedMediaFile.Move(destinationFilename)
} else if i.removeExistingFiles {
relatedMediaFile.Remove()
log.Printf("Deleted %s (already exists)", relatedMediaFile.GetFilename())
2018-02-04 17:34:07 +01:00
}
}
// mediaFile.Move(i.originalsPath)
2018-02-04 17:34:07 +01:00
return nil
})
sort.Slice(directories, func(i, j int) bool {
return len(directories[i]) > len(directories[j])
})
if i.removeEmptyDirectories {
// Remove empty directories from import path
for _, directory := range directories {
if directoryIsEmpty(directory) {
os.Remove(directory)
log.Printf("Deleted empty directory %s", directory)
}
}
}
2018-02-04 17:34:07 +01:00
if err != nil {
log.Print(err.Error())
}
}
func (i *Importer) GetDestinationFilename(masterFile *MediaFile, mediaFile *MediaFile) (string, error) {
canonicalName := masterFile.GetCanonicalName()
2018-02-04 17:34:07 +01:00
fileExtension := mediaFile.GetExtension()
dateCreated := masterFile.GetDateCreated()
2018-02-04 17:34:07 +01:00
// Mon Jan 2 15:04:05 -0700 MST 2006
pathName := i.originalsPath + "/" + dateCreated.UTC().Format("2006/01")
2018-02-04 17:34:07 +01:00
iteration := 1
2018-02-04 17:34:07 +01:00
result := pathName + "/" + canonicalName + fileExtension
2018-02-04 17:34:07 +01:00
for fileExists(result) {
if mediaFile.GetHash() == fileHash(result) {
2018-02-04 17:34:07 +01:00
return result, errors.New("File already exists")
}
iteration++
2018-02-04 17:34:07 +01:00
result = pathName + "/" + canonicalName + "_" + fmt.Sprintf("V%d", iteration) + fileExtension
}
2018-02-04 17:34:07 +01:00
return result, nil
}