Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error field name to multipart response #60

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
omerzi marked this conversation as resolved.
Show resolved Hide resolved
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()
omerzi marked this conversation as resolved.
Show resolved Hide resolved

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
Loading