Added more stuff
This commit is contained in:
43
tasks/clean-mov-files.go
Normal file
43
tasks/clean-mov-files.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"varia-go/shared"
|
||||
)
|
||||
|
||||
type CleanMovFilesTask struct{}
|
||||
|
||||
func (t *CleanMovFilesTask) Run(folderPath string) error {
|
||||
return filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if the file has a .mov extension
|
||||
if filepath.Ext(info.Name()) == ".mov" {
|
||||
baseName := shared.FileNameWithoutExt(info.Name())
|
||||
heicPath := filepath.Join(filepath.Dir(path), baseName+".heic")
|
||||
jpgPath := filepath.Join(filepath.Dir(path), baseName+".jpg")
|
||||
|
||||
// Check if corresponding .heic or .jpg file exists
|
||||
heicExists, err := shared.FileExists(heicPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jpgExists, err := shared.FileExists(jpgPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(baseName)
|
||||
if heicExists || jpgExists {
|
||||
fmt.Printf("Sending to Recycle Bin: %s\n", path)
|
||||
if err := shared.MoveToTrash(path); err != nil {
|
||||
return fmt.Errorf("failed to move to Recycle Bin: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
96
tasks/image-sort.go
Normal file
96
tasks/image-sort.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/rwcarlsen/goexif/exif"
|
||||
)
|
||||
|
||||
type ImageSortTask struct{}
|
||||
|
||||
func (t *ImageSortTask) Run(folderPath string) error {
|
||||
return filepath.WalkDir(folderPath, func(path string, d fs.DirEntry, err error) error {
|
||||
return visit(folderPath, path, d, err)
|
||||
})
|
||||
}
|
||||
|
||||
func visit(basePath, path string, di fs.DirEntry, err1 error) error {
|
||||
fmt.Printf("Visited: %s\n", path)
|
||||
|
||||
if di.IsDir() {
|
||||
if path == basePath {
|
||||
return nil
|
||||
}
|
||||
return fs.SkipDir
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
x, err := exif.Decode(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dateTag, err := x.Get(exif.DateTime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dateVal, err := dateTag.StringVal()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dateVal == "0000:00:00 00:00:00" {
|
||||
return nil
|
||||
}
|
||||
date, err := time.Parse("2006:01:02 15:04:05", dateVal)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Close()
|
||||
|
||||
newFolderName := fmt.Sprintf("%04d.%02d", date.Year(), date.Month())
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(basePath, newFolderName), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Rename(path, filepath.Join(basePath, newFolderName, filepath.Base(path))); err != nil {
|
||||
return err
|
||||
}
|
||||
//copy(path, filepath.Join(basePath, newFolderName, filepath.Base(path)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copy(src, dst string) (int64, error) {
|
||||
sourceFileStat, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if !sourceFileStat.Mode().IsRegular() {
|
||||
return 0, fmt.Errorf("%s is not a regular file", src)
|
||||
}
|
||||
|
||||
source, err := os.Open(src)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
destination, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer destination.Close()
|
||||
nBytes, err := io.Copy(destination, source)
|
||||
return nBytes, err
|
||||
}
|
||||
Reference in New Issue
Block a user