init
This commit is contained in:
commit
9469379b80
57
cmd/main.go
Normal file
57
cmd/main.go
Normal file
@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"timmy.narnian.us/git/timmy/sanitize"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var previousDir string = "./"
|
||||
filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
|
||||
fmt.Printf("%s >>>>> %s\t%t\n", previousDir, path, strings.HasPrefix(path, previousDir) || previousDir == ".")
|
||||
defer func() { previousDir = filepath.Dir(path) }()
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
|
||||
if info.IsDir() && !DirIsEmpty(path) {
|
||||
return nil
|
||||
}
|
||||
|
||||
newpath := sanitize.SanitizeFilepath(filepath.Dir(path))
|
||||
filename := sanitize.SanitizeFilename(info.Name())
|
||||
if path != filepath.Join(newpath, filename) {
|
||||
fmt.Println(path, "->", filepath.Join(newpath, filename))
|
||||
os.MkdirAll(newpath, os.ModePerm)
|
||||
os.Rename(path, filepath.Join(newpath, info.Name()))
|
||||
}
|
||||
if !(strings.HasPrefix(path, previousDir) || previousDir == "./") {
|
||||
rmpath := previousDir
|
||||
for len(rmpath) > 1 {
|
||||
fmt.Println("rmdir", rmpath)
|
||||
fmt.Println(rmpath, os.Remove(rmpath))
|
||||
rmpath = filepath.Dir(rmpath)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
fmt.Println(previousDir)
|
||||
}
|
||||
|
||||
// also returns false if os.Open fails
|
||||
func DirIsEmpty(path string) bool {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if _, err = file.Readdirnames(1); err == io.EOF {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
49
san.go
Normal file
49
san.go
Normal file
@ -0,0 +1,49 @@
|
||||
package sanitize
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const BadFilenameChars = "\n\t\r=*?:[]\"<>|(){}&'!;$`\\"
|
||||
|
||||
//assumes unicode
|
||||
func SanitizeFilename(filename string) string {
|
||||
filename = strings.Map(func(r rune) rune {
|
||||
if r < 32 || (r > 126 && r < 160) || strings.ContainsRune(BadFilenameChars, r) {
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}, filename)
|
||||
|
||||
filename = strings.TrimSpace(filename)
|
||||
filename = strings.TrimLeft(filename, " -~")
|
||||
filename = strings.TrimSpace(filename)
|
||||
filename = strings.Replace(filename, " ", "_", -1)
|
||||
filename = strings.Replace(filename, "_-_", "", -1)
|
||||
var (
|
||||
st strings.Builder
|
||||
prev rune
|
||||
)
|
||||
st.Grow(len(filename))
|
||||
for _, r := range filename {
|
||||
switch r {
|
||||
case '_':
|
||||
if r != prev {
|
||||
st.WriteRune(r)
|
||||
}
|
||||
default:
|
||||
st.WriteRune(r)
|
||||
}
|
||||
prev = r
|
||||
}
|
||||
return st.String()
|
||||
}
|
||||
|
||||
func SanitizeFilepath(path string) string {
|
||||
var newpath string
|
||||
for _, filename := range strings.Split(filepath.Clean(path), string(filepath.Separator)) {
|
||||
newpath = filepath.Join(newpath, SanitizeFilename(filename))
|
||||
}
|
||||
return newpath
|
||||
}
|
Loading…
Reference in New Issue
Block a user