-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file management and extraction methods
- Loading branch information
1 parent
59294bf
commit 86559ed
Showing
6 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |