88 "github.com/labstack/gommon/color"
99 "os"
1010 "bytes"
11+ "io"
1112)
1213
1314var 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