Skip to content

Commit

Permalink
Add error fieldname to multipart response
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Apr 8, 2024
1 parent bdbcbd3 commit b2ff09b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
17 changes: 17 additions & 0 deletions http/filestream/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package filestream

type MultipartError struct {
FileName string `json:"file_name"`
Message string `json:"message"`
}

func (e MultipartError) Error() string {
return e.Message
}

func NewMultipartError(fileName, message string) *MultipartError {
return &MultipartError{
FileName: fileName,
Message: message,
}
}
32 changes: 26 additions & 6 deletions http/filestream/filestream.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package filestream

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -11,7 +13,8 @@ import (
)

const (
FileType = "file"
FileType = "file"
ErrorType = "error"
)

// 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 @@ -60,21 +63,22 @@ type FileInfo struct {
}

func WriteFilesToStream(multipartWriter *multipart.Writer, filesList []*FileInfo) (err error) {
// Close finishes the multipart message and writes the trailing
// boundary end line to the output.
defer ioutils.Close(multipartWriter, &err)
for _, file := range filesList {
if err = writeFile(multipartWriter, file); err != nil {
return
return writeErrPart(multipartWriter, file, err)
}
}

// Close finishes the multipart message and writes the trailing
// boundary end line to the output.
return multipartWriter.Close()
return nil
}

func writeFile(multipartWriter *multipart.Writer, file *FileInfo) (err error) {
fileReader, err := os.Open(file.Path)
if err != nil {
return fmt.Errorf("failed opening %q: %w", file, err)
return fmt.Errorf("failed opening %q: %w", file.Name, err)
}
defer ioutils.Close(fileReader, &err)
fileWriter, err := multipartWriter.CreateFormFile(FileType, file.Name)
Expand All @@ -84,3 +88,19 @@ func writeFile(multipartWriter *multipart.Writer, file *FileInfo) (err error) {
_, err = io.Copy(fileWriter, fileReader)
return err
}

func writeErrPart(multipartWriter *multipart.Writer, file *FileInfo, writeFileErr error) error {
fileWriter, err := multipartWriter.CreateFormField(ErrorType)
if err != nil {
return fmt.Errorf("failed to CreateFormField: %w", err)
}

multipartErr := NewMultipartError(file.Name, writeFileErr.Error())
multipartErrJSON, err := json.Marshal(multipartErr)
if err != nil {
return fmt.Errorf("failed to marshal multipart error: %w", err)
}

_, err = io.Copy(fileWriter, bytes.NewReader(multipartErrJSON))
return err
}
29 changes: 29 additions & 0 deletions http/filestream/filestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filestream

import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"os"
Expand Down Expand Up @@ -46,6 +47,34 @@ func TestWriteFilesToStreamAndReadFilesFromStream(t *testing.T) {
assert.Equal(t, file2Content, content)
}

func TestWriteFilesToStreamWithError(t *testing.T) {
// Create a temporary directory for the test
sourceDir := t.TempDir()

nonExistentFileName := "nonexistent.txt"
// Create a FileInfo with a non-existent file
file := &FileInfo{Name: nonExistentFileName, Path: filepath.Join(sourceDir, nonExistentFileName)}

// Create a buffer and a multipart writer
body := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(body)

// Call WriteFilesToStream and expect an error
err := WriteFilesToStream(multipartWriter, []*FileInfo{file})
assert.NoError(t, err)

multipartReader := multipart.NewReader(body, multipartWriter.Boundary())
form, err := multipartReader.ReadForm(1024)
assert.NoError(t, err)

assert.Len(t, form.Value[ErrorType], 1)
var multipartErr MultipartError
assert.NoError(t, json.Unmarshal([]byte(form.Value[ErrorType][0]), &multipartErr))

assert.Equal(t, nonExistentFileName, multipartErr.FileName)
assert.NotEmpty(t, multipartErr.Error())
}

func simpleFileWriter(fileName string) (fileWriter []io.WriteCloser, err error) {
writer, err := os.Create(filepath.Join(targetDir, fileName))
if err != nil {
Expand Down

0 comments on commit b2ff09b

Please sign in to comment.