|
|
|
@ -43,7 +43,7 @@ func parseArgs() (inputDirectory string, outputDirectory string) {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isEmpty(flag.Args()[0]) {
|
|
|
|
|
if isEmptyDir(flag.Args()[0]) {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: Input directory is empty: %s\n", os.Args[0], flag.Args()[0])
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
@ -52,15 +52,16 @@ func parseArgs() (inputDirectory string, outputDirectory string) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
|
name string
|
|
|
|
|
modTime time.Time
|
|
|
|
|
thumbnail string
|
|
|
|
|
name string
|
|
|
|
|
path string
|
|
|
|
|
modTime time.Time
|
|
|
|
|
exists bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type directory struct {
|
|
|
|
|
name string
|
|
|
|
|
path string
|
|
|
|
|
modTime time.Time
|
|
|
|
|
thumbnail string
|
|
|
|
|
subdirectories []directory
|
|
|
|
|
files []file
|
|
|
|
|
}
|
|
|
|
@ -71,7 +72,7 @@ func checkError(e error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isEmpty(directory string) (isEmpty bool) {
|
|
|
|
|
func isEmptyDir(directory string) (isEmpty bool) {
|
|
|
|
|
list, err := ioutil.ReadDir(directory)
|
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
|
@ -81,28 +82,81 @@ func isEmpty(directory string) (isEmpty bool) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func discoverAsIs(outputDirectory string) (asIs directory) {
|
|
|
|
|
asIs.name = filepath.Base(outputDirectory)
|
|
|
|
|
func isMediaFile(filename string) (isMedia bool) {
|
|
|
|
|
//TODO fix add strings.toLower() once goimport stops removing it
|
|
|
|
|
switch filepath.Ext(filename) {
|
|
|
|
|
case ".jpg", ".jpeg", ".heic", ".png", ".gif", ".tif":
|
|
|
|
|
return true
|
|
|
|
|
case ".cr2", ".raw", ".arw":
|
|
|
|
|
return true
|
|
|
|
|
case ".mp4", ".mov", ".3gp", ".avi", ".mts", ".m4v", ".mpg":
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list, err := ioutil.ReadDir(outputDirectory)
|
|
|
|
|
func recurseDirectory(thisDirectory string, relativeDirectory string) (root directory) {
|
|
|
|
|
root.name = filepath.Base(thisDirectory)
|
|
|
|
|
asIsStat, _ := os.Stat(thisDirectory)
|
|
|
|
|
root.modTime = asIsStat.ModTime()
|
|
|
|
|
root.path = relativeDirectory
|
|
|
|
|
|
|
|
|
|
list, err := ioutil.ReadDir(thisDirectory)
|
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
|
|
for _, entry := range list {
|
|
|
|
|
if entry.IsDir() {
|
|
|
|
|
asIs.subdirectories = append(asIs.subdirectories, directory{name: entry.Name(), modTime: entry.ModTime()})
|
|
|
|
|
if !isEmptyDir(filepath.Join(thisDirectory, entry.Name())) {
|
|
|
|
|
root.subdirectories = append(root.subdirectories, recurseDirectory(filepath.Join(thisDirectory, entry.Name()), filepath.Join(relativeDirectory, entry.Name())))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
asIs.files = append(asIs.files, file{name: entry.Name(), modTime: entry.ModTime()})
|
|
|
|
|
if isMediaFile(entry.Name()) {
|
|
|
|
|
root.files = append(root.files, file{name: entry.Name(), modTime: entry.ModTime(), path: filepath.Join(relativeDirectory, entry.Name()), exists: false})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func compareDirectories(source *directory, gallery *directory, changes *int) {
|
|
|
|
|
for i, inputFile := range source.files {
|
|
|
|
|
for j, outputFile := range gallery.files {
|
|
|
|
|
// TODO what if modtimes are exact same as expected
|
|
|
|
|
if inputFile.name == outputFile.name && outputFile.modTime.After(inputFile.modTime) {
|
|
|
|
|
source.files[i].exists = true
|
|
|
|
|
gallery.files[j].exists = true
|
|
|
|
|
*changes--
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k, inputDir := range source.subdirectories {
|
|
|
|
|
for l, outputDir := range gallery.subdirectories {
|
|
|
|
|
if inputDir.name == outputDir.name {
|
|
|
|
|
compareDirectories(&(gallery.subdirectories[l]), &(source.subdirectories[k]), changes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func countFiles(source directory, inputChanges int) (outputChanges int) {
|
|
|
|
|
outputChanges = inputChanges + len(source.files)
|
|
|
|
|
|
|
|
|
|
for _, dir := range source.subdirectories {
|
|
|
|
|
outputChanges = countFiles(dir, outputChanges)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (asIs)
|
|
|
|
|
//fmt.Println(root)
|
|
|
|
|
return outputChanges
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
var inputDirectory string
|
|
|
|
|
var outputDirectory string
|
|
|
|
|
var asIs directory
|
|
|
|
|
var gallery directory
|
|
|
|
|
var source directory
|
|
|
|
|
var changes int
|
|
|
|
|
|
|
|
|
|
outputDirectory, inputDirectory = parseArgs()
|
|
|
|
|
|
|
|
|
@ -112,6 +166,9 @@ func main() {
|
|
|
|
|
fmt.Println("Creating static gallery in:", outputDirectory)
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
|
|
asIs = discoverAsIs(outputDirectory)
|
|
|
|
|
fmt.Println("As-is: ", asIs)
|
|
|
|
|
gallery = recurseDirectory(outputDirectory, "")
|
|
|
|
|
source = recurseDirectory(inputDirectory, "")
|
|
|
|
|
changes = countFiles(source, 0)
|
|
|
|
|
compareDirectories(&source, &gallery, &changes)
|
|
|
|
|
fmt.Println(changes, "new pictures to update")
|
|
|
|
|
}
|
|
|
|
|