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