From 34a681dfd209546c1bdd8e8f8cb1e87a40bce96f Mon Sep 17 00:00:00 2001 From: Alexandre Bruyant Date: Mon, 8 Jan 2024 12:25:24 +0100 Subject: [PATCH] Move ipfsMultipartWriter to package --- ipfsnodepin/writer.go | 51 +++++++++++++++++++++++++++++++++++++++++++ main.go | 48 ++-------------------------------------- 2 files changed, 53 insertions(+), 46 deletions(-) create mode 100644 ipfsnodepin/writer.go diff --git a/ipfsnodepin/writer.go b/ipfsnodepin/writer.go new file mode 100644 index 0000000..926302a --- /dev/null +++ b/ipfsnodepin/writer.go @@ -0,0 +1,51 @@ +package ipfsnodepin + +import ( + "fmt" + "io" + "mime/multipart" + "net/textproto" + "net/url" + "strings" +) + +type IpfsMultipartWriter struct { + multipart.Writer +} + +var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") + +func escapeQuotes(s string) string { + return quoteEscaper.Replace(s) +} + +func NewIpfsMultipartWriter(w io.Writer) *IpfsMultipartWriter { + return &IpfsMultipartWriter{ + Writer: *multipart.NewWriter(w), + } +} + +func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) { + h := make(textproto.MIMEHeader) + encodedName := url.QueryEscape(name) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, encodedName)) + h.Set("Content-Type", "application/x-directory") + return w.CreatePart(h) +} + +func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) { + h := make(textproto.MIMEHeader) + encodedName := url.QueryEscape(name) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName))) + h.Set("Content-Type", "application/octet-stream") + return w.CreatePart(h) +} + +func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) { + h := make(textproto.MIMEHeader) + encodedName := url.QueryEscape(name) + h.Set("AbsPath", absPath) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName))) + h.Set("Content-Type", "application/octet-stream") + return w.CreatePart(h) +} diff --git a/main.go b/main.go index 07c2c63..ef1cff4 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,11 @@ import ( "fmt" "io" "io/fs" - "mime/multipart" "net/http" - "net/textproto" - "net/url" "os" "path/filepath" - "strings" + "gitea.bruyant.xyz/alexandre/ipfs-node-pin/ipfsnodepin" "github.com/sethvargo/go-githubactions" ) @@ -23,47 +20,6 @@ type AddResponse struct { Size string } -type IpfsMultipartWriter struct { - multipart.Writer -} - -var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") - -func escapeQuotes(s string) string { - return quoteEscaper.Replace(s) -} - -func NewIpfsMultipartWriter(w io.Writer) *IpfsMultipartWriter { - return &IpfsMultipartWriter{ - Writer: *multipart.NewWriter(w), - } -} - -func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - encodedName := url.QueryEscape(name) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, encodedName)) - h.Set("Content-Type", "application/x-directory") - return w.CreatePart(h) -} - -func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - encodedName := url.QueryEscape(name) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName))) - h.Set("Content-Type", "application/octet-stream") - return w.CreatePart(h) -} - -func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - encodedName := url.QueryEscape(name) - h.Set("AbsPath", absPath) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName))) - h.Set("Content-Type", "application/octet-stream") - return w.CreatePart(h) -} - func main() { githubactions.Debugf("Checking inputs") @@ -112,7 +68,7 @@ func main() { q.Add("stream-true", "false") req.URL.RawQuery = q.Encode() - mwriter := NewIpfsMultipartWriter(writer) + mwriter := ipfsnodepin.NewIpfsMultipartWriter(writer) req.Header.Add("Content-Type", mwriter.FormDataContentType()) go func() {