Add documents on diags
Some checks failed
Build and Push Docker Image / build_and_push (push) Failing after 1m51s

This commit is contained in:
2025-02-10 16:17:20 +01:00
parent 023f5c735d
commit d0fc96f8bb
4 changed files with 115 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
// "io"
"net/http"
@@ -14,6 +15,8 @@ import (
"git.coopgo.io/coopgo-apps/parcoursmob/services"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
// filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
diags "git.coopgo.io/coopgo-platform/diags/grpcapi"
diagsstorage "git.coopgo.io/coopgo-platform/diags/storage"
@@ -22,6 +25,7 @@ import (
// "git.coopgo.io/coopgo-platform/groups-management/storage"
// mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
// "github.com/google/uuid"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/structpb"
@@ -135,11 +139,46 @@ func (h *ApplicationHandler) BeneficiariesCreateDiag(w http.ResponseWriter, r *h
return
}
contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "multipart/form-data") {
err = r.ParseMultipartForm(100 * 1024 * 1024) // 100 MB limit
if err != nil {
log.Error().Err(err).Msg("Error parsing multipart form")
w.WriteHeader(http.StatusBadRequest)
return
}
file, header, err := r.FormFile("file-upload")
if err == nil {
defer file.Close()
document_type := r.FormValue("file_type")
document_name := r.FormValue("file_name")
fileid := uuid.NewString()
metadata := map[string]string{
"file_type": document_type,
"file_name": document_name,
}
if err := h.filestorage.Put(file, filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s_%s", resp.Diag.Id, fileid, header.Filename), header.Size, metadata); err != nil {
log.Error().Err(err).Msg("Error uploading file")
w.WriteHeader(http.StatusInternalServerError)
return
}
} else if err != http.ErrMissingFile {
log.Error().Err(err).Msg("Error retrieving file")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
return
}
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID)
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID, h.config.GetStringSlice("modules.diags.documents_types"), h.config.GetStringMapString("storage.files.file_types"), nil)
}
func (h *ApplicationHandler) VehiclesCreateDiag(w http.ResponseWriter, r *http.Request) {
@@ -351,7 +390,12 @@ func (h *ApplicationHandler) DiagsDisplayDiag(w http.ResponseWriter, r *http.Req
}
h.Renderer.DiagsDisplayDiag(w, r, resp.Diag.ToStorageType())
documents := h.filestorage.List(filestorage.PREFIX_DIAGS + "/" + diagid)
events_file_types := h.config.GetStringSlice("modules.diags.documents_types")
file_types_map := h.config.GetStringMapString("storage.files.file_types")
h.Renderer.DiagsDisplayDiag(w, r, resp.Diag.ToStorageType(), events_file_types, file_types_map, documents)
}
func parseDiagsForm(r *http.Request) (*DiagsForm, error) {
@@ -498,3 +542,61 @@ func (h *ApplicationHandler) DiagsHistoryDiag(w http.ResponseWriter, r *http.Req
h.Renderer.DiagsHistoryDiag(w, r, resp.Diag.ToStorageType())
}
// //// ADD DOCUMENTS //////
func (h *ApplicationHandler) DiagsDocuments(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diagID := vars["diagid"]
//r.ParseForm()
r.ParseMultipartForm(100 * 1024 * 1024)
document_type := r.FormValue("type")
document_name := r.FormValue("name")
file, header, err := r.FormFile("file-upload")
if err != nil {
log.Error().Err(err).Msg("")
return
}
defer file.Close()
fileid := uuid.NewString()
metadata := map[string]string{
"type": document_type,
"name": document_name,
}
if err := h.filestorage.Put(file, filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s_%s", diagID, fileid, header.Filename), header.Size, metadata); err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", diagID), http.StatusFound)
}
func (h *ApplicationHandler) DiagsDocumentDownload(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diagID := vars["diagid"]
document := vars["document"]
file, info, err := h.filestorage.Get(filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s", diagID, document))
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", info.ContentType)
if _, err = io.Copy(w, file); err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", diagID), http.StatusFound)
}