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 6 commits
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,
}
}
34 changes: 27 additions & 7 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,27 +63,44 @@ 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, thereby marking the EOF.
defer ioutils.Close(multipartWriter, &err)
for _, file := range filesList {
if err = writeFile(multipartWriter, file); err != nil {
return
return writeErr(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 file %q: %w", file.Name, err)
}
defer ioutils.Close(fileReader, &err)
fileWriter, err := multipartWriter.CreateFormFile(FileType, file.Name)
if err != nil {
return fmt.Errorf("failed to CreateFormFile: %w", err)
return fmt.Errorf("failed to create form file for %q: %w", file.Name, err)
}
_, err = io.Copy(fileWriter, fileReader)
return err
}

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

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

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

import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var targetDir string
Expand Down Expand Up @@ -46,6 +48,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})
require.NoError(t, err)

multipartReader := multipart.NewReader(body, multipartWriter.Boundary())
form, err := multipartReader.ReadForm(10 * 1024)
require.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