Golang重命名unicode文件名

时间:2016-05-09 12:53:30

标签: file unicode go rename

如何以unicode格式重命名文件,差距如何? 当您尝试重命名标准库函数时,找不到该文件。

Old: /usr/home/spouk/go/src/simple/Agraba - I Know You Are Smiling Now (Original Mix).mp4 New: /usr/home/spouk/go/src/simple/Agraba_-_I_Know_You_Are_Smiling_Now__Original_Mix_.mp4 [RENAME] rename Agraba - I Know You Are Smiling Now (Original Mix).mp4 /usr/home/spouk/go/src/simple/Agraba_-_I_Know_You_Are_Smiling_Now__Original_Mix_.mp4: no such file or directory

有人告诉我使用什么或在哪里熟悉解决这个问题的方向。 TNX。

package main

import (
    "flag"
    d "github.com/fiam/gounidecode/unidecode"
    "fmt"
    "path/filepath"
    "os"
    "strings"
    "strconv"
    "os/exec"
)

type (
    File struct {
        OriginPath  string
        ConvertPath string
    }
    FilesStack struct {
        Files   []File
        InPath  string
        OutPath string
    }
)

func NewFilesStack() *FilesStack {
    n := new(FilesStack)
    n.Files = []File{}
    return n
}
func (f *FilesStack) RuneToAscii(r rune) string {
    if r < 128 {
        return string(r)
    } else {
        return "\\u" + strconv.FormatInt(int64(r), 16)
    }
}
func (f *FilesStack) TransliterCyr(str string) string {
    var result []string
    for _, sym := range d.Unidecode(str) {
        if (f.RuneToAscii(sym) == " ") || (f.RuneToAscii(sym) == "'") || (f.RuneToAscii(sym) == "`") || (f.RuneToAscii(sym) == ")") || (f.RuneToAscii(sym) == "(") {
            result = append(result, "_")
        } else {
            result = append(result, f.RuneToAscii(sym))
        }
    }
    return strings.Join(result, "")
}
func (f *FilesStack) walkFunction(path string, info os.FileInfo, err error) error {

    if !info.IsDir() {
        res, err := filepath.Abs(filepath.Dir(info.Name()))
        if err != nil {
            fmt.Printf(err.Error())
        }
        ext := filepath.Ext(info.Name())
        if ext == ".mp4" {
            file := new(File)
            //make new file name
            oldfile := filepath.Join(res, info.Name())
            newname := filepath.Join(res, f.TransliterCyr(info.Name()))

            file.OriginPath = filepath.Join(res, newname)
            fmt.Printf("Old: %s\nNew: %s\n", oldfile, newname)

            //rename file
            if err_rename := os.Rename(info.Name(), newname); err_rename != nil {
                fmt.Printf("[RENAME] %s\n", err_rename.Error())
            }

            tmpname := []string{}
            name := strings.TrimRight(info.Name(), filepath.Ext(info.Name()))
            for _, sym := range name {
                if f.RuneToAscii(sym) != " " {
                    tmpname = append(tmpname, string(sym))
                }
            }
            word := strings.Join(tmpname, "")
            oo, _ := filepath.Abs(f.OutPath)
            rr := strings.Join([]string{f.TransliterCyr(word), ".mp3"}, "")
            //fmt.Printf("Res: %v %v\n", rr, word)
            file.ConvertPath = filepath.Join(oo, rr)
            //fmt.Printf("Infile: %v\n", file.OriginPath)
            //fmt.Printf("Outfile: %v\n", file.ConvertPath)
            f.Files = append(f.Files, *file)
        }
    }
    return nil
}
func (f *FilesStack) ParseFilesmp4() {
    //парсинг файлов
    if err := filepath.Walk(f.InPath, f.walkFunction); err != nil {
        fmt.Printf(err.Error())
    }
    //конвертация

    for _, file := range f.Files {
        fmt.Printf("Start convertation %s \n", file.OriginPath)
        command := fmt.Sprintf("ffmpeg -i %s -f mp3 %s", file.OriginPath, file.ConvertPath)
        //fmt.Printf("Command %v\n", command)
        cmd, err_convert := exec.Command(command).Output()
        if err_convert != nil {
            fmt.Printf(err_convert.Error())
        }

        fmt.Printf("Result convert: %v\n", cmd)
    }
}

// note, that variables are pointers
var (
    inpath = flag.String("inpath", ".", "директория для поиска всех файлов с расширением .mp4")
    outpath = flag.String("outpath", ".", "директория для сохранения .mp3")
)

func main() {
    //make files structy
    f := NewFilesStack()
    //parse arguments
    flag.Parse()
    fmt.Printf("inpath: %s\noutpath: %s\n", *inpath, *outpath)
    //set arguments
    f.InPath = *inpath
    f.OutPath = *outpath
    f.ParseFilesmp4()
}

0 个答案:

没有答案