253 lines
7.7 KiB
Go
253 lines
7.7 KiB
Go
package webhandler
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"sort"
|
|
|
|
"git.agecem.com/agecem/agecem-org/api"
|
|
"git.agecem.com/agecem/agecem-org/apirequest"
|
|
"git.agecem.com/agecem/agecem-org/apiresponse"
|
|
"git.agecem.com/agecem/agecem-org/models"
|
|
"git.agecem.com/agecem/agecem-org/webresponse"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type WebHandler struct {
|
|
ApiClient *api.API
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
|
var response webresponse.HandleDocumentationResponse
|
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
|
if err != nil {
|
|
response.Error = err.Error()
|
|
response.Message = v1BucketsGET.Message
|
|
response.StatusCode = v1BucketsGET.StatusCode
|
|
|
|
return c.Render(response.StatusCode, "documentation-html", response)
|
|
}
|
|
|
|
//TODO check v1BucketsGET StatusCode and Error
|
|
|
|
for bucket, displayName := range v1BucketsGET.Data.Buckets {
|
|
// TODO move call to dedicated API client method
|
|
var v1BucketReadResponse apiresponse.V1BucketGET
|
|
|
|
if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil {
|
|
response.Error = err.Error()
|
|
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
|
}
|
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
|
Name: bucket,
|
|
DisplayName: displayName,
|
|
Documents: v1BucketReadResponse.Data.Keys,
|
|
})
|
|
}
|
|
|
|
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
|
|
|
|
response.StatusCode = http.StatusOK
|
|
//response.Message = "HandleDocumentation ok"
|
|
|
|
// TODO render .Message
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
|
//return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
|
|
}
|
|
|
|
func HandleFormulaires(c echo.Context) error {
|
|
return c.Render(http.StatusOK, "formulaires-html", nil)
|
|
}
|
|
|
|
func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
|
|
bucket := c.Param("bucket")
|
|
document := c.Param("document")
|
|
|
|
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)
|
|
if err != nil {
|
|
return c.JSON(apiresponse.NotFoundResponse())
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
switch response.StatusCode {
|
|
case http.StatusNotFound:
|
|
return c.JSON(apiresponse.NotFoundResponse())
|
|
case http.StatusInternalServerError:
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
|
}
|
|
|
|
body, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
|
}
|
|
|
|
return c.Blob(http.StatusOK, "application/octet-stream", body)
|
|
}
|
|
|
|
func HandleAdmin(c echo.Context) error {
|
|
return c.Render(http.StatusOK, "admin-html", nil)
|
|
}
|
|
|
|
func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
|
|
var response webresponse.HandleAdminDocumentsUploadResponse
|
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
|
if err != nil {
|
|
response.StatusCode = v1BucketsGET.StatusCode
|
|
response.Error = err.Error()
|
|
response.Message = v1BucketsGET.Message
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", nil)
|
|
}
|
|
|
|
for bucketName, displayName := range v1BucketsGET.Data.Buckets {
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
|
Name: bucketName,
|
|
DisplayName: displayName,
|
|
})
|
|
}
|
|
response.StatusCode = http.StatusOK
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
|
var request apirequest.V1DocumentsPOST
|
|
var response webresponse.HandleAdminDocumentsUploadResponse
|
|
|
|
v1BucketsGET, err := h.ApiClient.ListBuckets()
|
|
if err != nil {
|
|
response.StatusCode = v1BucketsGET.StatusCode
|
|
response.Message = v1BucketsGET.Message
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
for bucketName, displayName := range v1BucketsGET.Data.Buckets {
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
|
Name: bucketName,
|
|
DisplayName: displayName,
|
|
})
|
|
}
|
|
|
|
request.Params.Bucket = c.FormValue("bucket")
|
|
|
|
form, err := c.MultipartForm()
|
|
if err != nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Impossible de téléverser"
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
if form == nil {
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = "Formulaire pointe vers une addresse mémoire nulle"
|
|
response.Error = "Formulaire pointe vers une addresse mémoire nulle"
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
if len(form.File) == 0 {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Veuillez sélectionner au moins 1 fichier à téléverser"
|
|
response.Error = "Input 'documents' ne contient aucun fichier"
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
for inputName, inputFileHeaders := range form.File {
|
|
if inputName == "documents" {
|
|
request.Data.Documents = inputFileHeaders
|
|
break
|
|
}
|
|
}
|
|
|
|
if request.Data.Documents == nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Impossible d'obtenir les documents depuis le formulaire"
|
|
response.Error = "Impossible d'obtenir les documents depuis le formulaire"
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
uploadDocumentsResponse, err := h.ApiClient.UploadDocuments(request.Params.Bucket, request.Data.Documents...)
|
|
if err != nil {
|
|
//TODO figure out pourquoi `err` n'est jamais `nil`
|
|
response.StatusCode = uploadDocumentsResponse.StatusCode
|
|
response.Message = uploadDocumentsResponse.Message
|
|
response.Error = fmt.Sprintf("%s. Détails: %s", err.Error(), uploadDocumentsResponse.Error)
|
|
/*
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = fmt.Sprintf("api.(*API).UploadDocuments: %s", err)
|
|
response.Error = err.Error()
|
|
*/
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
//TODO figure out pourquoi on se rend jamais ici
|
|
|
|
// Format response
|
|
var info, status string
|
|
|
|
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)
|
|
}
|
|
|
|
status = uploadDocumentsResponse.Message
|
|
if errMsg := uploadDocumentsResponse.Error; errMsg != "" {
|
|
status = fmt.Sprintf("%s. Erreur: %s", status, errMsg)
|
|
}
|
|
|
|
response.StatusCode = http.StatusOK
|
|
response.Message = fmt.Sprintf("%s \n %s", status, info)
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|