Skip to content

Commit

Permalink
Add file management and extraction methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Themimitoof committed Mar 15, 2022
1 parent 59294bf commit 86559ed
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/extract.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ For more information, please consult: https://github.com/themimitoof/cambak.`,
},
Run: func(cmd *cobra.Command, args []string) {
conf := superchargeConf(cmd, args)

// Collect all files
files := cb_files.CollectFiles(conf)
allFiles := files.Pictures
allFiles = append(allFiles, files.RAW...)
allFiles = append(allFiles, files.Movies...)

totalFiles := len(allFiles)
remainingFiles := totalFiles

for _, fl := range allFiles {
remainingFiles--
fmt.Printf("Extract '%s' file. (Remaining %d/%d)\n", fl.Path, remainingFiles, totalFiles)

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

Expand Down
Empty file modified cmd/root.go
100644 → 100755
Empty file.
71 changes: 71 additions & 0 deletions files/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package files

import (
"fmt"
"io/fs"
"io/ioutil"
"os"

"github.com/themimitoof/cambak/config"
)

type FileCategory int8

const (
FC_PICTURE FileCategory = iota
FC_RAW
FC_MOVIE
)

type File struct {
Path string
File fs.FileInfo
Category FileCategory
}

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 {
fmt.Printf(errMsg, file.Path, err)
return err
}
} else {
// Copy the file instead of moving it
fl, err := ioutil.ReadFile(file.Path)
if err != nil {
fmt.Printf(errMsg, file.Path, err)
return err
}

if err = ioutil.WriteFile(fileDest, fl, 0755); err != nil {
fmt.Printf(errMsg, file.Path, err)
return err
}
}

return nil
}

func (file File) PrepareFileDestinationFolder(conf config.Configuration) (string, error) {
destinationPath := file.GenerateDestinationPath(conf)

if err := os.MkdirAll(destinationPath, 0755); err != nil {
fmt.Printf(
"Unable to create the destination folder '%s' for file '%s'. Err: %s\n",
destinationPath,
file.File.Name(),
err,
)

return "", err
}
return destinationPath, nil
}
15 changes: 15 additions & 0 deletions files/file_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package files

var picturesExtensions = []string{
"jpg",
"jpeg",
}

var rawExtensions = []string{
"dng",
"arw",
}

var moviesExtensions = []string{
"mp4",
}
76 changes: 76 additions & 0 deletions files/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package files

import (
"fmt"
"io/fs"
"os"
"strings"

"github.com/themimitoof/cambak/config"
)

type Files struct {
Pictures []File
RAW []File
Movies []File
}

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

files.Pictures = append(files.Pictures, ListFiles(source, picturesExtensions, FC_PICTURE)...)
files.RAW = append(files.Pictures, ListFiles(source, rawExtensions, FC_RAW)...)
files.Movies = append(files.Pictures, ListFiles(source, moviesExtensions, FC_MOVIE)...)

return files
}

func ListFiles(sourcePath string, extensions []string, category FileCategory) []File {
var files []File
fileSystem := os.DirFS(sourcePath)

fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
// Skip the current folder
if path == "." {
return nil
}

if err != nil {
fmt.Printf("Unable to manage %s (err: %s)", path, err)
return nil
}

path = fmt.Sprintf("%s/%s", sourcePath, path)

if d.IsDir() {
files = append(files, ListFiles(path, extensions, category)...)
} else if d.Type().IsRegular() {
fileInfo, _ := d.Info()
splittedFileName := strings.SplitAfter(fileInfo.Name(), ".")
ext := strings.ToLower(
splittedFileName[len(splittedFileName)-1],
)

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,
},
)
break
}
}
}

return nil
})

return files
}
46 changes: 46 additions & 0 deletions files/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package files

import (
"fmt"
"strings"

"github.com/themimitoof/cambak/config"
)

// Returns a string with the destination path of the given file.
//
// List of available verbs:
// - %y: Year
// - %m: Month
// - %d: Day
// - %n: Camera name
// - %t: Media type (Pictures, RAW, Movies)
func (file File) GenerateDestinationPath(conf config.Configuration) string {
destPath := conf.Extract.DestinationPath
format := conf.Extract.DestinationFormat

// TODO: Implement this part to use the created date.
createDate := file.File.ModTime()

format = strings.ReplaceAll(format, "%y", fmt.Sprintf("%d", createDate.Year()))
format = strings.ReplaceAll(format, "%m", fmt.Sprintf("%02d", createDate.Month()))
format = strings.ReplaceAll(format, "%d", fmt.Sprintf("%02d", createDate.Day()))
format = strings.ReplaceAll(format, "%n", conf.Extract.CameraName)

switch file.Category {
case FC_PICTURE:
format = strings.ReplaceAll(format, "%t", "Pictures")
case FC_RAW:
format = strings.ReplaceAll(format, "%t", "RAW")
case FC_MOVIE:
format = strings.ReplaceAll(format, "%t", "Movies")
}

if destPath[len(destPath)-1] != '/' {
destPath += "/"
}

destPath += format

return destPath
}

0 comments on commit 86559ed

Please sign in to comment.