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) { if _, err := getSession(w, r, c, s); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := r.ParseMultipartForm(10 << 20); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusBadRequest) return } file, _, err := r.FormFile("pdf-upload") if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } defer file.Close() 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 } pdf, err := os.Create(absFilepath) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } defer pdf.Close() if _, err = io.Copy(pdf, file); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) } }