pax_global_header00006660000000000000000000000064142757343770014535gustar00rootroot0000000000000052 comment=dd241e1950410aa10ffc4edeefd4bba0c0893df6 filenamify-1.1.1/000077500000000000000000000000001427573437700136605ustar00rootroot00000000000000filenamify-1.1.1/.gitignore000066400000000000000000000000061427573437700156440ustar00rootroot00000000000000.idea/filenamify-1.1.1/.travis.yml000066400000000000000000000001071427573437700157670ustar00rootroot00000000000000language: go go: - 1.13.x env: - GO111MODULE=on script: go test -vfilenamify-1.1.1/LICENSE000066400000000000000000000020521427573437700146640ustar00rootroot00000000000000MIT License Copyright (c) 2020 tanjiahui Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. filenamify-1.1.1/README.md000066400000000000000000000030101427573437700151310ustar00rootroot00000000000000## go-filenamify [![Build Status](https://travis-ci.com/flytam/filenamify.svg?branch=master)](https://travis-ci.com/flytam/filenamify) Convert a string to a valid safe filename #### Installation ```bash $ go get github.com/flytam/filenamify ``` (optional) To run unit tests: ```bash go test -v ``` #### Usage ```go package main import ( "github.com/flytam/filenamify" "fmt" ) func main() { output,err :=filenamify.Filenamify(``,filenamify.Options{}) fmt.Println(output,err) // => foo!bar,nil //--- output,err =filenamify.Filenamify(`foo:"bar"`,filenamify.Options{ Replacement:"🐴", }) fmt.Println(output,err) // => foo🐴bar,nil output,err =filenamify.FilenamifyV2(``) fmt.Println(output,err) // => foo!bar,nil //--- output,err =filenamify.FilenamifyV2(`foo:"bar"`,func(options *Options) { options.Replacement = "🐴" }) fmt.Println(output,err) // => foo🐴bar,nil } ``` #### API - `Filenamify(str string, options Options) (string, error)` - `func Path(filePath string, options Options) (string, error)` ```go type Options struct { // String for substitution Replacement string// default: "!" // maxlength MaxLength int// default: 100 } ``` FilenamifyV2 and PathV2 are added in v1.1.0 - `func FilenamifyV2(str string, optFuns ...func(options *Options)) (string, error)` - `func PathV2(str string, optFuns ...func(options *Options)) (string, error)` #### Related - [Node-filenamify](https://github.com/sindresorhus/filenamify) #### LICENSE MIT filenamify-1.1.1/filenamify.go000066400000000000000000000062331427573437700163360ustar00rootroot00000000000000package filenamify import ( "errors" "math" "path/filepath" "regexp" ) type Options struct { // String for substitution Replacement string // maxlength MaxLength int } const MAX_FILENAME_LENGTH = 100 func Filenamify(str string, options Options) (string, error) { var replacement string reControlCharsRegex := regexp.MustCompile("[\u0000-\u001f\u0080-\u009f]") reRelativePathRegex := regexp.MustCompile(`^\.+`) // https://github.com/sindresorhus/filename-reserved-regex/blob/master/index.js filenameReservedRegex := regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F]`) filenameReservedWindowsNamesRegex := regexp.MustCompile(`(?i)^(con|prn|aux|nul|com[0-9]|lpt[0-9])$`) if options.Replacement == "" { replacement = "!" } else { replacement = options.Replacement } if filenameReservedRegex.MatchString(replacement) && reControlCharsRegex.MatchString(replacement) { return "", errors.New("Replacement string cannot contain reserved filename characters") } // reserved word str = filenameReservedRegex.ReplaceAllString(str, replacement) // continue str = reControlCharsRegex.ReplaceAllString(str, replacement) str = reRelativePathRegex.ReplaceAllString(str, replacement) // for repeat if len(replacement) > 0 { str = trimRepeated(str, replacement) if len(str) > 1 { str = stripOuter(str, replacement) } } // for windows names if filenameReservedWindowsNamesRegex.MatchString(str) { str = str + replacement } // limit length var limitLength int if options.MaxLength > 0 { limitLength = options.MaxLength } else { limitLength = MAX_FILENAME_LENGTH } strBuf := []byte(str) strBuf = strBuf[0:int(math.Min(float64(limitLength), float64(len(strBuf))))] return string(strBuf), nil } func FilenamifyV2(str string, optFuns ...func(options *Options)) (string, error) { options := Options{ Replacement: "!", MaxLength: MAX_FILENAME_LENGTH, } for _, fn := range optFuns { fn(&options) } return Filenamify(str, options) } func Path(filePath string, options Options) (string, error) { p, err := filepath.Abs(filePath) if err != nil { return "", err } p, err = Filenamify(filepath.Base(p), options) if err != nil { return "", err } return filepath.Join(filepath.Dir(p), p), nil } func PathV2(str string, optFuns ...func(options *Options)) (string, error) { options := Options{ Replacement: "!", MaxLength: MAX_FILENAME_LENGTH, } for _, fn := range optFuns { fn(&options) } return Path(str, options) } func escapeStringRegexp(str string) string { // https://github.com/sindresorhus/escape-string-regexp/blob/master/index.js reg := regexp.MustCompile(`[|\\{}()[\]^$+*?.-]`) str = reg.ReplaceAllStringFunc(str, func(s string) string { return `\` + s }) return str } func trimRepeated(str string, replacement string) string { reg := regexp.MustCompile(`(?:` + escapeStringRegexp(replacement) + `){2,}`) return reg.ReplaceAllString(str, replacement) } func stripOuter(input string, substring string) string { // https://github.com/sindresorhus/strip-outer/blob/master/index.js substring = escapeStringRegexp(substring) reg := regexp.MustCompile(`^` + substring + `|` + substring + `$`) return reg.ReplaceAllString(input, "") } filenamify-1.1.1/filenamify_test.go000066400000000000000000000063661427573437700174040ustar00rootroot00000000000000package filenamify import ( "path/filepath" "testing" ) type inputItem struct { str string options Options } type exampleItem struct { input inputItem // output output string } func newExampleItem(inputStr string, options Options, outputStr string) exampleItem { return exampleItem{ input: inputItem{ inputStr, options, }, output: outputStr, } } func TestFilenamify(t *testing.T) { var output string example := []exampleItem{ newExampleItem("foo/bar", Options{}, "foo!bar"), newExampleItem("foo//bar", Options{}, "foo!bar"), newExampleItem("//foo//bar//", Options{}, "foo!bar"), newExampleItem("foo\\\\\\bar", Options{}, "foo!bar"), //--- newExampleItem("foo/bar", Options{ Replacement: "🐴🐴", }, "foo🐴🐴bar"), newExampleItem("////foo////bar////", Options{ Replacement: "((", }, "foo((bar"), //-- newExampleItem("foo\u0000bar", Options{}, "foo!bar"), newExampleItem(".", Options{}, "!"), newExampleItem("..", Options{}, "!"), newExampleItem("./", Options{}, "!"), newExampleItem("../", Options{}, "!"), newExampleItem("con", Options{}, "con!"), newExampleItem("foo/bar/nul", Options{}, "foo!bar!nul"), newExampleItem("con", Options{ Replacement: "🐴🐴", }, "con🐴🐴"), newExampleItem("c/n", Options{ Replacement: "o", }, "cono"), newExampleItem("c/n", Options{ Replacement: "con", }, "cconn"), } for index, item := range example { if output, _ = Filenamify(item.input.str, item.input.options); output != item.output { t.Error(index, item.input.str, item.input.options, item.output) } else { t.Log(index, "pass") } } } func TestFilenamifyV2(t *testing.T) { var output string input := "c/n" expect := "cconn" if output, _ = FilenamifyV2(input, func(options *Options) { options.Replacement = "con" }); output != expect { t.Error("expect:", expect, "got:", output) } else { t.Log("pass") } } func TestFilenamifyPath(t *testing.T) { expect := "foo!bar" expect2 := "foohbar" inputStr, _ := filepath.Abs("foo:bar") if output, _ := Path(inputStr, Options{}); filepath.Base(output) != expect { t.Error("TestFilenamifyPath error", filepath.Base(output), expect) } if output, _ := PathV2(inputStr); filepath.Base(output) != expect { t.Error("TestFilenamifyPath error", filepath.Base(output), expect) } if output, _ := PathV2(inputStr, func(options *Options) { options.Replacement = "h" }); filepath.Base(output) != expect2 { t.Error("TestFilenamifyPath error", filepath.Base(output), expect2) } } func TestFilenamifyLength(t *testing.T) { // Basename length: 152 const filename = "this/is/a/very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt" if output, _ := Filenamify(filepath.Base(filename), Options{}); output != "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_" { t.Error("TestFilenamifyLength error") } if output, _ := Filenamify(filepath.Base(filename), Options{MaxLength: 180}); output != "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt" { t.Error("TestFilenamifyLength error") } } filenamify-1.1.1/go.mod000066400000000000000000000000551427573437700147660ustar00rootroot00000000000000module github.com/flytam/filenamify go 1.13