diff --git a/http/filestream/filestream.go b/http/filestream/filestream.go index 1cbf9e0..89b6078 100644 --- a/http/filestream/filestream.go +++ b/http/filestream/filestream.go @@ -6,6 +6,7 @@ import ( ioutils "github.com/jfrog/gofrog/io" "io" "mime/multipart" + "os" ) const ( @@ -50,9 +51,14 @@ func readFile(fileReader *multipart.Part, fileWriterFunc FileWriterFunc) (err er // The expected type of function that should be provided to the WriteFilesToStream func, that returns the reader that should handle each file type FileReaderFunc func(fileName string) (writer io.ReadCloser, err error) -func WriteFilesToStream(multipartWriter *multipart.Writer, checksumsList []string, fileReaderFunc FileReaderFunc) (err error) { - for _, fileChecksum := range checksumsList { - if err = writeFile(multipartWriter, fileChecksum, fileReaderFunc); err != nil { +type FileInfo struct { + Name string + Path string +} + +func WriteFilesToStream(multipartWriter *multipart.Writer, filesList []FileInfo) (err error) { + for _, file := range filesList { + if err = writeFile(multipartWriter, file); err != nil { return } } @@ -62,10 +68,10 @@ func WriteFilesToStream(multipartWriter *multipart.Writer, checksumsList []strin return multipartWriter.Close() } -func writeFile(multipartWriter *multipart.Writer, fileChecksum string, fileReaderFunc FileReaderFunc) (err error) { - fileReader, err := fileReaderFunc(fileChecksum) +func writeFile(multipartWriter *multipart.Writer, file FileInfo) (err error) { + fileReader, err := os.Open(file.Path) defer ioutils.Close(fileReader, &err) - fileWriter, err := multipartWriter.CreateFormFile(FileType, fileChecksum) + fileWriter, err := multipartWriter.CreateFormFile(FileType, file.Name) if err != nil { return fmt.Errorf("failed to CreateFormFile: %w", err) } diff --git a/http/filestream/filestream_test.go b/http/filestream/filestream_test.go index 4ba2390..3e10a7a 100644 --- a/http/filestream/filestream_test.go +++ b/http/filestream/filestream_test.go @@ -15,17 +15,17 @@ var targetDir string func TestWriteFilesToStreamAndReadFilesFromStream(t *testing.T) { sourceDir := t.TempDir() // Create 2 file to be transferred via our multipart stream - file1 := filepath.Join(sourceDir, "test1.txt") - file2 := filepath.Join(sourceDir, "test2.txt") + file1 := FileInfo{Name: "test1.txt", Path: filepath.Join(sourceDir, "test1.txt")} + file2 := FileInfo{Name: "test2.txt", Path: filepath.Join(sourceDir, "test2.txt")} file1Content := []byte("test content1") file2Content := []byte("test content2") - assert.NoError(t, os.WriteFile(file1, file1Content, 0600)) - assert.NoError(t, os.WriteFile(file2, file2Content, 0600)) + assert.NoError(t, os.WriteFile(file1.Path, file1Content, 0600)) + assert.NoError(t, os.WriteFile(file2.Path, file2Content, 0600)) // Create the multipart writer that will stream our files body := &bytes.Buffer{} multipartWriter := multipart.NewWriter(body) - assert.NoError(t, WriteFilesToStream(multipartWriter, []string{file1, file2}, simpleFileReader)) + assert.NoError(t, WriteFilesToStream(multipartWriter, []FileInfo{file1, file2})) // Create local temp dir that will store our files targetDir = t.TempDir() @@ -35,24 +35,14 @@ func TestWriteFilesToStreamAndReadFilesFromStream(t *testing.T) { assert.NoError(t, ReadFilesFromStream(multipartReader, simpleFileWriter)) // Validate file 1 transferred successfully - file1 = filepath.Join(targetDir, "test1.txt") - assert.FileExists(t, file1) - content, err := os.ReadFile(file1) + content, err := os.ReadFile(filepath.Join(targetDir, file1.Name)) assert.NoError(t, err) assert.Equal(t, file1Content, content) - assert.NoError(t, os.Remove(file1)) // Validate file 2 transferred successfully - file2 = filepath.Join(targetDir, "test2.txt") - assert.FileExists(t, file2) - content, err = os.ReadFile(file2) + content, err = os.ReadFile(filepath.Join(targetDir, file2.Name)) assert.NoError(t, err) assert.Equal(t, file2Content, content) - assert.NoError(t, os.Remove(file2)) -} - -func simpleFileReader(fileName string) (fileWriter io.ReadCloser, err error) { - return os.Open(filepath.Join(targetDir, fileName)) } func simpleFileWriter(fileName string) (fileWriter io.WriteCloser, err error) {