Skip to content

Commit 0eadb0c

Browse files
committed
Implement recursive copy
1 parent f2a7098 commit 0eadb0c

File tree

4 files changed

+97
-35
lines changed

4 files changed

+97
-35
lines changed

arguments.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import (
1111
var arguments = struct {
1212
Input string
1313
Output string
14-
OutputChoice bool
1514
URL string
1615
DryRun bool
1716
Recursive bool
1817
Jobs int
1918
Network string
2019
}{
2120
// Default arguments
22-
OutputChoice: false,
2321
Jobs: 1,
2422
}
2523

@@ -36,11 +34,11 @@ func argumentParsing(args []string) {
3634
Required: true,
3735
Help: "Your input folder."})
3836

39-
//output := parser.String("o", "output", &argparse.Options{
40-
// Required: false,
41-
// Help: "Your output folder, if output is set, " +
42-
// "original files will not be renamed, " +
43-
// "but the renamed version will be copied in the output folder."})
37+
output := parser.String("o", "output", &argparse.Options{
38+
Required: false,
39+
Help: "Your output folder, if output is set, " +
40+
"original files will not be renamed, " +
41+
"but the renamed version will be copied in the output folder."})
4442

4543
network := parser.Selector("n", "network", []string{"resnet-50", "googlenet"}, &argparse.Options{
4644
Required: false,
@@ -70,15 +68,19 @@ func argumentParsing(args []string) {
7068
os.Exit(0)
7169
}
7270

73-
// Handle the input flag
71+
// Convert path parameters to absolute paths
7472
inputFolder, _ := filepath.Abs(*input)
75-
//arguments.Output = outputFolder
76-
//arguments.OutputChoice = true
73+
var outputFolder string
74+
if *output != "" {
75+
outputFolder, _ = filepath.Abs(*output)
76+
}
77+
7778
// Finally save the collected flags
7879
arguments.Network = *network
7980
arguments.Jobs = *jobs
8081
arguments.DryRun = *dryRun
8182
arguments.Recursive = *recursive
8283
arguments.Input = inputFolder
84+
arguments.Output = outputFolder
8385
arguments.URL = *URL
8486
}

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ func main() {
5858
logError("Unable to process this directory.", "["+arguments.Input+"]")
5959
os.Exit(1)
6060
}
61+
62+
// Prefix input path to file names
63+
for i, name := range fileList {
64+
fileList[i] = filepath.Join(arguments.Input, name)
65+
}
6166
}
6267

63-
process(&c, fileList)
68+
n := process(&c, fileList)
6469

65-
logSuccess(color.Yellow(len(fileList))+
70+
logSuccess(color.Yellow(n)+
6671
color.Cyan(" pictures sorted in ")+
6772
color.Yellow(time.Since(start))+
6873
color.Cyan("!"), color.Cyan("Congrats,"))

process.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88
)
99

10-
func process(c *ClassificationService, fileList []string) {
10+
func process(c *ClassificationService, fileList []string) (processed int) {
1111
// Process files in the input folder
1212
for _, file := range fileList {
1313
buf, _ := ioutil.ReadFile(file)
@@ -19,7 +19,10 @@ func process(c *ClassificationService, fileList []string) {
1919
os.Exit(1)
2020
}
2121

22-
renameFile(c, file, buf, tags)
22+
commitFile(c, file, buf, tags)
23+
processed++
2324
}
2425
}
26+
27+
return processed
2528
}

rename.go

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/labstack/gommon/color"
99
"os"
1010
"bytes"
11+
"io"
1112
)
1213

