Skip to content
Snippets Groups Projects
Commit 6ea75f2e authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6514 Umstellung File Download Response

parent 31632589
No related branches found
No related tags found
No related merge requests found
...@@ -36,16 +36,13 @@ import ( ...@@ -36,16 +36,13 @@ import (
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"io" "io"
"mime/multipart"
"net/http" "net/http"
) )
const ( const (
fileIdParam = "id" fileIdParam = "id"
samlTokenParam = "samlToken" samlTokenParam = "samlToken"
fileFormKey = "file"
contentTypeHeaderKey = "Content-Type" contentTypeHeaderKey = "Content-Type"
contentDispositionHeaderKey = "Content-Disposition"
) )
func RegisterHomeEndpoint(mux *runtime.ServeMux) { func RegisterHomeEndpoint(mux *runtime.ServeMux) {
...@@ -94,7 +91,10 @@ func getFileHandler(w http.ResponseWriter, r *http.Request, vars map[string]stri ...@@ -94,7 +91,10 @@ func getFileHandler(w http.ResponseWriter, r *http.Request, vars map[string]stri
return return
} }
err = writeMultipartResponse(w, fileBuffer) w.Header().Set(contentTypeHeaderKey, "application/octet-stream")
w.WriteHeader(http.StatusOK)
_, err = fileBuffer.WriteTo(w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
...@@ -135,30 +135,6 @@ func fetchFileFromGrpc(ctx context.Context, fileId string, samlToken string, grp ...@@ -135,30 +135,6 @@ func fetchFileFromGrpc(ctx context.Context, fileId string, samlToken string, grp
return &fileBuffer, nil return &fileBuffer, nil
} }
func writeMultipartResponse(w http.ResponseWriter, fileBuffer *bytes.Buffer) error {
w.Header().Set(contentTypeHeaderKey, "multipart/form-data")
w.Header().Set(contentDispositionHeaderKey, `attachment; filename="file"`)
w.WriteHeader(http.StatusOK)
mw := multipart.NewWriter(w)
part, err := mw.CreateFormFile(fileFormKey, fileFormKey)
if err != nil {
return fmt.Errorf("error creating multipart form file")
}
_, err = fileBuffer.WriteTo(part)
if err != nil {
return fmt.Errorf("error writing file data to multipart")
}
err = mw.Close()
if err != nil {
return fmt.Errorf("error closing multipart writer")
}
return nil
}
func ErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) { func ErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
st, ok := status.FromError(err) st, ok := status.FromError(err)
if !ok { if !ok {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment