Skip to content

Modern Docker Management, Designed for Everyone

fix: downloading files from volumes causes panic (#1907)

Kyle Mendell52317e8d5a68
diff --git a/backend/internal/huma/handlers/volumes.go b/backend/internal/huma/handlers/volumes.go
index 3c4868349..e1e724b58 100644
--- a/backend/internal/huma/handlers/volumes.go
+++ b/backend/internal/huma/handlers/volumes.go
@@ -6,6 +6,7 @@ import (
"mime/multipart"
"net/http"
"path"
+ "strconv"
"github.com/danielgtaylor/huma/v2"
"github.com/getarcaneapp/arcane/backend/internal/common"
@@ -173,13 +174,6 @@ type DownloadFileInput struct {
Path string `query:"path" doc:"File path"`
}
-type DownloadFileOutput struct {
- ContentType string `header:"Content-Type"`
- ContentDisposition string `header:"Content-Disposition"`
- ContentLength int64 `header:"Content-Length"`
- Body io.ReadCloser
-}
-
type UploadFileInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
VolumeName string `path:"volumeName" doc:"Volume name"`
@@ -289,13 +283,6 @@ type DownloadBackupInput struct {
BackupID string `path:"backupId" doc:"Backup ID"`
}
-type DownloadBackupOutput struct {
- ContentType string `header:"Content-Type"`
- ContentDisposition string `header:"Content-Disposition"`
- ContentLength int64 `header:"Content-Length"`
- Body io.ReadCloser
-}
-
type UploadAndRestoreInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
VolumeName string `path:"volumeName" doc:"Volume name"`
@@ -903,7 +890,7 @@ func (h *VolumeHandler) GetFileContent(ctx context.Context, input *GetFileConten
}, nil
}
-func (h *VolumeHandler) DownloadFile(ctx context.Context, input *DownloadFileInput) (*DownloadFileOutput, error) {
+func (h *VolumeHandler) DownloadFile(ctx context.Context, input *DownloadFileInput) (*huma.StreamResponse, error) {
if h.volumeService == nil {
return nil, huma.Error500InternalServerError("service not available")
}
@@ -911,11 +898,18 @@ func (h *VolumeHandler) DownloadFile(ctx context.Context, input *DownloadFileInp
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}
- return &DownloadFileOutput{
- ContentType: "application/octet-stream",
- ContentDisposition: "attachment; filename=" + path.Base(input.Path),
- ContentLength: size,
- Body: reader,
+
+ return &huma.StreamResponse{
+ Body: func(humaCtx huma.Context) {
+ defer func() { _ = reader.Close() }()
+
+ humaCtx.SetHeader("Content-Type", "application/octet-stream")
+ humaCtx.SetHeader("Content-Disposition", "attachment; filename="+path.Base(input.Path))
+ humaCtx.SetHeader("Content-Length", strconv.FormatInt(size, 10))
+
+ writer := humaCtx.BodyWriter()
+ _, _ = io.Copy(writer, reader)
+ },
}, nil
}
@@ -1154,7 +1148,7 @@ func (h *VolumeHandler) DeleteBackup(ctx context.Context, input *DeleteBackupInp
}, nil
}
-func (h *VolumeHandler) DownloadBackup(ctx context.Context, input *DownloadBackupInput) (*DownloadBackupOutput, error) {
+func (h *VolumeHandler) DownloadBackup(ctx context.Context, input *DownloadBackupInput) (*huma.StreamResponse, error) {
if h.volumeService == nil {
return nil, huma.Error500InternalServerError("service not available")
}
@@ -1163,11 +1157,18 @@ func (h *VolumeHandler) DownloadBackup(ctx context.Context, input *DownloadBacku
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}
- return &DownloadBackupOutput{
- ContentType: "application/x-gzip",
- ContentDisposition: "attachment; filename=" + input.BackupID + ".tar.gz",
- ContentLength: size,
- Body: reader,
+
+ return &huma.StreamResponse{
+ Body: func(humaCtx huma.Context) {
+ defer func() { _ = reader.Close() }()
+
+ humaCtx.SetHeader("Content-Type", "application/x-gzip")
+ humaCtx.SetHeader("Content-Disposition", "attachment; filename="+input.BackupID+".tar.gz")
+ humaCtx.SetHeader("Content-Length", strconv.FormatInt(size, 10))
+
+ writer := humaCtx.BodyWriter()
+ _, _ = io.Copy(writer, reader)
+ },
}, nil
}