Skip to content

Commit

Permalink
Improve existing detection and skip files
Browse files Browse the repository at this point in the history
  • Loading branch information
Themimitoof committed Mar 26, 2022
1 parent 219ea94 commit fd861d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
19 changes: 11 additions & 8 deletions cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,39 @@ For more information, please consult: https://github.com/themimitoof/cambak.`,
allFiles = append(allFiles, files.RAW...)
allFiles = append(allFiles, files.Movies...)

totalFiles := len(allFiles)
remainingFiles := totalFiles

finalMsg := fmt.Sprintf(
"✅ %d files collected (%d pictures, %d RAWs, %d movies)",
totalFiles,
files.TotalFiles,
len(files.Pictures),
len(files.RAW),
len(files.Movies),
)
if conf.Extract.DestinationConflict == cb_config.DEST_CONFLICT_SKIP {
s.FinalMSG = color.GreenString("%s. %d files skipped.\n", finalMsg, 0)
s.FinalMSG = color.GreenString("%s. %d files skipped.\n", finalMsg, files.SkippedFiles)
} else {
s.FinalMSG = color.GreenString(finalMsg + "\n")
}

s.Stop()

// Exit the program if there is no files to copy
if files.TotalFiles == 0 {
color.Green("😴 There is no files to copy, please let me sleep.")
os.Exit(0)
}

// Copying files
color.Yellow("Copying files...")
bar := progressbar.Default(int64(totalFiles))
bar := progressbar.Default(int64(files.TotalFiles))
for _, fl := range allFiles {
remainingFiles--

if !conf.Extract.DryRunMode {
destPath, _ := fl.PrepareFileDestinationFolder(conf)
fl.ExtractFile(conf, destPath)
}
bar.Add(1)
}

color.Green("✨ All files have been copied!")
},
}

Expand Down
4 changes: 0 additions & 4 deletions files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func (file File) ExtractFile(conf config.Configuration, dest string) error {
errMsg := "Unable to copy file '%s' to his destination folder. Err: %s\n"
fileDest := fmt.Sprintf("%s/%s", dest, file.File.Name())

if _, err := os.Stat(fileDest); err != nil && conf.Extract.DestinationConflict == "skip" {
return nil
}

if conf.Extract.CleanAfterCopy {
// Move the file
if err := os.Rename(file.Path, fileDest); err != nil {
Expand Down
52 changes: 34 additions & 18 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,43 @@ import (
)

type Files struct {
Pictures []File
RAW []File
Movies []File
Pictures []File
RAW []File
Movies []File
TotalFiles uint
SkippedFiles uint
}

func CollectFiles(conf config.Configuration) Files {
files := Files{}
source := conf.Extract.SourcePath

if conf.Extract.ExtractPictures {
files.Pictures = append(files.Pictures, ListFiles(source, picturesExtensions, FC_PICTURE)...)
pictures, ignoredPictures := ListFiles(conf, source, picturesExtensions, FC_PICTURE)
files.Pictures = append(files.Pictures, pictures...)
files.SkippedFiles += ignoredPictures
}

if conf.Extract.ExtractRaws {
files.RAW = append(files.RAW, ListFiles(source, rawExtensions, FC_RAW)...)
raws, ignoredRaws := ListFiles(conf, source, rawExtensions, FC_RAW)
files.RAW = append(files.RAW, raws...)
files.SkippedFiles += ignoredRaws
}

if conf.Extract.ExtractMovies {
files.Movies = append(files.Movies, ListFiles(source, moviesExtensions, FC_MOVIE)...)
movies, ignoredMovies := ListFiles(conf, source, moviesExtensions, FC_MOVIE)
files.Movies = append(files.Movies, movies...)
files.SkippedFiles += ignoredMovies
}

files.TotalFiles = uint(len(files.Pictures) + len(files.RAW) + len(files.Movies))
return files
}

func ListFiles(sourcePath string, extensions []string, category FileCategory) []File {
func ListFiles(conf config.Configuration, sourcePath string, extensions []string, category FileCategory) ([]File, uint) {
var files []File
fileSystem := os.DirFS(sourcePath)
var skippedFiles uint = 0

fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
// Skip the current folder
Expand Down Expand Up @@ -66,16 +76,22 @@ func ListFiles(sourcePath string, extensions []string, category FileCategory) []

for _, fileExt := range extensions {
if ext == fileExt {
// TODO: Add a check if the file already exists or not.

files = append(
files,
File{
File: fileInfo,
Path: path,
Category: category,
},
)
file := File{
File: fileInfo,
Path: path,
Category: category,
}

// Check if the file already exists or not
fileDest := fmt.Sprintf("%s/%s", file.GenerateDestinationPath(conf), fileInfo.Name())

if _, err := os.Open(fileDest); err == nil && conf.Extract.DestinationConflict == config.DEST_CONFLICT_SKIP {
skippedFiles++
return nil
}

// Append the file to the list of files
files = append(files, file)
break
}
}
Expand All @@ -84,5 +100,5 @@ func ListFiles(sourcePath string, extensions []string, category FileCategory) []
return nil
})

return files
return files, skippedFiles
}

0 comments on commit fd861d0

Please sign in to comment.