all repos — nirvash @ 59954b94d806d3e343b0fa6a17c70066b9d8fcda

modular CMS using the quartzgun library

archetype/fileManager.go (raw)

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
package archetype

import (
	"io/ioutil"
	"path/filepath"
	"strings"
)

type SimpleFileManager struct {
	Root       string
	ShowHtml   bool
	ShowHidden bool
}

type FileListing struct {
	Error   string
	Root    string
	Up      string
	SubDirs []string
	Files   []string
}

type FileManager interface {
	Init(cfg *Config) error
	//	ListTree() FileListing
	ListSubTree(root string) FileListing
	//	AddFile(path string, file multipart.FileHeader) error
	//	MkDir(path string) error
	//	Remove(path string) error
	//	Rename(old, new string) error
}

func (self *SimpleFileManager) Init(cfg *Config) error {
	self.Root = cfg.StaticRoot
	self.ShowHtml = cfg.StaticShowHtml
	self.ShowHidden = cfg.StaticShowHidden
	return nil
}

func (self *SimpleFileManager) ListSubTree(root string) FileListing {
	list := FileListing{}

	if strings.Contains(root, "../") || strings.Contains(root, "..\\") {
		list.Error = "You cannot escape!"
		return list
	}

	fullPath := filepath.Join(self.Root, root)

	files, err := ioutil.ReadDir(fullPath)

	if err != nil {
		list.Error = err.Error()
		return list
	}

	list.Root = root
	if !strings.HasSuffix(list.Root, "/") {
		list.Root += "/"
	}
	if !strings.HasPrefix(list.Root, "/") {
		list.Root = "/" + list.Root
	}

	levels := strings.Split(root, "/")
	if list.Root != "/" {
		list.Up = "/"
	}
	if len(levels) >= 2 {
		list.Up = "/" + strings.Join(levels[:len(levels)-1], "/")
	}

	for _, file := range files {
		if !self.ShowHidden && strings.HasPrefix(file.Name(), ".") {
			continue
		}
		if file.IsDir() {
			list.SubDirs = append(list.SubDirs, file.Name())
		} else {
			if !self.ShowHtml && strings.HasSuffix(file.Name(), ".html") {
				continue
			}
			list.Files = append(list.Files, file.Name())
		}
	}

	return list
}