1314
var replaceSpace = strings.NewReplacer(" ", "_")
@@ -18,16 +19,13 @@ var replaceDoubleQuote = strings.NewReplacer("\"", "")
1819
// its content as bytes and its tags (e.g. from Classify)
1920
// and returns a new file name for an image along with
2021
// the formatted tag portion of the new name
21-
func FormatFileName(path string, image []byte, tags []string) (fullPath string, tagPart string) {
22+
func FormatFileName(oldName string, image []byte, tags []string) (newName string, tagPart string) {
2223
tagPart = formatTags(tags)
2324

2425
hashBytes := md5.Sum(image)
2526
hash := hex.EncodeToString(hashBytes[:])
26-
absPath, _ := filepath.Abs(path)
27-
dirPath := filepath.Dir(absPath)
28-
extension := path[len(path)-4:]
29-
newName := tagPart + "_" + hash + extension
30-
fullPath = filepath.Join(dirPath, newName)
27+
extension := oldName[len(oldName)-4:]
28+
newName = tagPart + "_" + hash + extension
3129

3230
return
3331
}
@@ -41,21 +39,21 @@ func formatTags(class []string) string {
4139
return result
4240
}
4341

44-
func renameFile(c *ClassificationService,
42+
// commitFile copies or renames the target image
43+
// to its new path
44+
func commitFile(c *ClassificationService,
4545
path string, image []byte, tags []string) {
4646

4747
// Generate file name
48-
newPath, tagPart := FormatFileName(path, image, tags)
49-
50-
name := filepath.Base(path)
48+
newName, tagPart := FormatFileName(path, image, tags)
5149

5250
// Log file name to console
53-
if len(name) > 19 {
51+
if len(newName) > 19 {
5452
// File name is too long, truncate it
5553
var message bytes.Buffer
5654

5755
// Write first and last path of the file name
58-
truncatedName := name[0:5] + "…" + name[len(name)-9:]
56+
truncatedName := newName[0:5] + "…" + newName[len(newName)-9:]
5957
message.WriteString(color.Yellow("[") + color.Cyan(truncatedName) + color.Yellow("]"))
6058

6159
// Write tags
@@ -67,10 +65,10 @@ func renameFile(c *ClassificationService,
6765
var message bytes.Buffer
6866

6967
// Write file name
70-
message.WriteString(color.Yellow("[") + color.Cyan(name) + color.Yellow("]"))
68+
message.WriteString(color.Yellow("[") + color.Cyan(newName) + color.Yellow("]"))
7169

7270
// Pad to 19 characters
73-
for i := 15 - len(name); i > 0; i-- {
71+
for i := 15 - len(newName); i > 0; i-- {
7472
message.WriteByte(' ')
7573
}
7674

@@ -80,12 +78,66 @@ func renameFile(c *ClassificationService,
8078
logSuccess(message.String(), c.Tag)
8179
}
8280

83-
// Rename file
84-
if !arguments.DryRun {
85-
err := os.Rename(path, newPath)
86-
if err != nil {
87-
logError("Unable to rename this file.", "["+filepath.Base(path)+"]")
88-
os.Exit(1)
89-
}
81+
// Don't do anything if it's a dry run
82+
if arguments.DryRun { return }
83+
84+
// Copy or rename the file
85+
if arguments.Output != "" {
86+
copyFile(path, newName, image)
87+
} else {
88+
renameFile(path, newName)
89+
}
90+
}
91+
92+
// copyFile copies the image to a new path
93+
func copyFile(oldPath string, newName string, image []byte) {
94+
// Get relative path of image to input path
95+
relPath, err := filepath.Rel(arguments.Input, oldPath)
96+
if err != nil { panic(err) }
97+
98+
// Get relative directory of the image
99+
relDir := filepath.Dir(relPath)
100+
101+
// Get new target directory of the image
102+
newDir := filepath.Join(arguments.Output, relDir)
103+
104+
// Create directory if it doesn't exist
105+
err = os.MkdirAll(newDir, 0755)
106+
if err != nil {
107+
logError("Unable to create target directory this file.", "["+relPath+"]")
108+
os.Exit(1)
109+
}
110+
111+
// Create new image file
112+
newPath := filepath.Join(newDir, newName)
113+
114+
newImage, err := os.OpenFile(newPath,
115+
os.O_CREATE | os.O_EXCL | os.O_WRONLY, 0644)
116+
117+
if err != nil {
118+
logError("Unable to open this file.", "["+newPath+"]")
119+
os.Exit(1)
120+
}
121+
122+
defer newImage.Close()
123+
124+
// Copy
125+
_, err = io.Copy(newImage, bytes.NewReader(image))
126+
127+
if err != nil {
128+
logError("Failed to copy this file.", "["+newPath+"]")
129+
os.Exit(1)
130+
}
131+
}
132+
133+
// renameFile renames the image
134+
func renameFile(oldPath string, newImage string) {
135+
dir := filepath.Dir(oldPath)
136+
newPath := filepath.Join(dir, newImage)
137+
138+
err := os.Rename(oldPath, newPath)
139+
if err != nil {
140+
logError("Unable to rename this file.", "["+filepath.Base(oldPath)+"]")
141+
os.Exit(1)
90142
}
91143
}

0 commit comments

Comments
 (0)