sliproad/files/disk.go
Gabriel Simmer ea1075cb5b
Rewrite file provider to proxy data download (rather than redirect)
Reworked the fileprovider to proxy data directly from the provider
rather than attempt to funkily redirect when needed, since it was overly
complex and wouldn't work well in the long run. Temporarily added "file"
as constant return for determining the filetype through Backblaze.

Added a Makefile to make life easier as well, and rewrote the README to
reflect the refactor/rewrite and new approach to providers.
2020-03-15 23:49:00 +00:00

67 lines
1.2 KiB
Go

package files
import (
"io/ioutil"
"os"
"strings"
)
type DiskProvider struct{
FileProvider
}
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,
}
}
func (dp *DiskProvider) ViewFile(path string) []byte {
file := strings.Join([]string{dp.Location,path}, "/")
fileContents, err := ioutil.ReadFile(file)
if err != nil {
return nil
}
return fileContents
}
func (dp *DiskProvider) SaveFile(contents []byte, path string) bool {
err := ioutil.WriteFile(path, contents, 0600)
if err != nil {
return false
}
return true
}
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"
}