From 5e3b6e968a930a735fcb208bfeda372fe9ba7efc Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 15 Jul 2013 17:26:00 -0700 Subject: [PATCH] escaping upload file name special characters if already gzipped, avoid gzipping again --- go/operation/upload_content.go | 4 +++- go/weed/upload.go | 7 ++++++- go/weed/volume.go | 14 +++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/go/operation/upload_content.go b/go/operation/upload_content.go index e606ef7e3..b73ed0a66 100644 --- a/go/operation/upload_content.go +++ b/go/operation/upload_content.go @@ -21,11 +21,13 @@ type UploadResult struct { Error string } +var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"") + func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool) (*UploadResult, error) { body_buf := bytes.NewBufferString("") body_writer := multipart.NewWriter(body_buf) h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, filename)) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename))) h.Set("Content-Type", mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))) if isGzipped { h.Set("Content-Encoding", "gzip") diff --git a/go/weed/upload.go b/go/weed/upload.go index 8e75b387b..7256093cf 100644 --- a/go/weed/upload.go +++ b/go/weed/upload.go @@ -78,7 +78,12 @@ func upload(filename string, server string, fid string) (int, error) { debug("Failed to stat file:", filename) return 0, fiErr } - ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), path.Base(filename), fh, false) + filename = path.Base(filename) + isGzipped := path.Ext(filename) == ".gz" + if isGzipped { + filename = filename[0:len(filename)-3] + } + ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), filename, fh, isGzipped) if e != nil { return 0, e } diff --git a/go/weed/volume.go b/go/weed/volume.go index dbc0ee547..a22babd45 100644 --- a/go/weed/volume.go +++ b/go/weed/volume.go @@ -272,13 +272,13 @@ func parseURLPath(path string) (vid, fid, filename, ext string) { vid, fid, filename = parts[1], parts[2], parts[3] ext = filepath.Ext(filename) case 2: - parts := strings.Split(path, "/") - vid, fid = parts[1], parts[2] - dotIndex := strings.LastIndex(fid, ".") - if dotIndex > 0 { - ext = fid[dotIndex:] - fid = fid[0:dotIndex] - } + parts := strings.Split(path, "/") + vid, fid = parts[1], parts[2] + dotIndex := strings.LastIndex(fid, ".") + if dotIndex > 0 { + ext = fid[dotIndex:] + fid = fid[0:dotIndex] + } default: sepIndex := strings.LastIndex(path, "/") commaIndex := strings.LastIndex(path[sepIndex:], ",")