@@ -2,8 +2,12 @@ package main
22
33import (
44 "bytes"
5+ "crypto/md5"
6+ "encoding/hex"
57 "fmt"
8+ "io"
69 "io/ioutil"
10+ "log"
711 "math/rand"
812 "net/http"
913 "os"
@@ -33,17 +37,27 @@ func randString(length int, charset string) string {
3337 return string (b )
3438}
3539
40+ func hashFileMD5 (filePath string , name string ) string {
41+ h := md5 .New ()
42+ f , err := os .Open (filePath )
43+ if err != nil {
44+ log .Fatal (err )
45+ }
46+ defer f .Close ()
47+ if _ , err := io .Copy (h , f ); err != nil {
48+ log .Fatal (err )
49+ }
50+ return (hex .EncodeToString (h .Sum (nil )))
51+ }
52+
3653func renameFile (path string , name string , response string ) {
37- originalName := filepath .Base (path )
3854 absPath , _ := filepath .Abs (path )
55+ hash := hashFileMD5 (absPath , name )
3956 dirPath := filepath .Dir (absPath )
40- newName := response + "_" + originalName
41- if len (path ) > 100 {
42- extension := path [len (path )- 4 :]
43- newName = response + "_" + randString (11 , charset ) + extension
44- }
45- err := os .Rename (path , dirPath + "/" + newName )
57+ extension := path [len (path )- 4 :]
58+ newName := response + "_" + hash + extension
4659
60+ err := os .Rename (absPath , dirPath + "/" + newName )
4761 if err != nil {
4862 fmt .Println (err )
4963 stopDeepDetect (name )
@@ -80,7 +94,7 @@ func getClass(path string, name string) {
8094
8195 body , _ := ioutil .ReadAll (resp .Body )
8296 parsedResponse := parseResponse (body )
83- color .Println (color .Yellow ("[" ) + color .Cyan (path ) + color .Yellow ("]" ) + color .Yellow (" Response: " ) + color .Green (parsedResponse ))
97+ color .Println (color .Yellow ("[" ) + color .Cyan (filepath . Base ( path ) ) + color .Yellow ("]" ) + color .Yellow (" Response: " ) + color .Green (parsedResponse ))
8498 renameFile (path , name , parsedResponse )
8599}
86100
0 commit comments