escaping upload file name special characters
if already gzipped, avoid gzipping again
This commit is contained in:
@@ -21,11 +21,13 @@ type UploadResult struct {
|
|||||||
Error string
|
Error string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
|
||||||
|
|
||||||
func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool) (*UploadResult, error) {
|
func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool) (*UploadResult, error) {
|
||||||
body_buf := bytes.NewBufferString("")
|
body_buf := bytes.NewBufferString("")
|
||||||
body_writer := multipart.NewWriter(body_buf)
|
body_writer := multipart.NewWriter(body_buf)
|
||||||
h := make(textproto.MIMEHeader)
|
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))))
|
h.Set("Content-Type", mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))))
|
||||||
if isGzipped {
|
if isGzipped {
|
||||||
h.Set("Content-Encoding", "gzip")
|
h.Set("Content-Encoding", "gzip")
|
||||||
|
|||||||
@@ -78,7 +78,12 @@ func upload(filename string, server string, fid string) (int, error) {
|
|||||||
debug("Failed to stat file:", filename)
|
debug("Failed to stat file:", filename)
|
||||||
return 0, fiErr
|
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 {
|
if e != nil {
|
||||||
return 0, e
|
return 0, e
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,13 +272,13 @@ func parseURLPath(path string) (vid, fid, filename, ext string) {
|
|||||||
vid, fid, filename = parts[1], parts[2], parts[3]
|
vid, fid, filename = parts[1], parts[2], parts[3]
|
||||||
ext = filepath.Ext(filename)
|
ext = filepath.Ext(filename)
|
||||||
case 2:
|
case 2:
|
||||||
parts := strings.Split(path, "/")
|
parts := strings.Split(path, "/")
|
||||||
vid, fid = parts[1], parts[2]
|
vid, fid = parts[1], parts[2]
|
||||||
dotIndex := strings.LastIndex(fid, ".")
|
dotIndex := strings.LastIndex(fid, ".")
|
||||||
if dotIndex > 0 {
|
if dotIndex > 0 {
|
||||||
ext = fid[dotIndex:]
|
ext = fid[dotIndex:]
|
||||||
fid = fid[0:dotIndex]
|
fid = fid[0:dotIndex]
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
sepIndex := strings.LastIndex(path, "/")
|
sepIndex := strings.LastIndex(path, "/")
|
||||||
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
||||||
|
|||||||
Reference in New Issue
Block a user