mirror of
https://github.com/gmemstr/sliproad.git
synced 2024-09-20 00:21:15 +01:00
ea1075cb5b
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.
67 lines
1.2 KiB
Go
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"
|
|
} |