diff --git a/lib/events/filesessions/filestream.go b/lib/events/filesessions/filestream.go index 29669e57a6bed..4cc91118325c2 100644 --- a/lib/events/filesessions/filestream.go +++ b/lib/events/filesessions/filestream.go @@ -124,30 +124,26 @@ func (h *Handler) CompleteUpload(ctx context.Context, upload events.StreamUpload } }() - files := make([]*os.File, 0, len(parts)) - readers := make([]io.Reader, 0, len(parts)) - - defer func() { - for i := 0; i < len(files); i++ { - if err := files[i].Close(); err != nil { - h.WithError(err).Errorf("Failed to close file %q.", files[i].Name()) - } + writePartToFile := func(path string) error { + file, err := os.Open(path) + if err != nil { + return err } - }() + defer func() { + if err := file.Close(); err != nil { + h.WithError(err).Errorf("failed to close file %q", path) + } + }() + + _, err = io.Copy(f, file) + return err + } for _, part := range parts { partPath := h.partPath(upload, part.Number) - file, err := os.Open(partPath) - if err != nil { - return trace.Wrap(err, "failed to open part file for upload") + if err := writePartToFile(partPath); err != nil { + return trace.Wrap(err) } - files = append(files, file) - readers = append(readers, file) - } - - _, err = io.Copy(f, io.MultiReader(readers...)) - if err != nil { - return trace.Wrap(err) } err = h.Config.OnBeforeComplete(ctx, upload)