Fix ClearPath()
This commit is contained in:
parent
6ce210a00a
commit
51abd3cf47
11
file_test.go
11
file_test.go
|
@ -1,6 +1,7 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
@ -45,3 +46,13 @@ func TestFileCreate(t *testing.T) {
|
|||
tw.Wait()
|
||||
//< Threaded write =
|
||||
}
|
||||
|
||||
func TestClearPath(t *testing.T) {
|
||||
exp := "./path/to/file/with.ext"
|
||||
fn := "....//path/////to/////file/with....ext"
|
||||
r := ClearPath(fn)
|
||||
if exp != r {
|
||||
t.Errorf("%s not match %s", exp, r)
|
||||
}
|
||||
fmt.Println(r)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
|
@ -87,9 +88,15 @@ func RealPath(path string) string {
|
|||
return rpath
|
||||
}
|
||||
|
||||
var (
|
||||
rs = regexp.MustCompile("[/]+")
|
||||
rd = regexp.MustCompile("[.]+")
|
||||
)
|
||||
|
||||
// Очистить путь от лишних символов
|
||||
func ClearPath(path string) string {
|
||||
return strings.ReplaceAll(strings.ReplaceAll(path, "//", ""), "..", "")
|
||||
out := rd.ReplaceAllString(rs.ReplaceAllString(path, "/"), ".")
|
||||
return out
|
||||
}
|
||||
|
||||
// Получить расширение файла
|
||||
|
|
Loading…
Reference in New Issue