2022-05-30 20:34:42 +00:00
|
|
|
package emojy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
2022-06-08 20:57:24 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-06-06 14:20:50 +00:00
|
|
|
"io/ioutil"
|
2022-05-30 20:34:42 +00:00
|
|
|
"net/http"
|
2022-06-08 20:57:24 +00:00
|
|
|
"os"
|
2022-06-06 14:20:50 +00:00
|
|
|
"path"
|
2022-06-08 20:57:24 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-07-21 15:25:10 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-05-30 20:34:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed *.html
|
|
|
|
var embeddedFS embed.FS
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) registerWeb() {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/all", p.handleAll)
|
2022-06-08 20:57:24 +00:00
|
|
|
r.HandleFunc("/allFiles", p.handleAllFiles)
|
|
|
|
r.HandleFunc("/upload", p.handleUpload)
|
2022-06-06 14:20:50 +00:00
|
|
|
r.HandleFunc("/file/{name}", p.handleEmojy)
|
2024-02-28 02:40:19 +00:00
|
|
|
r.HandleFunc("/stats", p.handleStats)
|
|
|
|
r.HandleFunc("/list", p.handleList)
|
|
|
|
r.HandleFunc("/new", p.handleUploadForm)
|
|
|
|
r.HandleFunc("/", p.handleStats)
|
2024-02-27 19:29:54 +00:00
|
|
|
p.b.GetWeb().RegisterWebName(r, "/emojy", "Emojys")
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 02:40:19 +00:00
|
|
|
type emojyMap map[string][]EmojyCount
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handleUploadForm(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.b.GetWeb().Index("Emojy", p.uploadIndex()).Render(r.Context(), w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handleList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
threshold := p.c.GetInt("emojy.statthreshold", 1)
|
|
|
|
emojy, err := p.allCounts(threshold)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "Error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.b.GetWeb().Index("Emojy", p.listTempl(emojy)).Render(r.Context(), w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handleStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
threshold := p.c.GetInt("emojy.statthreshold", 1)
|
|
|
|
emojy, err := p.allCounts(threshold)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
log.Error().Err(err).Msgf("handleAll")
|
|
|
|
out, _ := json.Marshal(struct{ err error }{err})
|
|
|
|
w.Write(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.b.GetWeb().Index("Emojy", p.statsIndex(emojy)).Render(r.Context(), w)
|
2022-07-22 13:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handlePage(file string) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
index, _ := embeddedFS.ReadFile(file)
|
|
|
|
w.Write(index)
|
|
|
|
}
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handleAll(w http.ResponseWriter, r *http.Request) {
|
2022-07-23 16:47:31 +00:00
|
|
|
threshold := p.c.GetInt("emojy.statthreshold", 1)
|
|
|
|
emojy, err := p.allCounts(threshold)
|
2022-05-30 20:34:42 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
log.Error().Err(err).Msgf("handleAll")
|
|
|
|
out, _ := json.Marshal(struct{ err error }{err})
|
|
|
|
w.Write(out)
|
|
|
|
}
|
|
|
|
out, _ := json.Marshal(emojy)
|
|
|
|
w.Write(out)
|
|
|
|
}
|
2022-06-06 14:20:50 +00:00
|
|
|
|
2022-06-08 20:57:24 +00:00
|
|
|
func (p *EmojyPlugin) handleAllFiles(w http.ResponseWriter, r *http.Request) {
|
2022-07-22 13:44:42 +00:00
|
|
|
_, urlMap, err := AllFiles(p.emojyPath, p.baseURL)
|
2022-06-08 20:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
out, _ := json.Marshal(struct{ err error }{err})
|
|
|
|
w.Write(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
err = enc.Encode(urlMap)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
out, _ := json.Marshal(struct{ err error }{err})
|
|
|
|
w.Write(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
newFilePath, err := p.FileSave(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("could not upload file")
|
|
|
|
w.WriteHeader(500)
|
2024-02-28 02:40:19 +00:00
|
|
|
fmt.Fprintf(w, "Error with file upload")
|
2022-06-08 20:57:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("uploaded file to %s", newFilePath)
|
|
|
|
w.WriteHeader(200)
|
2024-02-28 02:40:19 +00:00
|
|
|
fmt.Fprintf(w, "success")
|
2022-06-08 20:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
|
|
|
|
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
|
|
|
if err != http.ErrNotMultipart {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.MultipartForm == nil || len(r.MultipartForm.File) == 0 {
|
|
|
|
return "", fmt.Errorf("no files")
|
|
|
|
}
|
|
|
|
|
2024-02-28 02:40:19 +00:00
|
|
|
password := r.FormValue("password")
|
2022-06-08 20:57:24 +00:00
|
|
|
if password != p.b.GetPassword() {
|
|
|
|
return "", fmt.Errorf("incorrect password")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileHeaders := range r.MultipartForm.File {
|
|
|
|
for _, fileHeader := range fileHeaders {
|
|
|
|
body, err := fileHeader.Open()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err)
|
|
|
|
}
|
|
|
|
emojyFileName := fileHeader.Filename
|
2022-06-09 01:21:59 +00:00
|
|
|
emojyName := strings.TrimSuffix(emojyFileName, filepath.Ext(emojyFileName))
|
|
|
|
if ok, _, _, _ := p.isKnownEmojy(emojyName); ok {
|
|
|
|
return "", fmt.Errorf("emojy already exists")
|
|
|
|
}
|
2022-06-08 20:57:24 +00:00
|
|
|
contentType := fileHeader.Header.Get("Content-Type")
|
|
|
|
if !strings.HasPrefix(contentType, "image") {
|
|
|
|
return "", fmt.Errorf("incorrect mime type - given: %s", contentType)
|
|
|
|
}
|
2022-07-22 13:44:42 +00:00
|
|
|
fullPath := filepath.Clean(filepath.Join(p.emojyPath, emojyFileName))
|
|
|
|
_ = os.MkdirAll(p.emojyPath, os.ModePerm)
|
2022-06-08 20:57:24 +00:00
|
|
|
log.Debug().Msgf("trying to create/open file: %s", fullPath)
|
|
|
|
file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return emojyFileName, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("did not find file")
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:20:50 +00:00
|
|
|
func (p *EmojyPlugin) handleEmojy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fname := chi.URLParam(r, "name")
|
2022-07-22 13:44:42 +00:00
|
|
|
contents, err := ioutil.ReadFile(path.Join(p.emojyPath, fname))
|
2022-06-06 14:20:50 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
out, _ := json.Marshal(struct{ err error }{err})
|
|
|
|
w.Write(out)
|
|
|
|
}
|
|
|
|
w.Write(contents)
|
|
|
|
}
|