2021-06-28 20:17:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
2021-07-12 17:29:32 +00:00
|
|
|
|
|
|
|
"github.com/thoas/go-funk"
|
2021-06-28 20:17:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *goBlog) serveEditorFiles(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-07-18 14:39:20 +00:00
|
|
|
// Get files
|
2021-06-28 20:17:18 +00:00
|
|
|
files, err := a.mediaFiles()
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-07-18 14:39:20 +00:00
|
|
|
// Check if files at all
|
|
|
|
if len(files) == 0 {
|
|
|
|
a.render(w, r, templateEditorFiles, &renderData{
|
|
|
|
BlogString: blog,
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-06-28 20:17:18 +00:00
|
|
|
// Sort files time desc
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
|
|
return files[i].Time.After(files[j].Time)
|
|
|
|
})
|
2021-07-12 17:29:32 +00:00
|
|
|
// Find uses
|
|
|
|
fileNames, ok := funk.Map(files, func(f *mediaFile) string {
|
|
|
|
return f.Name
|
|
|
|
}).([]string)
|
|
|
|
if !ok {
|
|
|
|
a.serveError(w, r, "Failed to get file names", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uses, err := a.db.usesOfMediaFile(fileNames...)
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-06-28 20:17:18 +00:00
|
|
|
// Serve HTML
|
|
|
|
a.render(w, r, templateEditorFiles, &renderData{
|
|
|
|
BlogString: blog,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Files": files,
|
2021-07-12 17:29:32 +00:00
|
|
|
"Uses": uses,
|
2021-06-28 20:17:18 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) serveEditorFilesView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.FormValue("filename")
|
|
|
|
if filename == "" {
|
|
|
|
a.serveError(w, r, "No file selected", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, a.mediaFileLocation(filename), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) serveEditorFilesDelete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.FormValue("filename")
|
|
|
|
if filename == "" {
|
|
|
|
a.serveError(w, r, "No file selected", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := a.deleteMediaFile(filename); err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-07-27 10:51:08 +00:00
|
|
|
http.Redirect(w, r, a.getRelativePath(r.Context().Value(blogKey).(string), "/editor/files"), http.StatusFound)
|
2021-06-28 20:17:18 +00:00
|
|
|
}
|