Skip to content

Commit

Permalink
light refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Dec 24, 2023
1 parent 938e448 commit 0ca04c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
43 changes: 2 additions & 41 deletions internal/protocol/upload/http_client.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package upload

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"

"merkle-file-uploader/internal/merkle"
"merkle-file-uploader/internal/protocol"
"merkle-file-uploader/internal/utils"
)

var (
Expand All @@ -38,7 +35,7 @@ func (h *HttpUploader) UploadFilesFrom(filePaths []string) (
merkleRoot string,
err error,
) {
requestBody, formDataContentType, err := prepareRequestBody(filePaths)
requestBody, formDataContentType, err := utils.MultipartFormFromFiles(filePaths)
if err != nil {
err = fmt.Errorf("%w: error preparing POST request body: %s", ErrFailedUpload, err)

Expand Down Expand Up @@ -98,39 +95,3 @@ func (h *HttpUploader) computeMerkleRoot(filePaths []string) (merkleRoot string,

return tree.Root.Data, nil
}

func prepareRequestBody(filePaths []string) (requestBody bytes.Buffer, formDataContentType string, err error) {
multipartWriter := multipart.NewWriter(&requestBody)

for _, fp := range filePaths {
var file *os.File
file, err = os.Open(fp)
if err != nil {
return
}

var filePart io.Writer
filePart, err = multipartWriter.CreateFormFile("files", filepath.Base(fp))
if err != nil {
return
}

// copy the file content to the form file part
if _, err = io.Copy(filePart, file); err != nil {
return
}

if err = file.Close(); err != nil {
return
}
}

// Close the multipart writer to finish building the request body
if err = multipartWriter.Close(); err != nil {
return
}

formDataContentType = multipartWriter.FormDataContentType()

return
}
41 changes: 41 additions & 0 deletions internal/utils/http.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package utils

import (
"bytes"
"encoding/json"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)

func HttpOkJson(w http.ResponseWriter, payload any) (err error) {
Expand All @@ -16,3 +21,39 @@ func HttpError(w http.ResponseWriter, statusCode int, err error) {
log.Printf("%s: %s\n", http.StatusText(statusCode), err)
http.Error(w, http.StatusText(statusCode), statusCode)
}

func MultipartFormFromFiles(filePaths []string) (multipartForm bytes.Buffer, formDataContentType string, err error) {
multipartWriter := multipart.NewWriter(&multipartForm)

for _, fp := range filePaths {
var file *os.File
file, err = os.Open(fp)
if err != nil {
return
}

var filePart io.Writer
filePart, err = multipartWriter.CreateFormFile("files", filepath.Base(fp))
if err != nil {
return
}

// copy the file content to the form file part
if _, err = io.Copy(filePart, file); err != nil {
return
}

if err = file.Close(); err != nil {
return
}
}

// Close the multipart writer to finish building the request body
if err = multipartWriter.Close(); err != nil {
return
}

formDataContentType = multipartWriter.FormDataContentType()

return
}

0 comments on commit 0ca04c1

Please sign in to comment.