archiver/FileArchiver.go

107 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-09-26 11:33:03 +02:00
package archiver
import (
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
"time"
)
const _YEARMONTHONLY = "200601"
const ArchiveFolder = "archive"
const ErrorFolder = "error"
var logger = logrus.WithField("package", "archiver")
func NewFileArchiver(conf *Config) func(string, error) {
return func(f string, archiveErr error) {
var err error = nil
if archiveErr != nil {
err = moveToError(conf, f, archiveErr)
} else {
err = moveToArchive(conf, f)
}
if err != nil {
logger.Error(err)
}
}
}
func extractGlobbedDir(rootPath string, filePath string) string {
if !strings.Contains(rootPath, "*") {
return ""
}
fileDir := filepath.Dir(filePath)
globRoot := getGlobRoot(rootPath)
globbedDir, err := filepath.Rel(globRoot, fileDir)
if err != nil {
logger.
WithField("rootPath", rootPath).
WithField("globRoot", globRoot).
WithField("fileDir", fileDir).
Error(err)
return ""
}
return globbedDir
}
func getDestFolder(conf *Config, archiveFolder string) string {
var destFolder string
if len(conf.ArchiveFolder) > 0 {
destFolder = filepath.Join(conf.ArchiveFolder)
} else {
destFolder = filepath.Join(conf.Folder)
destFolder = getGlobRoot(destFolder)
}
return filepath.Join(destFolder, archiveFolder)
}
func moveToArchive(conf *Config, filePath string) error {
baseName := filepath.Base(filePath)
dateString := time.Now().Format(_YEARMONTHONLY)
destFolder := getDestFolder(conf, ArchiveFolder)
globDir := extractGlobbedDir(conf.Folder, filePath)
if globDir != "" {
destFolder = filepath.Join(destFolder, globDir)
}
destFolder = filepath.Join(destFolder, dateString)
err := os.MkdirAll(destFolder, os.ModePerm)
if err != nil {
return err
}
err = os.Rename(filePath, filepath.Join(destFolder, baseName))
if err != nil {
return err
}
return nil
}
func moveToError(conf *Config, filePath string, archiveErr error) error {
baseName := filepath.Base(filePath)
destFolder := getDestFolder(conf, ErrorFolder)
globDir := extractGlobbedDir(conf.Folder, filePath)
if globDir != "" {
destFolder = filepath.Join(destFolder, globDir)
}
err := os.MkdirAll(destFolder, os.ModePerm)
if err != nil {
return err
}
err = writeErrorFile(conf, filePath, archiveErr)
if err != nil {
return err
}
err = os.Rename(filePath, filepath.Join(destFolder, baseName))
if err != nil {
return err
}
2024-09-26 11:33:03 +02:00
return nil
}