|
|
@@ -8,7 +8,9 @@ import (
|
|
|
"io"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
+ "encoding/hex"
|
|
|
"os"
|
|
|
+ "runtime"
|
|
|
"path"
|
|
|
"path/filepath"
|
|
|
"reflect"
|
|
|
@@ -63,6 +65,71 @@ func writeLog(filepath string, prefix string, message string) bool {
|
|
|
logger.Println(message)
|
|
|
return true
|
|
|
}
|
|
|
+/*
|
|
|
+Functions realted to UM filename conversion and path parsing
|
|
|
+*/
|
|
|
+
|
|
|
+func UMpath2UTF8path(rawname string) string{
|
|
|
+ //Check seperator
|
|
|
+ sep := "/"
|
|
|
+ if runtime.GOOS == "windows" {
|
|
|
+ sep = "\\"
|
|
|
+ }
|
|
|
+ pathChunks := strings.Split(rawname,sep)
|
|
|
+ var newPath []string
|
|
|
+ for i := 0; i<len(pathChunks); i++{
|
|
|
+ //For each path chunks, convert it to UTF8 representation
|
|
|
+ thisChunk := pathChunks[i];
|
|
|
+ if (strings.Contains(thisChunk,".")){
|
|
|
+ filename := thisChunk;
|
|
|
+ extension := filepath.Ext(filename)
|
|
|
+ name := thisChunk[0 : len(thisChunk)-len(extension)]
|
|
|
+ if name[0:5] == "inith"{
|
|
|
+ //This might be a umfilename
|
|
|
+ hexNameOnly := strings.Replace(name,"inith","",1)
|
|
|
+ r, e := hex2bin(hexNameOnly)
|
|
|
+ if (e != nil){
|
|
|
+ //Assume it is normal filename.
|
|
|
+ newPath = append(newPath,thisChunk)
|
|
|
+ }else{
|
|
|
+ decodedName := string(r) + extension
|
|
|
+ newPath = append(newPath,getSafeFilename(decodedName))
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //This is not an umfilename
|
|
|
+ newPath = append(newPath,thisChunk)
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //This might be a folder name
|
|
|
+ r, e := hex2bin(thisChunk)
|
|
|
+ if e != nil{
|
|
|
+ //This path is not hexified
|
|
|
+ newPath = append(newPath,thisChunk)
|
|
|
+ }else{
|
|
|
+ //This path is hexified. Append the converted info into newpath
|
|
|
+ newPath = append(newPath,getSafeFilename(string(r)))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strings.Join(newPath,sep)
|
|
|
+}
|
|
|
+
|
|
|
+func getSafeFilename(s string) string{
|
|
|
+ replacer := strings.NewReplacer("/", "", "\\","", "@","-", "&","-", "*","-", "<","-", ">","-", "|","-", "?","-", ":","-")
|
|
|
+ safeName := replacer.Replace(string(s))
|
|
|
+ return safeName
|
|
|
+}
|
|
|
+
|
|
|
+func hex2bin(s string) ([]byte, error) {
|
|
|
+ ret, err := hex.DecodeString(s)
|
|
|
+ return ret, err
|
|
|
+}
|
|
|
+
|
|
|
+func bin2hex(s string) (string) {
|
|
|
+ src := []byte(s)
|
|
|
+ encodedStr := hex.EncodeToString(src)
|
|
|
+ return encodedStr;
|
|
|
+}
|
|
|
|
|
|
func compress(source, target string) error {
|
|
|
zipfile, err := os.Create(target)
|
|
|
@@ -95,7 +162,9 @@ func compress(source, target string) error {
|
|
|
}
|
|
|
|
|
|
if baseDir != "" {
|
|
|
- header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
|
|
|
+ rawHeaderName := filepath.Join(baseDir, strings.TrimPrefix(path, source))
|
|
|
+ convertedName := UMpath2UTF8path(rawHeaderName);
|
|
|
+ header.Name = convertedName
|
|
|
}
|
|
|
|
|
|
if info.IsDir() {
|