Permettre de téléverser plusieurs fichiers à la fois dans admin-upload
#182
1 changed files with 74 additions and 0 deletions
74
api/api.go
74
api/api.go
|
@ -36,6 +36,80 @@ func New(client *http.Client, host, key string, port int, protocol string) (*API
|
|||
return &API{Voki: voki.New(client, host, key, port, protocol)}, nil
|
||||
}
|
||||
|
||||
func (a *API) UploadDocuments(bucketName string, fileHeaders ...*multipart.FileHeader) (response apiresponse.V1DocumentsPOST, err error) {
|
||||
if count := len(fileHeaders); count == 0 {
|
||||
err = fmt.Errorf("api.(*API).UploadDocuments requiert au moins 1 fichier")
|
||||
return
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("%s://%s:%d/v1/bucket/%s/many",
|
||||
a.Voki.Protocol,
|
||||
a.Voki.Host,
|
||||
a.Voki.Port,
|
||||
bucketName,
|
||||
)
|
||||
|
||||
// Create new multipart writer
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// Add files to the request
|
||||
for i, fileHeader := range fileHeaders {
|
||||
if fileHeader == nil {
|
||||
return response, fmt.Errorf("Fichier %d pointe vers une addresse mémoire nulle", i)
|
||||
}
|
||||
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Impossible de lire le contenu du fichier %d '%s': %s", i, fileHeader.Filename, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileName, err := url.QueryUnescape(fileHeader.Filename)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Fichier %d '%s' a un nom invalide et impossible à convertir: %s", i, fileHeader.Filename, err)
|
||||
}
|
||||
|
||||
part, err := writer.CreatePart(fileHeader.Header)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Impossible d'ajouter %d '%s' au formulaire de téléversement: %s", i, fileName, err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, file)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Impossible d'ajouter le contenu de %d '%s' au formulaire de téléversement: %s", i, fileName, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return response, fmt.Errorf("Impossible de fermer le io.Writer: %s", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, endpoint, body)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Impossible de produire une requête: %s", err)
|
||||
}
|
||||
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return response, fmt.Errorf("Impossible de parse le formulaire: %s", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
if a.Voki.Key != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.Voki.Key))
|
||||
}
|
||||
|
||||
// Send the HTTP request
|
||||
httpResponse, err := a.Voki.Client.Do(req)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Impossible d'exécuter la requête http: %s", err)
|
||||
}
|
||||
defer httpResponse.Body.Close()
|
||||
|
||||
return response, json.NewDecoder(httpResponse.Body).Decode(&response)
|
||||
}
|
||||
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (apiresponse.V1DocumentPOST, error) {
|
||||
var response apiresponse.V1DocumentPOST
|
||||
endpoint := fmt.Sprintf("%s://%s:%d",
|
||||
|
|
Loading…
Reference in a new issue