/
file.go
96 lines (84 loc) · 2.38 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package model
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/TomSuzuki/markdown-wiki/config"
"github.com/TomSuzuki/markdown-wiki/view"
)
// SaveWord ...ファイルを保存する。
func SaveWord(word string, text string) {
path := fmt.Sprintf("%s%s.md", config.PageSavePath, word)
dir := filepath.Dir(path)
os.MkdirAll(dir, os.ModePerm)
ioutil.WriteFile(path, []byte(text), 0666)
}
// GetFileNameWithoutExt ...文字列から拡張子を削除。
func GetFileNameWithoutExt(path string) string {
return strings.Replace(filepath.Join(filepath.Dir(path), filepath.Base(path[:len(path)-len(filepath.Ext(path))])), "\\", "/", -1)
}
// Dirwalk ...ディレクトリ内ファイルをを再帰的に検索し返す。
func Dirwalk(dir string, isIncludeRoot bool) []string {
files, _ := ioutil.ReadDir(dir)
var paths []string
for _, file := range files {
if file.IsDir() {
paths2 := Dirwalk(filepath.Join(dir, file.Name()), isIncludeRoot)
if !isIncludeRoot {
for f := range paths2 {
paths2[f] = filepath.Join(file.Name(), paths2[f])
}
}
paths = append(paths, paths2...)
continue
}
if isIncludeRoot {
paths = append(paths, filepath.Join(dir, file.Name()))
} else {
paths = append(paths, file.Name())
}
}
return paths
}
// GetReadDir ...指定ディレクトリからディレクトリ(true)かワード(false)を返す。
func GetReadDir(path string, sw bool) []view.PathData {
var data []view.PathData
dir := filepath.Join(config.PageSavePath, path)
files, _ := ioutil.ReadDir(dir)
// for display
template := ""
if sw {
template = "📁 %s"
} else {
template = "%s"
}
// list
for _, file := range files {
if file.IsDir() == sw {
data = append(data, view.PathData{
Path: url.QueryEscape(fmt.Sprintf("%s/%s", path, file.Name())),
PathName: fmt.Sprintf(template, file.Name()),
})
}
}
return data
}
// DirPathClean ...ディレクトリパスをきれいにする。
func DirPathClean(path string) string {
path = filepath.Clean(path)
path = filepath.ToSlash(path)
if string(path[len(path)-1]) != "/" {
path += "/"
}
if string(path[0]) == "/" {
path = path[1:]
}
return path
}
// IsRoot ...指定パスがルートディレクトリかチェックする(判定が雑い)。
func IsRoot(dir string) bool {
return dir != "." && dir != "" && dir != "\\" && dir != "/" && dir != "./"
}