Allow for PDF uploads from hub
This commit is contained in:
parent
8147d9bef6
commit
0c9a79e24a
66
cmd/frontend/pdf.go
Normal file
66
cmd/frontend/pdf.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package frontend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UploadPDF(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Println("Content-Type:", r.Header.Get("Content-Type"))
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(1)
|
||||||
|
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(2)
|
||||||
|
file, _, err := r.FormFile("pdf-upload")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
fmt.Println(3)
|
||||||
|
filename := fmt.Sprint(uuid.New(), ".pdf")
|
||||||
|
absFilepath, err := filepath.Abs(fmt.Sprint(c.PDFDir, "/", filename))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(4)
|
||||||
|
pdf, err := os.Create(absFilepath)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer pdf.Close()
|
||||||
|
|
||||||
|
fmt.Println(5)
|
||||||
|
if _, err = io.Copy(pdf, file); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(6)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@
|
|||||||
{{if lt . 3}}
|
{{if lt . 3}}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<h2>Redakteur</h2>
|
<h2>Redakteur</h2>
|
||||||
<div class="grid grid-cols-2 gap-4">
|
<div class="grid grid-cols-2 gap-x-4 gap-y-2">
|
||||||
<button class="btn" hx-get="/article/all-unpublished" hx-target="#page-content">
|
<button class="btn" hx-get="/article/all-unpublished" hx-target="#page-content">
|
||||||
Unveröffentlichte Artikel
|
Unveröffentlichte Artikel
|
||||||
</button>
|
</button>
|
||||||
@ -28,9 +28,14 @@
|
|||||||
{{if lt . 2}}
|
{{if lt . 2}}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<h2>Herausgeber</h2>
|
<h2>Herausgeber</h2>
|
||||||
<div class="grid grid-cols-2 gap-4">
|
<div class="grid grid-cols-2 gap-x-4 gap-y-2">
|
||||||
<button class="btn" hx-get="/issue/this" hx-target="#page-content">Diese Ausgabe</button>
|
<button class="btn" hx-get="/issue/this" hx-target="#page-content">Diese Ausgabe</button>
|
||||||
<button class="btn" hx-get="/article/all-published" hx-target="#page-content">Artikel löschen</button>
|
<button class="btn" hx-get="/article/all-published" hx-target="#page-content">Artikel löschen</button>
|
||||||
|
<form class="flex" hx-encoding="multipart/form-data">
|
||||||
|
<label class="btn text-center" for="pdf-upload">PDF hochladen</label>
|
||||||
|
<input accept=".pdf" class="hidden" id="pdf-upload" name="pdf-upload" type="file"
|
||||||
|
hx-post="/pdf/upload" />
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user