-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patheditorFiles.go
91 lines (83 loc) · 2.5 KB
/
editorFiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"net/http"
"sort"
"github.com/go-chi/chi/v5"
)
const (
editorFilesPath = "/files"
editorFileViewPath = editorFilesPath + "/view"
editorFileUsesPath = editorFilesPath + "/uses"
editorFileUsesPathPlaceholder = "/{filename}"
editorFileDeletePath = editorFilesPath + "/delete"
)
func (a *goBlog) serveEditorFiles(w http.ResponseWriter, r *http.Request) {
// Get files
files, err := a.mediaFiles()
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
// Check if files at all
if len(files) == 0 {
a.render(w, r, a.renderEditorFiles, &renderData{
Data: &editorFilesRenderData{},
})
return
}
// Sort files time desc
sort.Slice(files, func(i, j int) bool {
return files[i].Time.After(files[j].Time)
})
// Serve HTML
a.render(w, r, a.renderEditorFiles, &renderData{
Data: &editorFilesRenderData{
files: files,
},
})
}
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) serveEditorFilesUses(w http.ResponseWriter, r *http.Request) {
filename := r.FormValue("filename")
if filename == "" {
a.serveError(w, r, "No file selected", http.StatusBadRequest)
return
}
_, bc := a.getBlog(r)
http.Redirect(w, r, bc.getRelativePath(editorPath)+editorFileUsesPath+"/"+filename, http.StatusFound)
}
func (a *goBlog) serveEditorFilesUsesResults(w http.ResponseWriter, r *http.Request) {
filename := chi.URLParam(r, "filename")
if filename == "" {
a.serveError(w, r, "No file selected", http.StatusBadRequest)
return
}
_, bc := a.getBlog(r)
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
path: bc.getRelativePath(editorPath + editorFileUsesPath + "/" + filename),
usesFile: filename,
withoutFeeds: true,
allBlogs: true,
})))
}
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
}
_, bc := a.getBlog(r)
http.Redirect(w, r, bc.getRelativePath(editorPath+editorFilesPath), http.StatusFound)
}