-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileops.go
131 lines (108 loc) · 4.77 KB
/
fileops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// file fileops.go
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// handleFileTransfer manages the process of transferring a file from a source to a target directory
// based on a detection event. It involves renaming the file according to a specified format,
// creating necessary subdirectories in the target location, and performing the file transfer operation.
func handleFileTransfer(detection Detection, sourceFilesDir, targetFilesDir string, operation FileOperationType) {
// Custom layout to parse the detection date and time.
const customLayout = "2006-01-02T15:04:05"
dateTime := detection.Date + "T" + detection.Time
parsedDate, err := time.Parse(customLayout, dateTime)
if err != nil {
log.Printf("Error parsing combined date and time: %v", err)
return // Ensure further processing is halted upon error.
}
/*
// Format the date and time for the filename in the format YYYYMMDDTHHMMSSZ.
formattedDateTime := parsedDate.Format("20060102T150405Z")
// Format the scientific name for the filename: lowercase, spaces to underscores, remove hyphens and colons.
sciNameFormatted := strings.ToLower(strings.ReplaceAll(detection.SciName, " ", "_"))
// Generate the new filename with the formatted date, time, and confidence level.
confidencePercentage := fmt.Sprintf("%dp", int(detection.Confidence*100))
newFileName := fmt.Sprintf("%s_%s_%s.wav", sciNameFormatted, confidencePercentage, formattedDateTime)
*/
// Determine the year and month for subdirectory structuring within the target directory.
year, month := parsedDate.Format("2006"), parsedDate.Format("01")
subDirPath := filepath.Join(targetFilesDir, year, month)
// Ensure the target subdirectories exist or create them.
if err := os.MkdirAll(subDirPath, os.ModePerm); err != nil {
log.Printf("Failed to create subdirectories: %v", err)
return
}
// Construct the full target file path with the new filename.
newFileName := GenerateClipName(detection)
targetFilePath := filepath.Join(subDirPath, newFileName)
//fmt.Println(targetFilePath)
// Construct the source directory and file paths.
sourceDirPath := filepath.Join(sourceFilesDir, "Extracted", "By_Date", detection.Date, detection.ComName)
sourceFilePath := filepath.Join(sourceDirPath, detection.FileName)
// Check if the source file exists before attempting transfer.
if _, err := os.Stat(sourceFilePath); os.IsNotExist(err) {
//log.Printf("Source file does not exist, skipping copy: %s", sourceFilePath)
return
} else {
// Perform the file operation (copy or move).
if err := performFileOperation(sourceFilePath, targetFilePath, operation); err != nil {
log.Printf("File operation error: %v", err)
}
}
}
func GenerateClipName(detection Detection) string {
// Custom layout to parse the detection date and time.
const customLayout = "2006-01-02T15:04:05"
dateTime := detection.Date + "T" + detection.Time
parsedDate, err := time.Parse(customLayout, dateTime)
if err != nil {
log.Printf("Error parsing combined date and time: %v", err)
return ""
}
// Format the date and time for the filename in the format YYYYMMDDTHHMMSSZ.
formattedDateTime := parsedDate.Format("20060102T150405Z")
// Format the scientific name for the filename: lowercase, spaces to underscores, remove hyphens and colons.
sciNameFormatted := strings.ToLower(strings.ReplaceAll(detection.SciName, " ", "_"))
// Generate the new filename with the formatted date, time, and confidence level.
confidencePercentage := fmt.Sprintf("%dp", int(detection.Confidence*100))
newFileName := fmt.Sprintf("%s_%s_%s.wav", sciNameFormatted, confidencePercentage, formattedDateTime)
return newFileName
}
// performFileOperation abstracts the logic for copying or moving files based on the specified operation.
func performFileOperation(sourceFilePath, targetFilePath string, operation FileOperationType) error {
switch operation {
case CopyFile:
return copyFile(sourceFilePath, targetFilePath)
case MoveFile:
return moveFile(sourceFilePath, targetFilePath)
default:
return fmt.Errorf("unsupported file operation")
}
}
// copyFile handles the copying of a file from the source path to the destination path.
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err // Handle file opening error.
}
defer sourceFile.Close()
destinationFile, err := os.Create(dst)
if err != nil {
return err // Handle destination file creation error.
}
defer destinationFile.Close()
// Perform the actual file copy operation.
_, err = io.Copy(destinationFile, sourceFile)
return err // Return the result of the copy operation.
}
// moveFile handles moving a file from the source path to the destination path.
func moveFile(src, dst string) error {
// Attempt to rename (move) the file directly.
return os.Rename(src, dst)
}