Skip to content

Commit

Permalink
fix file serving after huma update
Browse files Browse the repository at this point in the history
huma context doesn’t seem to work when streaming files, use fiber’s
context instead.
  • Loading branch information
odrling committed Dec 10, 2024
1 parent bd195a6 commit 680bdd5
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions server/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,8 @@ func parseRangeHeader(range_header string) (int64, int64, error) {
}

func Download(ctx context.Context, input *DownloadInput) (*huma.StreamResponse, error) {
obj, err := GetFileObject(ctx, input.Filename)
if err != nil {
return nil, err
}

return serveObject(obj, input.Range, input.IfNoneMatch)
return serveObject(input.Filename, input.Range, input.IfNoneMatch)
}

type FileSender struct {
Expand Down Expand Up @@ -202,8 +198,7 @@ func detectType(obj *minio.Object) (*mimetype.MIME, error) {
return mime, nil
}

func serveObject(obj *minio.Object, range_header string, if_none_match string) (*huma.StreamResponse, error) {
stat, err := obj.Stat()
func serveObject(filename string, range_header string, if_none_match string) (*huma.StreamResponse, error) {

// if stat.Expires.Unix() != 0 && stat.Expires.Before(time.Now()) {
// go DeleteObject(context.Background(), stat.Key)
Expand All @@ -212,6 +207,13 @@ func serveObject(obj *minio.Object, range_header string, if_none_match string) (

return &huma.StreamResponse{
Body: func(ctx huma.Context) {
fiber_ctx := ctx.BodyWriter().(*fiber.Ctx)
obj, err := GetFileObject(fiber_ctx.Context(), filename)
if err != nil {
ctx.SetStatus(500)
return
}

defer func() {
r := recover()
if r != nil {
Expand All @@ -224,6 +226,7 @@ func serveObject(obj *minio.Object, range_header string, if_none_match string) (
}
}()

stat, err := obj.Stat()
if err != nil {
resp := minio.ToErrorResponse(err)
if resp.Code == "NoSuchKey" {
Expand Down Expand Up @@ -277,8 +280,7 @@ func serveObject(obj *minio.Object, range_header string, if_none_match string) (

filesender := FileSender{obj, reqRange, 0}

fiber_ctx := ctx.BodyWriter().(*fiber.Ctx)
err = fiber_ctx.SendStream(&filesender, int(reqRange.Length))
},
}, err
}, nil
}

0 comments on commit 680bdd5

Please sign in to comment.