Ajouter upload de fichier par form html #49

Merged
vlbeaudoin merged 7 commits from feature/admin-route into main 2023-04-28 19:33:30 -04:00
2 changed files with 29 additions and 23 deletions
Showing only changes of commit 5d984ccacb - Show all commits

View file

@ -109,7 +109,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method)) return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
} }
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (string, error) { func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (map[string]interface{}, error) {
endpoint := fmt.Sprintf("%s://%s:%d", endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol, a.Protocol,
a.Host, a.Host,
@ -125,29 +125,29 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
// Add the file to the request // Add the file to the request
file, err := file_header.Open() file, err := file_header.Open()
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#file_header.Open: %s", err) return nil, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
} }
defer file.Close() defer file.Close()
part, err := writer.CreateFormFile("document", file_header.Filename) part, err := writer.CreateFormFile("document", file_header.Filename)
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) return nil, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
} }
_, err = io.Copy(part, file) _, err = io.Copy(part, file)
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#io.Copy: %s", err) return nil, fmt.Errorf("UploadDocument#io.Copy: %s", err)
} }
err = writer.Close() err = writer.Close()
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#writer.Close: %s", err) return nil, fmt.Errorf("UploadDocument#writer.Close: %s", err)
} }
// Create a new HTTP request with the multipart body // Create a new HTTP request with the multipart body
req, err := http.NewRequest(http.MethodPost, url, body) req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#http.NewRequest: %s", err) return nil, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
} }
req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Content-Type", writer.FormDataContentType())
@ -160,7 +160,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("UploadDocument#client.Do: %s", err) return nil, fmt.Errorf("UploadDocument#client.Do: %s", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -168,7 +168,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
var res map[string]interface{} var res map[string]interface{}
json.NewDecoder(resp.Body).Decode(&res) json.NewDecoder(resp.Body).Decode(&res)
return fmt.Sprintf("%s", res), nil return res, nil
} }
// CallWithData takes data and returns a string representing a response body. // CallWithData takes data and returns a string representing a response body.

View file

@ -741,33 +741,39 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error {
Password: viper.GetString("server.admin.password"), Password: viper.GetString("server.admin.password"),
}) })
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)})
} }
bucket := c.FormValue("bucket") bucket := c.FormValue("bucket")
document, err := c.FormFile("document") document, err := c.FormFile("document")
if err != nil { if err != nil {
//return c.JSON(http.StatusBadRequest, map[string]string{"message": "Error during file parse"}) return c.Render(http.StatusBadRequest, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)})
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during echo#Context.FormFile", "error": err.Error()})
} }
/*
file, err := document.Open()
if err != nil {
//return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during file.Open()", "error": err.Error()})
}
defer file.Close()
*/
response, err := client.UploadDocument(bucket, document) response, err := client.UploadDocument(bucket, document)
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error duing api#client.UploadDocument", "error": err.Error()}) return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)})
//return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
} }
return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: response}) // Format response
var message, info, status string
for key, value := range response {
switch key {
case "info":
info_map, ok := value.(map[string]interface{})
if ok {
info = fmt.Sprintf("/public/documentation/%s/%s [%.2f]", info_map["bucket"], info_map["key"], info_map["size"])
}
case "message":
status = fmt.Sprint(value)
}
}
message = fmt.Sprintf("%s - %s", status, info)
return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: message})
} }
// CSS Handlers // CSS Handlers