Skip to content

Commit

Permalink
update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 committed Mar 11, 2024
1 parent d53e18b commit e42884e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
9 changes: 2 additions & 7 deletions http/filestream/filestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
ioutils "github.com/jfrog/gofrog/io"
"io"
"mime/multipart"
"net/http"
"os"
)

const (
contentType = "Content-Type"
FileType = "file"
FileType = "file"
)

// The expected type of function that should be provided to the ReadFilesFromStream func, that returns the writer that should handle each file
Expand Down Expand Up @@ -50,10 +48,7 @@ func readFile(fileReader *multipart.Part, fileHandlerFunc FileHandlerFunc) (err
return err
}

func WriteFilesToStream(responseWriter http.ResponseWriter, filePaths []string) (err error) {
multipartWriter := multipart.NewWriter(responseWriter)
responseWriter.Header().Set(contentType, multipartWriter.FormDataContentType())

func WriteFilesToStream(multipartWriter *multipart.Writer, filePaths []string) (err error) {
for _, filePath := range filePaths {
if err = writeFile(multipartWriter, filePath); err != nil {
return
Expand Down
12 changes: 5 additions & 7 deletions http/filestream/filestream_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package filestream

import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"mime/multipart"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)

Expand All @@ -24,16 +23,15 @@ func TestWriteFilesToStreamAndReadFilesFromStream(t *testing.T) {
assert.NoError(t, os.WriteFile(file2, file2Content, 0600))

// Create the multipart writer that will stream our files
responseWriter := httptest.NewRecorder()
assert.NoError(t, WriteFilesToStream(responseWriter, []string{file1, file2}))
body := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(body)
assert.NoError(t, WriteFilesToStream(multipartWriter, []string{file1, file2}))

// Create local temp dir that will store our files
targetDir = t.TempDir()

// Get boundary hash from writer
boundary := strings.Split(responseWriter.Header().Get(contentType), "boundary=")[1]
// Create the multipart reader that will read the files from the stream
multipartReader := multipart.NewReader(responseWriter.Body, boundary)
multipartReader := multipart.NewReader(body, multipartWriter.Boundary())
assert.NoError(t, ReadFilesFromStream(multipartReader, simpleFileHandler))

// Validate file 1 transferred successfully
Expand Down

0 comments on commit e42884e

Please sign in to comment.