2024-08-18 11:20:06 +02:00
|
|
|
package calls
|
2024-08-17 18:21:56 +02:00
|
|
|
|
|
|
|
import (
|
2024-08-17 20:15:35 +02:00
|
|
|
"encoding/json"
|
2024-08-17 18:21:56 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-08-17 20:15:35 +02:00
|
|
|
"os"
|
2024-08-17 18:21:56 +02:00
|
|
|
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
)
|
|
|
|
|
2024-08-17 20:15:35 +02:00
|
|
|
func ServePDFList(c *b.Config) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-30 21:20:29 +02:00
|
|
|
if !tokenIsVerified(w, r, c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := os.ReadDir(c.PDFDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames := make([]string, 0)
|
|
|
|
for _, file := range files {
|
|
|
|
fileNames = append(fileNames, file.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err = json.NewEncoder(w).Encode(fileNames); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-08-17 18:21:56 +02:00
|
|
|
}
|
2024-08-17 20:15:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 18:21:56 +02:00
|
|
|
|
2024-08-17 20:56:45 +02:00
|
|
|
func ServePDF(c *b.Config) http.HandlerFunc {
|
2024-08-17 20:15:35 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-30 21:20:29 +02:00
|
|
|
if !tokenIsVerified(w, r, c) {
|
|
|
|
return
|
2024-08-17 18:21:56 +02:00
|
|
|
}
|
2024-08-30 21:20:29 +02:00
|
|
|
|
2024-10-04 16:37:19 +02:00
|
|
|
http.ServeFile(w, r, c.PDFDir+"/"+r.PathValue("id"))
|
2024-08-17 18:21:56 +02:00
|
|
|
}
|
|
|
|
}
|