Permettre de téléverser plusieurs fichiers à la fois dans admin-upload
#182
1 changed files with 58 additions and 0 deletions
|
@ -3,6 +3,10 @@ package media
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/agecem-org/config"
|
||||
"github.com/minio/minio-go/v7"
|
||||
|
@ -76,3 +80,57 @@ func (m *MediaClient) Seed() ([]string, error) {
|
|||
|
||||
return new_buckets, nil
|
||||
}
|
||||
|
||||
func (m *MediaClient) UploadFormFiles(fileHeaders []*multipart.FileHeader) (statusCode int, result string) {
|
||||
switch count := len(fileHeaders); count {
|
||||
case 0:
|
||||
return http.StatusBadRequest, "Veuillez sélectionner au moins 1 document à téléverser"
|
||||
case 1:
|
||||
result = "Téléversement de 1 fichier\n"
|
||||
default:
|
||||
result = fmt.Sprintf("Téléversement de %d fichiers\n", count)
|
||||
}
|
||||
|
||||
var allowedMediaTypes = []string{"application/pdf", "text/markdown", "text/plain"}
|
||||
|
||||
var fileNames []string
|
||||
for _, fileHeader := range fileHeaders {
|
||||
fileNames = append(fileNames, fileHeader.Filename)
|
||||
}
|
||||
|
||||
for i, fileHeader := range fileHeaders {
|
||||
// Check for conflicting file names in upload
|
||||
for j, fileName := range fileNames {
|
||||
if fileName == fileHeader.Filename && i != j {
|
||||
return http.StatusBadRequest, fmt.Sprintf("Doublon de nom de fichier '%s' trouvé, les noms de fichiers doivent être uniques", fileName)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO check for conflicting fileNames with existing files
|
||||
|
||||
// Check media type
|
||||
mediaType, _, err := mime.ParseMediaType(fileHeader.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, fmt.Sprintf("Impossible de déterminer le type de fichier pour %d '%s'.\nPlus de détails: %s", i, fileHeader.Filename, err.Error())
|
||||
}
|
||||
|
||||
var isAllowedMediaType bool
|
||||
|
||||
for _, allowedMediaType := range allowedMediaTypes {
|
||||
if allowedMediaType == mediaType {
|
||||
isAllowedMediaType = true
|
||||
}
|
||||
}
|
||||
|
||||
if !isAllowedMediaType {
|
||||
return http.StatusBadRequest, fmt.Sprintf("Type de fichier interdit '%s' pour '%s'.\nTypes de fichiers permis: %s", mediaType, fileHeader.Filename, allowedMediaTypes)
|
||||
}
|
||||
|
||||
result = fmt.Sprintf("%sDocument %d '%s' est de type '%s'\n",
|
||||
result, i, fileHeader.Filename, mediaType)
|
||||
|
||||
//TODO Upload file
|
||||
}
|
||||
|
||||
return http.StatusCreated, result
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue