Esempio n. 1
0
func uploadHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		req.ParseMultipartForm(32 << 20)

		/* Retrieve the file */
		file, fileheader, err := req.FormFile("uploadFile")
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}

		/* Retrieve:
		*		- file extension
		*		- title: if there is a custom title, it will be used, otherwise the
								   current file name will be used
		*		- time to live (per time), if set
		*		- time to live (per views), if set
		*/
		fileExt := strings.Split(fileheader.Filename, ".")[1]
		var title string
		if req.FormValue("title") != "" {
			title = req.FormValue("title")
		} else {
			title = strings.Split(fileheader.Filename, ".")[0]
		}
		var ttlTime int64
		var ttlViews int64
		if req.FormValue("ttlTime") != "" {
			ttlTime, err = strconv.ParseInt(req.FormValue("ttlTime"), 10, 64)
			if err != nil {
				cio.PrintMessage(1, err.Error())
			}
		} else {
			ttlTime = 60 * 24 * 7 /*2 weeks (default)*/
		}
		if req.FormValue("ttlViews") != "" {
			/* + 1 at the end to allow an additional view
			 * because the user automatically gets redirected
			 * to the image page when uploading through the website
			 * and therefore uses one view
			 */
			ttlViews, err = strconv.ParseInt(req.FormValue("ttlViews"), 10, 64)
			ttlViews++
			if err != nil {
				cio.PrintMessage(1, err.Error())
			}
		} else {
			ttlViews = -1
		}

		defer file.Close()
		var id string
		created := false
		for created != true {
			id = generateImageID()
			err = db.CheckIfImageIDInUse(id)
			if err != nil {
				if err.Error() == "Image ID '"+id+"' exists.'" {
					created = false
					continue
				} else {
					created = false
					cio.PrintMessage(1, err.Error())
					return
				}
			}
			created = true
		}

		/* Generate the path where the image will be stored
		 * from the image storage directory path + the image id (generated by generateImageID()) +
		 * file extension
		 */
		filePath := fsys.ImgStoragePath + id + "." + fileExt

		/* Store the image
		 * Options:
		 *		- custom title
		 *		- custom time to live (per time)
		 *		- custom time to live (per views)
		 */
		err = db.StoreImage(id, title, filePath, fileExt, ttlTime, ttlViews)
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}

		bytesCopied, err := fsys.StoreImage(filePath, file)
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}
		cio.PrintMessage(0, "File "+filePath+" has been created.")
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}
		cio.PrintMessage(0, "Content of uploaded image ("+strconv.FormatInt(bytesCopied, 10)+" Bytes) has been copied to "+filePath+".")
		http.Redirect(w, req, "/"+id, http.StatusFound)
	}
}
Esempio n. 2
0
func apiUploadHandler(w http.ResponseWriter, req *http.Request) {
	cio.PrintMessage(0, "Request from pixmate client...")
	if req.Method == "POST" {
		req.ParseMultipartForm(32 << 20)

		/* Retrieve the file */
		file, fileheader, err := req.FormFile("image")
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}

		/* Retrieve:
		*		- file extension
		*		- title: if there is a custom title, it will be used, otherwise the
								   current file name will be used
		*		- time to live (per time), if set
		*		- time to live (per views), if set
		*/
		fileExt := strings.Split(fileheader.Filename, ".")[1]
		var title string
		if req.FormValue("title") != "" {
			title = req.FormValue("title")
		} else {
			title = strings.Split(fileheader.Filename, ".")[0]
		}
		ttlTime, err := strconv.ParseInt(req.FormValue("ttltime"), 10, 64)
		if err != nil {
			cio.PrintMessage(1, err.Error())
		}
		ttlViews, err := strconv.ParseInt(req.FormValue("ttlviews"), 10, 64)
		if err != nil {
			cio.PrintMessage(1, err.Error())
		}

		defer file.Close()
		var id string
		created := false
		for created != true {
			id = generateImageID()
			err = db.CheckIfImageIDInUse(id)
			if err != nil {
				if err.Error() == "Image ID '"+id+"' exists.'" {
					created = false
					continue
				} else {
					created = false
					cio.PrintMessage(1, err.Error())
					return
				}
			}
			created = true
		}

		/* Generate the path where the image will be stored
		 * from the image storage directory path + the image id (generated by generateImageID()) +
		 * file extension
		 */
		filePath := fsys.ImgStoragePath + id + "." + fileExt

		/* Store the image
		 * Options:
		 *		- custom title
		 *		- custom time to live (per time)
		 *		- custom time to live (per views)
		 */
		err = db.StoreImage(id, title, filePath, fileExt, ttlTime, ttlViews)
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}

		bytesCopied, err := fsys.StoreImage(filePath, file)
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}
		cio.PrintMessage(0, "File "+filePath+" has been created.")
		if err != nil {
			cio.PrintMessage(1, err.Error())
			return
		}
		cio.PrintMessage(0, "Content of uploaded image ("+strconv.FormatInt(bytesCopied, 10)+" Bytes) has been copied to "+filePath+".")
		w.Write([]byte(id))
	}

}