X-Git-Url: http://www.git.cypherpunks.ru/?p=gocheese.git;a=blobdiff_plain;f=gocheese.go;h=25a1b562918b1a6b35f7e48c663cfc38faa21829;hp=4b47f3c51a72deda900a457e07f9e59166d184a8;hb=48325fb9a20dcc7fab91c6f3c86618e19057254e;hpb=4b49f65744b823da2903583ab348167a1e2756ac diff --git a/gocheese.go b/gocheese.go index 4b47f3c..25a1b56 100644 --- a/gocheese.go +++ b/gocheese.go @@ -93,6 +93,8 @@ var ( warranty = flag.Bool("warranty", false, "Print warranty information") killed bool + + normalizationRe *regexp.Regexp = regexp.MustCompilePOSIX("[-_.]+") ) func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool { @@ -343,6 +345,13 @@ func serveUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } + pkgNames, exists := r.MultipartForm.Value["name"] + if !exists || len(pkgNames) != 1 { + http.Error(w, "name is expected in request", http.StatusBadRequest) + return + } + dir := normalizationRe.ReplaceAllString(pkgNames[0], "-") + dirPath := filepath.Join(*root, dir) var digestExpected []byte if digestExpectedHex, exists := r.MultipartForm.Value["sha256_digest"]; exists { digestExpected, err = hex.DecodeString(digestExpectedHex[0]) @@ -356,8 +365,6 @@ func serveUpload(w http.ResponseWriter, r *http.Request) { filename := file.Filename gpgSigsExpected[filename+GPGSigExt] = struct{}{} log.Println(r.RemoteAddr, "put", filename, "by", username) - dir := filename[:strings.LastIndex(filename, "-")] - dirPath := filepath.Join(*root, dir) path := filepath.Join(dirPath, filename) if _, err = os.Stat(path); err == nil { log.Println(r.RemoteAddr, "already exists", filename) @@ -430,8 +437,6 @@ func serveUpload(w http.ResponseWriter, r *http.Request) { } delete(gpgSigsExpected, filename) log.Println(r.RemoteAddr, "put", filename, "by", username) - dir := filename[:strings.LastIndex(filename, "-")] - dirPath := filepath.Join(*root, dir) path := filepath.Join(dirPath, filename) if _, err = os.Stat(path); err == nil { log.Println(r.RemoteAddr, "already exists", filename)