Пример #1
0
func handleTestForm(conn http.ResponseWriter, req *http.Request) {
	if !(req.Method == "POST" && req.URL.Path == "/camli/testform") {
		httputil.BadRequestError(conn, "Inconfigured handler.")
		return
	}

	multipart, err := req.MultipartReader()
	if multipart == nil {
		httputil.BadRequestError(conn, fmt.Sprintf("Expected multipart/form-data POST request; %v", err))
		return
	}

	for {
		part, err := multipart.NextPart()
		if err != nil {
			fmt.Println("Error reading:", err)
			break
		}
		if part == nil {
			break
		}
		formName := part.FormName()
		fmt.Printf("New value [%s], part=%v\n", formName, part)

		sha1 := sha1.New()
		io.Copy(sha1, part)
		fmt.Printf("Got part digest: %x\n", sha1.Sum())

	}
	fmt.Println("Done reading multipart body.")

}
Пример #2
0
func (ui *UIHandler) serveUploadHelper(rw http.ResponseWriter, req *http.Request) {
	rollSum := req.URL.Query().Get("rollsum") == "1"

	ret := make(map[string]interface{})
	defer httputil.ReturnJson(rw, ret)

	if ui.Storage == nil {
		ret["error"] = "No BlobRoot configured"
		ret["errorType"] = "server"
		return
	}

	mr, err := req.MultipartReader()
	if err != nil {
		ret["error"] = "reading body: " + err.String()
		ret["errorType"] = "server"
		return
	}

	got := make([]map[string]interface{}, 0)
	for {
		part, err := mr.NextPart()
		if err == os.EOF {
			break
		}
		if err != nil {
			ret["error"] = "reading body: " + err.String()
			ret["errorType"] = "server"
			break
		}
		fileName := part.FileName()
		if fileName == "" {
			continue
		}
		writeFn := schema.WriteFileFromReader
		if rollSum {
			writeFn = schema.WriteFileFromReaderRolling
		}
		br, err := writeFn(ui.Storage, fileName, part)

		if err == nil {
			got = append(got, map[string]interface{}{
				"filename": part.FileName(),
				"formname": part.FormName(),
				"fileref":  br.String(),
			})
		} else {
			ret["error"] = "writing to blobserver: " + err.String()
			return
		}
	}
	ret["got"] = got
}
Пример #3
0
func handleUploads(r *http.Request) (fileInfos []*FileInfo) {
	fileInfos = make([]*FileInfo, 0)
	mr, err := r.MultipartReader()
	check(err)
	r.Form, err = url.ParseQuery(r.URL.RawQuery)
	check(err)
	part, err := mr.NextPart()
	for err == nil {
		if name := part.FormName(); name != "" {
			if part.FileName() != "" {
				fileInfos = append(fileInfos, handleUpload(r, part))
			} else {
				r.Form[name] = append(r.Form[name], getFormValue(part))
			}
		}
		part, err = mr.NextPart()
	}
	return
}
Пример #4
0
func HandleNess(c http.ResponseWriter, r *http.Request) (title, id, origID, date, desc, body []byte, fileName string, importance int, delete bool) {
	mreader, e := r.MultipartReader()
	if e != nil {
		return
	}
	for {
		mpart, e := mreader.NextPart()
		if mpart == nil {
			return
		}
		switch mpart.FormName() {
		case "title":
			title, e = ioutil.ReadAll(mpart)
		case "id":
			id, e = ioutil.ReadAll(mpart)
		case "orig-id":
			origID, e = ioutil.ReadAll(mpart)
		case "date":
			date, e = ioutil.ReadAll(mpart)
		case "desc":
			desc, e = ioutil.ReadAll(mpart)
		case "body":
			body, e = ioutil.ReadAll(mpart)
		case "delete-event":
			delete = true
			fmt.Println("!!!!Deleting event!!!!")
		case "importance":
			var imp []byte
			imp, e = ioutil.ReadAll(mpart)
			importance, e = strconv.Atoi(string(imp))
			fmt.Println("Importance:", string(imp), imp, importance)
		case "img":
			fileName = saveFile(mpart, string(id))
		}
		if e != nil {
			fmt.Println("Error:", e)
		}
	}
	return
}
Пример #5
0
func handleMultiPartUpload(conn http.ResponseWriter, req *http.Request, blobReceiver blobserver.BlobReceiveConfiger) {
	if w, ok := blobReceiver.(blobserver.ContextWrapper); ok {
		blobReceiver = w.WrapContext(req).(blobserver.BlobReceiveConfiger)
	}

	if !(req.Method == "POST" && strings.Contains(req.URL.Path, "/camli/upload")) {
		log.Printf("Inconfigured handler upload handler")
		httputil.BadRequestError(conn, "Inconfigured handler.")
		return
	}

	receivedBlobs := make([]blobref.SizedBlobRef, 0, 10)

	multipart, err := req.MultipartReader()
	if multipart == nil {
		httputil.BadRequestError(conn, fmt.Sprintf(
			"Expected multipart/form-data POST request; %v", err))
		return
	}

	var errText string
	addError := func(s string) {
		log.Printf("Client error: %s", s)
		if errText == "" {
			errText = s
			return
		}
		errText = errText + "\n" + s
	}

	for {
		mimePart, err := multipart.NextPart()
		if err == os.EOF {
			break
		}
		if err != nil {
			addError(fmt.Sprintf("Error reading multipart section: %v", err))
			break
		}

		//POST-r60:
		//contentDisposition, params, err := mime.ParseMediaType(mimePart.Header.Get("Content-Disposition"))
		//if err != nil {
		//	addError(err.String())
		//	break
		//}
		// r60:
		contentDisposition, params := mime.ParseMediaType(mimePart.Header.Get("Content-Disposition"))
		if contentDisposition == "" {
			addError("invalid Content-Disposition")
			break
		}

		if contentDisposition != "form-data" {
			addError(fmt.Sprintf("Expected Content-Disposition of \"form-data\"; got %q", contentDisposition))
			break
		}

		formName := params["name"]
		ref := blobref.Parse(formName)
		if ref == nil {
			addError(fmt.Sprintf("Ignoring form key %q", formName))
			continue
		}

		if oldAppEngineHappySpec {
			_, hasContentType := mimePart.Header["Content-Type"]
			if !hasContentType {
				addError(fmt.Sprintf("Expected Content-Type header for blobref %s; see spec", ref))
				continue
			}

			_, hasFileName := params["filename"]
			if !hasFileName {
				addError(fmt.Sprintf("Expected 'filename' Content-Disposition parameter for blobref %s; see spec", ref))
				continue
			}
		}

		blobGot, err := blobReceiver.ReceiveBlob(ref, mimePart)
		if err != nil {
			addError(fmt.Sprintf("Error receiving blob %v: %v\n", ref, err))
			break
		}
		log.Printf("Received blob %v\n", blobGot)
		receivedBlobs = append(receivedBlobs, blobGot)
	}

	log.Println("Done reading multipart body.")
	ret := commonUploadResponse(blobReceiver, req)

	received := make([]map[string]interface{}, 0)
	for _, got := range receivedBlobs {
		log.Printf("Got blob: %v\n", got)
		blob := make(map[string]interface{})
		blob["blobRef"] = got.BlobRef.String()
		blob["size"] = got.Size
		received = append(received, blob)
	}
	ret["received"] = received

	if errText != "" {
		ret["errorText"] = errText
	}

	httputil.ReturnJson(conn, ret)
}
Пример #6
0
func handleMultiPartUpload(conn http.ResponseWriter, req *http.Request) {
	if !(req.Method == "POST" && req.URL.Path == "/camli/upload") {
		httputil.BadRequestError(conn, "Inconfigured handler.")
		return
	}

	receivedBlobs := make([]*receivedBlob, 0, 10)

	multipart, err := req.MultipartReader()
	if multipart == nil {
		httputil.BadRequestError(conn, fmt.Sprintf(
			"Expected multipart/form-data POST request; %v", err))
		return
	}

	var errText string
	addError := func(s string) {
		log.Printf("Client error: %s", s)
		if errText == "" {
			errText = s
			return
		}
		errText = errText + "\n" + s
	}

	for {
		part, err := multipart.NextPart()
		if err != nil {
			addError(fmt.Sprintf("Error reading multipart section: %v", err))
			break
		}
		if part == nil {
			break
		}

		contentDisposition, params := mime.ParseMediaType(part.Header["Content-Disposition"])
		if contentDisposition != "form-data" {
			addError(fmt.Sprintf("Expected Content-Disposition of \"form-data\"; got %q", contentDisposition))
			break
		}

		formName := params["name"]
		ref := blobref.Parse(formName)
		if ref == nil {
			addError(fmt.Sprintf("Ignoring form key %q", formName))
			continue
		}

		_, hasContentType := part.Header["Content-Type"]
		if !hasContentType {
			addError(fmt.Sprintf("Expected Content-Type header for blobref %s; see spec", ref))
			continue
		}

		_, hasFileName := params["filename"]
		if !hasFileName {
			addError(fmt.Sprintf("Expected 'filename' Content-Disposition parameter for blobref %s; see spec", ref))
			continue
		}

		blobGot, err := receiveBlob(ref, part)
		if err != nil {
			addError(fmt.Sprintf("Error receiving blob %v: %v\n", ref, err))
			break
		}
		log.Printf("Received blob %v\n", blobGot)
		receivedBlobs = append(receivedBlobs, blobGot)
	}

	log.Println("Done reading multipart body.")
	ret := commonUploadResponse(req)

	received := make([]map[string]interface{}, 0)
	for _, got := range receivedBlobs {
		log.Printf("Got blob: %v\n", got)
		blob := make(map[string]interface{})
		blob["blobRef"] = got.blobRef.String()
		blob["size"] = got.size
		received = append(received, blob)
	}
	ret["received"] = received

	if errText != "" {
		ret["errorText"] = errText
	}

	httputil.ReturnJson(conn, ret)
}
Пример #7
0
func Handler(c http.ResponseWriter, r *http.Request) {

	c.Header().Set("Content-Type", "application/json")

	api_key := r.FormValue("api_key")
	//base_url := r.FormValue("base_url")

	if api_key != Cfg.ApiKey {
		write(c, jsonError("failed", "Invalid api key"))
		return
	}

	requested_url := r.FormValue("u")
	if requested_url == "" {

		r.ParseForm()
		file_name := r.FormValue("file_name")

		if file_name == "" {
			write(c, jsonError("failed", "no `file_name` parameter"))
			return
		}

		fmt.Printf("file_name: %v\n", file_name)

		file, err := r.MultipartReader()
		if err != nil {
			write(c, jsonError("failed", "cannot get multipart reader"))
			return
		}

		part, err := file.NextPart()
		if err != nil {
			write(c, jsonError("failed", "no `u` nor `file`"))
			return
		}
		var data [1000]byte
		var i int = 0
		var data_size int64 = 0
		md5ed := hmac.NewMD5([]byte("cdnized-2194"))
		abs_path := "/tmp/" + RandStrings(100)
		dst_file, err := os.OpenFile(abs_path, os.O_WRONLY|os.O_CREATE, 0755)
		if err != nil {
			anlog.Error("Cannot create file `%s`. error: %s\n", abs_path, err.String())
			write(c, jsonError("failed", fmt.Sprintf("cannot create temporary data. %v\n", err)))
			return
		}

		for data_size < r.ContentLength {
			i, err = part.Read(data[0:999])
			if err != nil {
				break
			}

			_, err := md5ed.Write(data[0:i])
			if err != nil {
				anlog.Error("Cannot calculate MD5 hash")
				write(c, jsonError("failed", "cannot calculate checksum"))
				break
			}

			_, err = dst_file.Write(data[0:i])
			if err != nil {
				anlog.Error("Cannot write %d bytes data in file `%s`. error: %s\n", data_size, abs_path, err.String())
			}

			data_size += int64(i)
		}

		dst_file.Close()

		//fmt.Printf("content-length: %v, file: %v, file-length: %v, i: %v\n", r.ContentLength, string(data[0:]), i, i)

		hash := fmt.Sprintf("%x", md5ed.Sum())
		file_ext := path.Ext(file_name)
		file_name = hash + RandStrings(9) + file_ext
		new_path, err := os.Getwd()

		new_path = path.Join(new_path, Cfg.StoreDir[2:], Cfg.ApiStorePrefix, file_name)

		if err != nil {
			anlog.Error("Cannot getwd\n")
			write(c, jsonError("failed", "internal error"))
			return
		}

		//fmt.Printf("abs_path: %v, new_path: %v\n", abs_path, new_path)
		if err := syscall.Rename(abs_path, new_path); err != 0 {
			anlog.Error("Cannot move from file `%s` to `%s`. %v.\n", abs_path, new_path, err)
			write(c, jsonError("failed", "internal error"))
			return
		}

		cdnized_url := fmt.Sprintf("http://%s/%s/%s/%s", Cfg.CdnServerName, Cfg.StoreDir[2:], Cfg.ApiStorePrefix, file_name)

		anlog.Info("cdnized_url: %s\n", cdnized_url)

		os.Remove(abs_path)

		type success struct {
			Status      string
			Size        int64
			Cdnized_url string
		}

		write(c, Jsonize(&success{"ok", data_size, cdnized_url}))
		return
	}

	//write(c, fmt.Sprintf("{Status: 'ok', url_path: '%s', gen: '%s'}", requested_url, x))

	file_ext := path.Ext(requested_url)
	abs_path, _ := os.Getwd()
	abs_path = path.Join(abs_path, Cfg.StoreDir[2:], Cfg.ApiStorePrefix, RandStrings(64)+file_ext)

	fmt.Printf("abs_path: %s\n", abs_path)

	var data []byte
	rv, lm, tsize := downloader.Download(requested_url, abs_path, true, &data)
	if rv != true {
		write(c, jsonError("failed", "Cannot fetch from source url"))
		return
	}

	md5ed := hmac.NewMD5([]byte("cdnized-2194"))
	for {
		brw, err := md5ed.Write(data)
		if err != nil {
			anlog.Error("Cannot calculate MD5 hash")
			write(c, jsonError("failed", "Internal error"))
			return
		}
		if brw >= tsize {
			break
		}
	}

	hash := fmt.Sprintf("%x", md5ed.Sum())
	dir, _ := path.Split(abs_path)
	file_name := hash + RandStrings(8) + file_ext
	new_path := path.Join(dir, file_name)

	if err := syscall.Rename(abs_path, new_path); err != 0 {
		anlog.Error("Cannot rename from file `%s` to `%s`", abs_path, new_path)
		write(c, jsonError("failed", "Internal error"))
		return
	}

	cdnized_url := fmt.Sprintf("http://%s/%s/%s/%s", Cfg.CdnServerName, Cfg.StoreDir[2:], Cfg.ApiStorePrefix, file_name)

	anlog.Info("cdnized_url: %s", cdnized_url)

	type success struct {
		Status      string
		Lm          string
		Size        int
		Original    string
		Cdnized_url string
	}

	write(c, Jsonize(&success{"ok", lm, tsize, requested_url, cdnized_url}))
}
Пример #8
0
func AdminController(w http.ResponseWriter, r *http.Request) {
	View.Blog = 0
	View.Rubric = 0
	View.Article = 0
	View.Server = 0
	View.Theme = 0
	View.Global = 0
	View.Resource = 0

	rd, err := r.MultipartReader()
	if err == nil {
		r.Form = make(map[string][]string)
		for {
			pt, _ := rd.NextPart()
			if pt == nil {
				break
			}
			var fh [1]string
			fd, _ := ioutil.ReadAll(pt)
			fh[0] = strings.TrimSpace(string(fd))
			r.Form[strings.TrimSpace(pt.FormName())] = fh[0:1]
		}
	}
	// Originalpfad der Url zwischenspeichern und nach Redirect widerherstellen
	route := r.FormValue("Route")
	if route != "" {
		orig := r.URL.Path
		r.URL.Path = route
		View.Master.HandleForm(route, w, r)
		r.URL.Path = orig
	}

	dir, file := path.Split(r.URL.Path)
	ids := strings.Split(file, ",", -1)

	if !sort.IsSorted(View.Blogs) {
		sort.Sort(View.Blogs)
	}
	if !sort.IsSorted(View.Themes) {
		sort.Sort(View.Themes)
	}
	if !sort.IsSorted(View.Globals) {
		sort.Sort(View.Globals)
	}

	kind := strings.Replace(dir, "/", "", -1)
	switch kind {
	case "blogs", "newrubrics":
		View.Blog, _ = strconv.Atoi(ids[0])
	case "rubrics", "newarticles":
		View.Blog, _ = strconv.Atoi(ids[0])
		View.Rubric, _ = strconv.Atoi(ids[1])
	case "articles":
		View.Blog, _ = strconv.Atoi(ids[0])
		View.Rubric, _ = strconv.Atoi(ids[1])
		View.Article, _ = strconv.Atoi(ids[2])
	case "servers":
		View.Server, _ = strconv.Atoi(ids[0])
	case "globals":
		View.Global, _ = strconv.Atoi(ids[0])
	case "themes", "newresources":
		View.Theme, _ = strconv.Atoi(ids[0])
	case "resources":
		View.Theme, _ = strconv.Atoi(ids[0])
		View.Resource, _ = strconv.Atoi(ids[1])
	case "":
		kind = "admin"
	}

	w.SetHeader("Content-Type", "text/html; charset=utf-8")
	w.SetHeader("Content-Encoding", "gzip")
	AdminDispatch(w, kind)
}