Move query escaping to multipartwriter

This commit is contained in:
Alexandre Bruyant 2024-01-08 08:01:04 +01:00
parent dfd4fc1ecf
commit 3f708b16d9

16
main.go
View File

@ -41,30 +41,32 @@ func NewIpfsMultipartWriter(w io.Writer) *IpfsMultipartWriter {
func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, name)) encodedName := url.QueryEscape(name)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, encodedName))
h.Set("Content-Type", "application/x-directory") h.Set("Content-Type", "application/x-directory")
return w.CreatePart(h) return w.CreatePart(h)
} }
func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(name))) 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") h.Set("Content-Type", "application/octet-stream")
return w.CreatePart(h) return w.CreatePart(h)
} }
func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
encodedName := url.QueryEscape(name)
h.Set("AbsPath", absPath) h.Set("AbsPath", absPath)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(name))) h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName)))
h.Set("Content-Type", "application/octet-stream") h.Set("Content-Type", "application/octet-stream")
return w.CreatePart(h) return w.CreatePart(h)
} }
func main() { func main() {
githubactions.Infof("Checking inputs...") githubactions.Debugf("Checking inputs")
// Check inputs
path := githubactions.GetInput("path_to_add") path := githubactions.GetInput("path_to_add")
if path == "" { if path == "" {
githubactions.Fatalf("Missing: path_to_add") githubactions.Fatalf("Missing: path_to_add")
@ -95,7 +97,7 @@ func main() {
githubactions.Fatalf("%v is not a directory", path) githubactions.Fatalf("%v is not a directory", path)
} }
githubactions.Infof("Inputs OK") githubactions.Debugf("Inputs OK")
body, writer := io.Pipe() body, writer := io.Pipe()
@ -128,7 +130,7 @@ func main() {
} }
relPath, _ := filepath.Rel(path, innerPath) relPath, _ := filepath.Rel(path, innerPath)
w, err := mwriter.CreateIpfsFilePart(url.QueryEscape(fmt.Sprintf("/%v", relPath))) w, err := mwriter.CreateIpfsFilePart(relPath)
if err != nil { if err != nil {
return err return err
} }