feat(web): permettre à admin-upload de téléverser plusieurs fichiers à la fois

Seuls les fichiers pdf, markdown et plain text sont permis.
This commit is contained in:
Victor Lacasse-Beaudoin 2023-12-19 19:44:25 -05:00
parent 8f185c8e85
commit f46dc54aaf
2 changed files with 62 additions and 11 deletions

View file

@ -15,7 +15,12 @@
<div class="formContent">
<div class="formDocUploadDiv">
<p class="formLabel">Document à téléverser</p>
<input class="formDocUpload" type="file" name="document">
<input class="formDocUpload"
type="file"
name="documents"
accept="application/pdf,.md,text/markdown;charset=UTF-8,text/plain"
multiple
/>
</div>
<div class="formSelectDiv">
<label class="formLabel" for="bucket">Type de document</label>

View file

@ -8,6 +8,7 @@ import (
"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"
@ -155,6 +156,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
}
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
var request apirequest.V1DocumentsPOST
var response webresponse.HandleAdminDocumentsUploadResponse
v1BucketsGET, err := h.ApiClient.ListBuckets()
@ -173,35 +175,79 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
})
}
bucket := c.FormValue("bucket")
request.Params.Bucket = c.FormValue("bucket")
document, err := c.FormFile("document")
form, err := c.MultipartForm()
if err != nil {
response.StatusCode = http.StatusBadRequest
response.Message = "Formulaire invalide"
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"
uploadDocumentResponse, err := h.ApiClient.UploadDocument(bucket, document)
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 {
response.StatusCode = uploadDocumentResponse.StatusCode
response.Message = uploadDocumentResponse.Message
response.Error = err.Error()
//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
info = fmt.Sprintf("[%d] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Key)
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 = uploadDocumentResponse.Message
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 - %s", status, info)
response.Message = fmt.Sprintf("%s \n %s", status, info)
return c.Render(response.StatusCode, "admin-upload-html", response)
}