mirror of
https://github.com/gmemstr/sliproad.git
synced 2024-09-20 00:21:15 +01:00
c28851fcc3
Making some improvements to the future maintainability of the project, namely in the form of tests. Also implemented a new "dist" command to the Makefile that packs up the binaries, and will eventually be expanded to do more legwork when prepping for a release. Updated file providers to use io.Reader over an explicit multipart file, and removed the handler in favour of a plain filename, since that was it's primary purpose.
86 lines
1.6 KiB
Go
86 lines
1.6 KiB
Go
package files
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type DiskProvider struct{
|
|
FileProvider
|
|
}
|
|
|
|
func (dp *DiskProvider) Setup(args map[string]string) bool {
|
|
return true
|
|
}
|
|
|
|
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, w io.Writer) {
|
|
file := strings.Join([]string{dp.Location,path}, "/")
|
|
fileReader, err := os.Open(file)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_, err = io.Copy(w, fileReader)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
func (dp *DiskProvider) SaveFile(file io.Reader, filename string, path string) bool {
|
|
fullPath := strings.Join([]string{dp.Location,path,filename}, "/")
|
|
f, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, 0666)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return false
|
|
}
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, file)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
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"
|
|
}
|