68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package files
|
|
|
|
import (
|
|
"io/fs"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"velvettear/dedupe/log"
|
|
"velvettear/dedupe/settings"
|
|
)
|
|
|
|
var Duplicates []string
|
|
|
|
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())
|
|
|
|
timestamp = time.Now()
|
|
log.Info("comparing files...")
|
|
for _, sourceFile := range sourceFiles {
|
|
log.Debug("checking file", sourceFile)
|
|
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)
|
|
}
|
|
}
|
|
log.InfoTimed("found "+strconv.Itoa(len(Duplicates))+" duplicate files", timestamp.Local().UnixMilli())
|
|
}
|
|
|
|
// 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
|
|
}
|