mirror of https://github.com/velour/catbase.git
emojy: handle with httpin
This commit is contained in:
parent
81fb3dd068
commit
a6d224c87b
|
@ -12,6 +12,7 @@ templ (p *EmojyPlugin) uploadIndex() {
|
||||||
@p.emojyNav()
|
@p.emojyNav()
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<form hx-post="/emojy/upload" enctype="multipart/form-data">
|
||||||
<div class="grid-x">
|
<div class="grid-x">
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<label>Passphrase</label>
|
<label>Passphrase</label>
|
||||||
|
@ -19,12 +20,13 @@ templ (p *EmojyPlugin) uploadIndex() {
|
||||||
</div>
|
</div>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<label>File
|
<label>File
|
||||||
<input type="file" />
|
<input type="file" name="attachment" />
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<button class="button" hx-post="/emojy/upload">Submit</button>
|
<button class="button" type="submit">Submit</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func (p *EmojyPlugin) uploadIndex() templ.Component {
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></div><div class=\"grid-x\"><div class=\"cell\"><label>Passphrase</label> <input type=\"text\" name=\"password\" placeholder=\"Password...\"></div><div class=\"cell\"><label>File <input type=\"file\"></label></div><div class=\"cell\"><button class=\"button\" hx-post=\"/emojy/upload\">Submit</button></div></div></div>")
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></div><form hx-post=\"/emojy/upload\" enctype=\"multipart/form-data\"><div class=\"grid-x\"><div class=\"cell\"><label>Passphrase</label> <input type=\"text\" name=\"password\" placeholder=\"Password...\"></div><div class=\"cell\"><label>File <input type=\"file\" name=\"attachment\"></label></div><div class=\"cell\"><button class=\"button\" type=\"submit\">Submit</button></div></div></form></div>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/ggicci/httpin"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -23,8 +24,10 @@ func (p *EmojyPlugin) registerWeb() {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.HandleFunc("/all", p.handleAll)
|
r.HandleFunc("/all", p.handleAll)
|
||||||
r.HandleFunc("/allFiles", p.handleAllFiles)
|
r.HandleFunc("/allFiles", p.handleAllFiles)
|
||||||
r.HandleFunc("/upload", p.handleUpload)
|
r.With(httpin.NewInput(UploadReq{})).
|
||||||
r.HandleFunc("/file/{name}", p.handleEmojy)
|
Post("/upload", p.handleUpload)
|
||||||
|
r.With(httpin.NewInput(EmojyReq{})).
|
||||||
|
HandleFunc("/file/{name}", p.handleEmojy)
|
||||||
r.HandleFunc("/stats", p.handleStats)
|
r.HandleFunc("/stats", p.handleStats)
|
||||||
r.HandleFunc("/list", p.handleList)
|
r.HandleFunc("/list", p.handleList)
|
||||||
r.HandleFunc("/new", p.handleUploadForm)
|
r.HandleFunc("/new", p.handleUploadForm)
|
||||||
|
@ -98,8 +101,15 @@ func (p *EmojyPlugin) handleAllFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UploadReq struct {
|
||||||
|
Password string `in:"form=password"`
|
||||||
|
Attachment *httpin.File `in:"form=attachment"`
|
||||||
|
}
|
||||||
|
|
||||||
func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) {
|
func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
newFilePath, err := p.FileSave(r)
|
input := r.Context().Value(httpin.Input).(*UploadReq)
|
||||||
|
log.Printf("handleUpload: %#v", input)
|
||||||
|
newFilePath, err := p.FileSave(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msgf("could not upload file")
|
log.Error().Err(err).Msgf("could not upload file")
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
|
@ -111,60 +121,46 @@ func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "success")
|
fmt.Fprintf(w, "success")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
|
func (p *EmojyPlugin) FileSave(input *UploadReq) (string, error) {
|
||||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
if !p.b.CheckPassword("", input.Password) {
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
password := r.FormValue("password")
|
|
||||||
if password != p.b.GetPassword() {
|
|
||||||
return "", fmt.Errorf("incorrect password")
|
return "", fmt.Errorf("incorrect password")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fileHeaders := range r.MultipartForm.File {
|
file := input.Attachment
|
||||||
for _, fileHeader := range fileHeaders {
|
emojyFileName := file.Filename()
|
||||||
body, err := fileHeader.Open()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err)
|
|
||||||
}
|
|
||||||
emojyFileName := fileHeader.Filename
|
|
||||||
emojyName := strings.TrimSuffix(emojyFileName, filepath.Ext(emojyFileName))
|
emojyName := strings.TrimSuffix(emojyFileName, filepath.Ext(emojyFileName))
|
||||||
if ok, _, _, _ := p.isKnownEmojy(emojyName); ok {
|
if ok, _, _, _ := p.isKnownEmojy(emojyName); ok {
|
||||||
return "", fmt.Errorf("emojy already exists")
|
return "", fmt.Errorf("emojy already exists")
|
||||||
}
|
}
|
||||||
contentType := fileHeader.Header.Get("Content-Type")
|
contentType := file.MIMEHeader().Get("Content-Type")
|
||||||
if !strings.HasPrefix(contentType, "image") {
|
if !strings.HasPrefix(contentType, "image") {
|
||||||
return "", fmt.Errorf("incorrect mime type - given: %s", contentType)
|
return "", fmt.Errorf("incorrect mime type - given: %s", contentType)
|
||||||
}
|
}
|
||||||
fullPath := filepath.Clean(filepath.Join(p.emojyPath, emojyFileName))
|
fullPath := filepath.Clean(filepath.Join(p.emojyPath, emojyFileName))
|
||||||
_ = os.MkdirAll(p.emojyPath, os.ModePerm)
|
_ = os.MkdirAll(p.emojyPath, os.ModePerm)
|
||||||
log.Debug().Msgf("trying to create/open file: %s", fullPath)
|
log.Debug().Msgf("trying to create/open file: %s", fullPath)
|
||||||
file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
|
outFile, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer outFile.Close()
|
||||||
_, err = io.Copy(file, body)
|
inFile, _ := file.ReadAll()
|
||||||
|
stream, _ := file.OpenReceiveStream()
|
||||||
|
_, err = outFile.Write(inFile)
|
||||||
|
_, err = io.Copy(outFile, stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return emojyFileName, nil
|
return emojyFileName, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return "", fmt.Errorf("did not find file")
|
type EmojyReq struct {
|
||||||
|
Name string `in:"path=name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *EmojyPlugin) handleEmojy(w http.ResponseWriter, r *http.Request) {
|
func (p *EmojyPlugin) handleEmojy(w http.ResponseWriter, r *http.Request) {
|
||||||
fname := chi.URLParam(r, "name")
|
input := r.Context().Value(httpin.Input).(*EmojyReq)
|
||||||
contents, err := ioutil.ReadFile(path.Join(p.emojyPath, fname))
|
contents, err := ioutil.ReadFile(path.Join(p.emojyPath, input.Name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
out, _ := json.Marshal(struct{ err error }{err})
|
out, _ := json.Marshal(struct{ err error }{err})
|
||||||
|
|
Loading…
Reference in New Issue