2023-08-11 13:34:54 +02:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2023-08-15 14:51:54 +02:00
|
|
|
"strings"
|
2023-08-11 13:34:54 +02:00
|
|
|
"time"
|
|
|
|
"velvettear/dedupe/log"
|
|
|
|
"velvettear/dedupe/settings"
|
|
|
|
)
|
|
|
|
|
2023-08-15 14:51:54 +02:00
|
|
|
var Duplicates []string
|
|
|
|
|
2023-08-11 13:34:54 +02:00
|
|
|
var sourceFiles []string
|
|
|
|
var comparisonFiles []string
|
|
|
|
|
|
|
|
// exported function(s)
|
|
|
|
func Scan() {
|
|
|
|
timestamp := time.Now()
|
|
|
|
log.Info("scanning source directory...", settings.SourceDirectory)
|
|
|
|
filepath.WalkDir(settings.SourceDirectory, fillSourceFiles)
|
|
|
|
log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli())
|
|
|
|
|
|
|
|
timestamp = time.Now()
|
|
|
|
log.Info("scanning comparison directory...", settings.SourceDirectory)
|
|
|
|
filepath.WalkDir(settings.ComparisonDirectory, fillComparisonFiles)
|
|
|
|
log.InfoTimed("found "+strconv.Itoa(len(comparisonFiles))+" comparison files", timestamp.UnixMilli())
|
|
|
|
|
2023-08-15 14:51:54 +02:00
|
|
|
timestamp = time.Now()
|
|
|
|
log.Info("comparing files...")
|
2023-08-11 13:34:54 +02:00
|
|
|
for _, sourceFile := range sourceFiles {
|
|
|
|
log.Debug("checking file", sourceFile)
|
2023-08-15 14:51:54 +02:00
|
|
|
sourceFilePath := strings.Replace(strings.Replace(sourceFile, filepath.Ext(sourceFile), "", 1), settings.SourceDirectory, "", 1)
|
|
|
|
for _, comparisonFile := range comparisonFiles {
|
|
|
|
comparisonFilePath := strings.Replace(strings.Replace(comparisonFile, filepath.Ext(comparisonFile), "", 1), settings.ComparisonDirectory, "", 1)
|
|
|
|
if comparisonFilePath != sourceFilePath {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Debug("duplicate file found", sourceFile+" -> "+comparisonFile)
|
|
|
|
Duplicates = append(Duplicates, comparisonFile)
|
|
|
|
}
|
2023-08-11 13:34:54 +02:00
|
|
|
}
|
2023-08-15 14:51:54 +02:00
|
|
|
log.InfoTimed("found "+strconv.Itoa(len(Duplicates))+" duplicate files", timestamp.Local().UnixMilli())
|
2023-08-11 13:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// unexported function(s)
|
|
|
|
func fillSourceFiles(path string, dir fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dir.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sourceFiles = append(sourceFiles, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillComparisonFiles(path string, dir fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dir.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
comparisonFiles = append(comparisonFiles, path)
|
|
|
|
return nil
|
|
|
|
}
|