Browse Source

escaping upload file name special characters

if already gzipped, avoid gzipping again
pull/2/head
Chris Lu 12 years ago
parent
commit
5e3b6e968a
  1. 4
      go/operation/upload_content.go
  2. 7
      go/weed/upload.go
  3. 14
      go/weed/volume.go

4
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")

7
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
}

14
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:], ",")

Loading…
Cancel
Save