You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/geplauder/lithium/pipelines"
|
|
"github.com/geplauder/lithium/settings"
|
|
"github.com/geplauder/lithium/storage"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type File struct {
|
|
Bucket string `json:"bucket"`
|
|
FileName string `json:"file_name"`
|
|
Size int `json:"size"`
|
|
ModifiedAt time.Time `json:"modified_at"`
|
|
}
|
|
|
|
func FileHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPipeline, storageProvider storage.IStorageProvider, appSettings settings.Settings) {
|
|
buckets, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath)
|
|
if err != nil {
|
|
writeError(w, 500, "Base path not found")
|
|
return
|
|
}
|
|
|
|
var files []File
|
|
|
|
for _, b := range buckets {
|
|
if b.IsDir() == false {
|
|
continue
|
|
}
|
|
|
|
bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name())
|
|
if err != nil {
|
|
writeError(w, 500, "Base path not found")
|
|
return
|
|
}
|
|
|
|
for _, f := range bucketFiles {
|
|
if b.IsDir() == false {
|
|
continue
|
|
}
|
|
|
|
files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()})
|
|
}
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(struct {
|
|
Files []File `json:"files"`
|
|
}{files})
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|