86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"alfred/pkg/log"
|
|
"go.uber.org/zap"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func DeleteFileFromLocal(uuidSessionId string, events *[]string) {
|
|
//deleting files from local
|
|
_ = os.RemoveAll(filepath.Join(TempDestinationFolder, uuidSessionId))
|
|
for _, event := range *events {
|
|
eventCopy := event
|
|
_ = os.Remove(filepath.Join(TempDestinationFolder, eventCopy+ZipExtension.String()))
|
|
}
|
|
}
|
|
|
|
func DeleteFileFromLocalWithExtension(uuidSessionId string, events *[]string, fileTypeExtension string) {
|
|
_ = os.RemoveAll(filepath.Join(TempDestinationFolder, uuidSessionId))
|
|
for _, event := range *events {
|
|
eventCopy := event
|
|
_ = os.Remove(filepath.Join(TempDestinationFolder, eventCopy+fileTypeExtension))
|
|
}
|
|
}
|
|
|
|
func DeleteAllFiles(directoryPath string) {
|
|
os.RemoveAll(directoryPath)
|
|
}
|
|
|
|
func CreateDirectory(directoryPath string) error {
|
|
err := os.MkdirAll(directoryPath, os.ModePerm)
|
|
if err != nil {
|
|
log.Error("Error creating directory", zap.String("directoryPath", directoryPath))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CopyFile(src, dest string) error {
|
|
sourceFile, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer sourceFile.Close()
|
|
|
|
destFile, err := os.Create(dest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer destFile.Close()
|
|
|
|
_, err = io.Copy(destFile, sourceFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = sourceFile.Seek(0, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func FolderExists(folderPath string) bool {
|
|
|
|
_, err := os.Stat(folderPath)
|
|
|
|
return err == nil || os.IsExist(err)
|
|
}
|
|
|
|
func ConvertToValidFilename(input string) string {
|
|
// Replace invalid characters with underscores
|
|
re := regexp.MustCompile(`[^\w]+`)
|
|
validFilename := re.ReplaceAllString(input, "_")
|
|
|
|
// Remove leading and trailing underscores
|
|
validFilename = strings.Trim(validFilename, "_")
|
|
|
|
return validFilename
|
|
}
|