2023-11-20 14:19:59 -05:00
|
|
|
package webhandler
|
2023-08-20 16:19:05 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-24 17:00:49 -04:00
|
|
|
"io"
|
2023-08-20 16:19:05 -04:00
|
|
|
"net/http"
|
2023-10-24 17:20:57 -04:00
|
|
|
"net/url"
|
2023-08-20 16:19:05 -04:00
|
|
|
"sort"
|
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
"codeberg.org/vlbeaudoin/voki/v3"
|
2023-08-20 16:19:05 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/api"
|
2023-12-19 19:44:25 -05:00
|
|
|
"git.agecem.com/agecem/agecem-org/apirequest"
|
2023-08-20 16:19:05 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/models"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
type WebHandler struct {
|
|
|
|
ApiClient *api.API
|
|
|
|
}
|
|
|
|
|
2023-08-20 16:19:05 -04:00
|
|
|
func HandleIndex(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "index-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleAPropos(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "a-propos-html", nil)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleActualite(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "actualite-html", nil)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleActualiteArticle(c echo.Context) error {
|
|
|
|
article := c.Param("article")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func HandleVieEtudiante(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "vie-etudiante-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleVieEtudianteOrganisme(c echo.Context) error {
|
|
|
|
organisme := c.Param("organisme")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
2024-08-22 12:07:40 -04:00
|
|
|
var response HandleDocumentationResponse
|
2023-08-20 18:03:45 -04:00
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Error = err.Error()
|
2023-11-20 15:26:11 -05:00
|
|
|
response.Message = v1BucketsGET.Message
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(v1BucketsGET.StatusCode())
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "documentation-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
//TODO check v1BucketsGET StatusCode and Error
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
for bucket, displayName := range v1BucketsGET.Data.Buckets {
|
2023-08-20 18:03:45 -04:00
|
|
|
// TODO move call to dedicated API client method
|
2024-08-22 12:07:40 -04:00
|
|
|
var v1BucketReadResponse V1BucketGET
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil {
|
2023-08-20 18:35:07 -04:00
|
|
|
response.Error = err.Error()
|
2023-10-24 17:00:49 -04:00
|
|
|
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusInternalServerError)
|
2023-08-20 18:35:07 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucket,
|
|
|
|
DisplayName: displayName,
|
2023-08-22 15:08:10 -04:00
|
|
|
Documents: v1BucketReadResponse.Data.Keys,
|
2023-08-20 16:19:05 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusOK)
|
2023-08-22 15:08:10 -04:00
|
|
|
//response.Message = "HandleDocumentation ok"
|
2023-08-20 18:03:45 -04:00
|
|
|
|
|
|
|
// TODO render .Message
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
2024-08-21 13:39:03 -04:00
|
|
|
//return c.Render(response.StatusCode(), "documentation-html", response.Data.Buckets)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleFormulaires(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "formulaires-html", nil)
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
|
2023-08-20 16:19:05 -04:00
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
2023-10-24 17:20:57 -04:00
|
|
|
unescaped, err := url.QueryUnescape(fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"message": "Bad Request"})
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := h.ApiClient.Voki.Call(http.MethodGet, unescaped, nil, true)
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response := voki.ResponseNotFound{}
|
|
|
|
return c.JSON(response.StatusCode(), response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
2023-10-24 17:00:49 -04:00
|
|
|
defer response.Body.Close()
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
2024-08-21 13:39:03 -04:00
|
|
|
response := voki.ResponseNotFound{}
|
|
|
|
return c.JSON(response.StatusCode(), response)
|
2023-10-24 17:00:49 -04:00
|
|
|
case http.StatusInternalServerError:
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
|
|
|
}
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Blob(http.StatusOK, "application/octet-stream", body)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleAdmin(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "admin-html", nil)
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
|
2024-08-22 12:07:40 -04:00
|
|
|
var response HandleAdminDocumentsUploadResponse
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(v1BucketsGET.StatusCode())
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Error = err.Error()
|
2023-11-20 15:26:11 -05:00
|
|
|
response.Message = v1BucketsGET.Message
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", nil)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
for bucketName, displayName := range v1BucketsGET.Data.Buckets {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucketName,
|
|
|
|
DisplayName: displayName,
|
|
|
|
})
|
|
|
|
}
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusOK)
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
2023-12-19 19:44:25 -05:00
|
|
|
var request apirequest.V1DocumentsPOST
|
2024-08-22 12:07:40 -04:00
|
|
|
var response HandleAdminDocumentsUploadResponse
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(v1BucketsGET.StatusCode())
|
2023-11-20 15:26:11 -05:00
|
|
|
response.Message = v1BucketsGET.Message
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Error = err.Error()
|
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:26:11 -05:00
|
|
|
for bucketName, displayName := range v1BucketsGET.Data.Buckets {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucketName,
|
|
|
|
DisplayName: displayName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-19 19:44:25 -05:00
|
|
|
request.Params.Bucket = c.FormValue("bucket")
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-12-19 19:44:25 -05:00
|
|
|
form, err := c.MultipartForm()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusBadRequest)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = "Impossible de téléverser"
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Error = err.Error()
|
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
2023-12-19 19:44:25 -05:00
|
|
|
if form == nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusInternalServerError)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = "Formulaire pointe vers une addresse mémoire nulle"
|
|
|
|
response.Error = "Formulaire pointe vers une addresse mémoire nulle"
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-12-19 19:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.File) == 0 {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusBadRequest)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = "Veuillez sélectionner au moins 1 fichier à téléverser"
|
|
|
|
response.Error = "Input 'documents' ne contient aucun fichier"
|
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-12-19 19:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for inputName, inputFileHeaders := range form.File {
|
|
|
|
if inputName == "documents" {
|
|
|
|
request.Data.Documents = inputFileHeaders
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Data.Documents == nil {
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusBadRequest)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = "Impossible d'obtenir les documents depuis le formulaire"
|
|
|
|
response.Error = "Impossible d'obtenir les documents depuis le formulaire"
|
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-12-19 19:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uploadDocumentsResponse, err := h.ApiClient.UploadDocuments(request.Params.Bucket, request.Data.Documents...)
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-12-19 19:44:25 -05:00
|
|
|
//TODO figure out pourquoi `err` n'est jamais `nil`
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(uploadDocumentsResponse.StatusCode())
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = uploadDocumentsResponse.Message
|
|
|
|
response.Error = fmt.Sprintf("%s. Détails: %s", err.Error(), uploadDocumentsResponse.Error)
|
|
|
|
/*
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusInternalServerError)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = fmt.Sprintf("api.(*API).UploadDocuments: %s", err)
|
|
|
|
response.Error = err.Error()
|
|
|
|
*/
|
2023-08-20 18:03:45 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-12-19 19:44:25 -05:00
|
|
|
//TODO figure out pourquoi on se rend jamais ici
|
|
|
|
|
2023-08-20 16:19:05 -04:00
|
|
|
// Format response
|
|
|
|
var info, status string
|
|
|
|
|
2023-12-19 19:44:25 -05:00
|
|
|
for i, document := range uploadDocumentsResponse.Data.Documents {
|
|
|
|
info = fmt.Sprintf("%s[%d] /public/documentation/%s/%s (%dk) ok\n",
|
|
|
|
info, i, uploadDocumentsResponse.Data.Bucket, document.Key, document.Size)
|
|
|
|
}
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-12-19 19:44:25 -05:00
|
|
|
status = uploadDocumentsResponse.Message
|
|
|
|
if errMsg := uploadDocumentsResponse.Error; errMsg != "" {
|
|
|
|
status = fmt.Sprintf("%s. Erreur: %s", status, errMsg)
|
|
|
|
}
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
response.SetStatusCode(http.StatusOK)
|
2023-12-19 19:44:25 -05:00
|
|
|
response.Message = fmt.Sprintf("%s \n %s", status, info)
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2024-08-21 13:39:03 -04:00
|
|
|
return c.Render(response.StatusCode(), "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|