2020-02-24 18:07:47 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2020-03-19 10:13:38 +00:00
|
|
|
"io"
|
2020-02-24 18:07:47 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiskProvider struct{
|
|
|
|
FileProvider
|
|
|
|
}
|
|
|
|
|
2020-03-21 00:31:24 +00:00
|
|
|
func (dp *DiskProvider) Setup(args map[string]string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:07:47 +00:00
|
|
|
func (dp *DiskProvider) GetDirectory(path string) Directory {
|
|
|
|
rp := strings.Join([]string{dp.Location,path}, "/")
|
|
|
|
fileDir, err := ioutil.ReadDir(rp)
|
|
|
|
if err != nil {
|
|
|
|
_ = os.MkdirAll(path, 0644)
|
|
|
|
}
|
|
|
|
var fileList []FileInfo
|
|
|
|
|
|
|
|
for _, file := range fileDir {
|
|
|
|
info := FileInfo{
|
|
|
|
IsDirectory: file.IsDir(),
|
|
|
|
Name: file.Name(),
|
|
|
|
}
|
|
|
|
if !info.IsDirectory {
|
|
|
|
split := strings.Split(file.Name(), ".")
|
|
|
|
info.Extension = split[len(split) - 1]
|
|
|
|
}
|
|
|
|
fileList = append(fileList, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Directory{
|
|
|
|
Path: rp,
|
|
|
|
Files: fileList,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:13:38 +00:00
|
|
|
func (dp *DiskProvider) ViewFile(path string, w io.Writer) {
|
2020-03-15 23:48:37 +00:00
|
|
|
file := strings.Join([]string{dp.Location,path}, "/")
|
2020-03-19 10:13:38 +00:00
|
|
|
fileReader, err := os.Open(file)
|
2020-03-15 23:48:37 +00:00
|
|
|
if err != nil {
|
2020-03-19 10:13:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, fileReader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-03-15 23:48:37 +00:00
|
|
|
}
|
2020-02-24 18:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *DiskProvider) SaveFile(contents []byte, path string) bool {
|
|
|
|
err := ioutil.WriteFile(path, contents, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2020-03-15 23:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *DiskProvider) DetermineType(path string) string {
|
|
|
|
rp := strings.Join([]string{dp.Location,path}, "/")
|
|
|
|
file, err := os.Stat(rp)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if file.IsDir() {
|
|
|
|
return "directory"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "file"
|
2020-02-24 18:07:47 +00:00
|
|
|
}
|