Commit ae5259f0 authored by Matt Bell's avatar Matt Bell

commands: URL escape filenames in multipart files, resolves #654

parent 34aee4d7
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"mime" "mime"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/url"
) )
const ( const (
...@@ -67,7 +68,12 @@ func (f *MultipartFile) NextFile() (File, error) { ...@@ -67,7 +68,12 @@ func (f *MultipartFile) NextFile() (File, error) {
} }
func (f *MultipartFile) FileName() string { func (f *MultipartFile) FileName() string {
return f.Part.FileName() filename, err := url.QueryUnescape(f.Part.FileName())
if err != nil {
// if there is a unescape error, just treat the name as unescaped
return f.Part.FileName()
}
return filename
} }
func (f *MultipartFile) Read(p []byte) (int, error) { func (f *MultipartFile) Read(p []byte) (int, error) {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"io" "io"
"mime/multipart" "mime/multipart"
"net/textproto" "net/textproto"
"net/url"
"sync" "sync"
files "github.com/jbenet/go-ipfs/commands/files" files "github.com/jbenet/go-ipfs/commands/files"
...@@ -74,11 +75,12 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) { ...@@ -74,11 +75,12 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
// write the boundary and headers // write the boundary and headers
header := make(textproto.MIMEHeader) header := make(textproto.MIMEHeader)
filename := url.QueryEscape(file.FileName())
if mfr.form { if mfr.form {
contentDisposition := fmt.Sprintf("form-data; name=\"file\"; filename=\"%s\"", file.FileName()) contentDisposition := fmt.Sprintf("form-data; name=\"file\"; filename=\"%s\"", filename)
header.Set("Content-Disposition", contentDisposition) header.Set("Content-Disposition", contentDisposition)
} else { } else {
header.Set("Content-Disposition", fmt.Sprintf("file; filename=\"%s\"", file.FileName())) header.Set("Content-Disposition", fmt.Sprintf("file; filename=\"%s\"", filename))
} }
if file.IsDirectory() { if file.IsDirectory() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment