Example #1
0
func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {

	ext, err := fileFilter(req, hdr)
	if err != nil {
		return "", err
	}
	name := getSha(mpf) + `.` + ext

	rand.Seed(time.Now().Unix())
	x := rand.Intn(3)
	fmt.Println(x)

	var pfx string
	switch x {
	case 0:
		pfx = "bob/"
	case 1:
		pfx = "james/"
	case 2:
		pfx = "stacey/"
	}

	name = pfx + name

	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, putFile(ctx, name, mpf)
}
Example #2
0
/**
takes a multipart file. Does not close it. creates a thumbnail and returns the file path
*/
func ThumbnailMultipart(file multipart.File, fileName string) (string, error) {
	var (
		err      error
		img      image.Image
		thumbImg *image.NRGBA
	)
	if _, err := file.Seek(0, 0); err != nil {
		log.Printf("failed to seek to beginning of img " + err.Error())
		return "", err
	}
	var thumbPath string = "/tmp/" + time.Now().String() + fileName
	img, _, err = image.Decode(file)
	if nil != err {
		log.Printf("failed to decode img " + err.Error())
		return "", err
	}

	thumbImg = imaging.Thumbnail(img, 300, 300, imaging.Lanczos)

	out, err := os.Create(thumbPath)

	if nil != err {
		log.Printf("failed to create thumb path " + err.Error())
		return "", err
	}

	defer out.Close()

	// write new image to file
	err = jpeg.Encode(out, thumbImg, nil)

	return thumbPath, err
}
Example #3
0
func generateThumbnails(file multipart.File, userId, pathPrefix string, bucket *s3.Bucket) error {
	file.Seek(0, 0)

	img, _, err := image.Decode(file)
	if err != nil {
		log.Println("Error decoding image", err)
		return err
	}

	var buf bytes.Buffer
	for _, i := range THUMBNAIL_SIZES {
		resized := resize.Resize(uint(i), 0, img, resize.Lanczos3)
		err = jpeg.Encode(&buf, resized, nil)
		if err != nil {
			return err
		}
		path := fmt.Sprintf("%s/%d.jpg", pathPrefix, i)
		err = bucket.Put(path, buf.Bytes(), "image/jpeg", s3.PublicRead)
		if err != nil {
			return err
		}
		buf.Reset()
	}

	return err
}
Example #4
0
// StoreRelease saves what was uploaded. Buffered in case of large files.
func (r Release) StoreRelease(data multipart.File) {
	f, err := os.Create(r.ReleaseFilePath())
	if err != nil {
		log.Println("Can't open file: " + r.ReleaseFilePath())
		return
	}
	defer f.Close()

	buf := make([]byte, 1024)

	for {
		n, err := data.Read(buf)

		if err != nil && err != io.EOF {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}

		if n == 0 {
			break
		}

		if _, err := f.Write(buf[:n]); err != nil {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}
	}
}
Example #5
0
func (s ConsoleServer) Start() {
	// here is where I would specify the console interface if I actually planned on using the
	// from the command line.  For now it will just expect a command to look like `./calhoun
	// -ui=cli upload file=/path/to/file`
	url := s.Args[0]
	filepath := strings.SplitAfter(s.Args[1], "=")[1] // file=/path/to/file, so I need everything after the =

	var file multipart.File

	switch url {
	case "upload":
		file, err := os.Open(filepath)
		defer file.Close()

		if err != nil {
			log.Fatal("error reading photo upload: ", err)
		}
	default:
		log.Fatal("invalid command: `", url, "`")
	}

	route := s.routeWithPath(url)
	baseHandler := s.App.LookupHandler(route.Action)
	calhounHandler := route.ApplyMiddlewareToBase(baseHandler)
	request := CalhounRequest{UploadFile: &file}

	calhounHandler(os.Stdout, &request)
}
Example #6
0
//Execute the file save process and return the result
func (f *filerSaver) Exec() (path string, status int, err error) {
	if _, err := os.Stat(f.path); err == nil {
		if err := os.MkdirAll(f.path, 0755); err == nil {
			var file multipart.File
			file, f.header, err = f.request.FormFile(f.key)
			if file != nil {
				defer file.Close()
			}
			if err == nil {
				fname := f.nameSetter(f.header.Filename)
				var fwriter *os.File
				path = filepath.Join(f.path, fname)
				fwriter, err = os.Create(path)
				if err == nil {
					defer fwriter.Close()
					_, err = io.Copy(fwriter, file)
				} else {
					status = SAVEFILE_COPY_ERROR
				}
			} else {
				status = SAVEFILE_FORMFILE_ERROR
			}
		} else {
			status = SAVEFILE_CREATE_DIR_ERROR
		}
	} else {
		status = SAVEFILE_STATE_DIR_ERROR
	}
	return
}
Example #7
0
//Execute the file save process and return the result
func (f *filerSaver) Exec() (path string, status int, err error) {
	// bug  if the path's stat is not ok, try make this dir.
	if _, err := os.Stat(f.path); err != nil {
		if err1 := os.MkdirAll(f.path, 0755); err1 != nil {
			//make dir fail,return status and err
			status = SAVEFILE_CREATE_DIR_ERROR
			return "", status, err1
		}
	}
	var file multipart.File
	file, f.header, err = f.request.FormFile(f.key)
	if file != nil {
		defer file.Close()
	}
	if err == nil {
		if fname, err := f.nameSetter(f.header.Filename); err == nil {
			var fwriter *os.File
			path = filepath.Join(f.path, fname)
			fwriter, err = os.Create(path)
			if err == nil {
				defer fwriter.Close()
				_, err = io.Copy(fwriter, file)
			} else {
				status = SAVEFILE_COPY_ERROR
			}
		} else {
			status = SAVEFILE_RENAME_ERROR_BY_USER
		}
	} else {
		status = SAVEFILE_FORMFILE_ERROR
	}
	return
}
Example #8
0
// Create takes the Profile Folder, a content-type (passed to S3), and the binary file
// data, and saves the file to S3, then saving the S3 information to the database.
// It returns the number of bytes sent to S3, for no particular reason, and any error.
func (p *Photo) Create(folder, ctype string, file multipart.File) (int64, error) {
	end, err := file.Seek(0, 2)
	if err != nil {
		return 0, err
	}
	_, err = file.Seek(0, 0)
	if err != nil {
		return 0, err
	}
	// ensure that folder actually exists in S3, here!
	b := s3Photos.Bucket(BaseBucket + "/" + folder)
	// ensure that folder exists... this doesn't error if it's already there
	err = b.PutBucket(s3.Private)
	if err != nil {
		return 0, err
	}
	err = b.PutReader(p.Href, file, end, ctype, s3.Private)
	if err != nil {
		return 0, err
	}

	err = dbmap.Insert(p)
	if err != nil {
		return 0, err
	}
	return end, nil
}
Example #9
0
func (s *Server) upload(wr http.ResponseWriter, r *http.Request) {
	var (
		vid    int64
		key    int64
		cookie int64
		size   int64
		err    error
		str    string
		v      *volume.Volume
		n      *needle.Needle
		file   multipart.File
		res    = map[string]interface{}{}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), &err, res)
	if err = checkContentLength(r, s.conf.NeedleMaxSize); err != nil {
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	str = r.FormValue("key")
	if key, err = strconv.ParseInt(str, 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	str = r.FormValue("cookie")
	if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		log.Errorf("r.FormFile() error(%v)", err)
		err = errors.ErrInternal
		return
	}
	if size, err = checkFileSize(file, s.conf.NeedleMaxSize); err == nil {
		n = s.store.Needle()
		if err = n.WriteFrom(key, int32(cookie), int32(size), file); err == nil {
			if v = s.store.Volumes[int32(vid)]; v != nil {
				err = v.Write(n)
			} else {
				err = errors.ErrVolumeNotExist
			}
		}
		s.store.FreeNeedle(n)
	}
	file.Close()
	return
}
Example #10
0
File: http_api.go Project: qida/bfs
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		v                *Volume
		n                int
		vid, key, cookie int64
		err              error
		buf              []byte
		file             multipart.File
		res              = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if key, err = strconv.ParseInt(r.FormValue("key"), 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("key"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if cookie, err = strconv.ParseInt(r.FormValue("cookie"), 10, 64); err !=
		nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("cookie"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	buf = v.Buffer()
	if n, err = file.Read(buf); err == nil {
		err = v.Add(key, cookie, buf[:n])
	}
	file.Close()
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
Example #11
0
func UploadProfilePic(wr http.ResponseWriter, req *http.Request) {
	var (
		profileImgLoc string
		id            int64
		err           error
		p             *data.Profile
		file          multipart.File
		header        *multipart.FileHeader
		enc           *json.Encoder
	)

	enc = json.NewEncoder(wr)
	params := mux.Vars(req)
	id, err = strconv.ParseInt(params["id"], 10, 64)
	p, err = data.FindProfileById(id)
	req.ParseMultipartForm(10 << 20) //approx 10MB
	file, header, err = req.FormFile("file")

	handleUploadErr := func(err error, status int) {
		if nil != err {
			wr.WriteHeader(status)
			enc.Encode(NewErrorJSON(err.Error(), status))
		}
	}

	if err != nil {
		log.Println("error upload pic " + err.Error())
		handleUploadErr(err, http.StatusBadRequest)
		return
	}
	defer file.Close()
	uploadedFilePath, err := service.SaveUploadedFile(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	uploadedFilePath, err = service.ThumbnailMultipart(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	profileImgLoc, err = data.PutInBucket(uploadedFilePath, header.Filename)

	if err != nil {
		log.Println("failed up upload to s3 " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	p.UpdateProfilePic(profileImgLoc)
	enc.Encode(p)

}
func saveUpload(file multipart.File, handler *multipart.FileHeader) {
	defer file.Close()
	fmt.Printf("Uploaded file info: %#v", handler.Header)
	localFilename := fmt.Sprintf("./uploads/%v.%v", handler.Filename, submissions.NewToken())
	f, err := os.OpenFile(localFilename, os.O_WRONLY|os.O_CREATE, 0666)
	checkError(err)
	defer f.Close()
	_, err = io.Copy(f, file)
	checkError(err)
}
Example #13
0
func getMultipart(r *knot.WebContext, fileName string) (server colonycore.ServerByType, payload colonycore.FileBrowserPayload, err error) {
	var tmp map[string]interface{}
	_, s, err := r.GetPayloadMultipart(&tmp)

	if err != nil {
		return
	}
	payload.ServerId = s["serverId"][0]
	payload.ServerType = s["serverType"][0]
	payload.Path = s["path"][0]

	err = r.Request.ParseMultipartForm(100000)
	if err != nil {
		return
	}

	m := r.Request.MultipartForm
	files := m.File[fileName]

	for key := range files {
		var file multipart.File
		file, err = files[key].Open()
		defer file.Close()
		if err != nil {
			return
		}

		payload.File = append(payload.File, file)
		payload.FileName = append(payload.FileName, s["filename"][key])

		tmpSize, _ := strconv.ParseInt(s["filesizes"][key], 10, 64)
		payload.FileSizes = append(payload.FileSizes, tmpSize)
	}

	query := dbox.Eq("_id", payload.ServerId)

	cursor, err := colonycore.Find(new(colonycore.Server), query)
	if err != nil {
		return
	}

	data := []colonycore.ServerByType{}
	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return
	}
	defer cursor.Close()

	if len(data) != 0 {
		server = data[0]
		server.ServerType = payload.ServerType
	}

	return
}
Example #14
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, session *sessions.Session) {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	addPhoto(fName, session)
}
Example #15
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	return addPhoto(fName, c)
}
Example #16
0
func uploadPhoto(m map[string]string, src multipart.File, hdr *multipart.FileHeader) map[string]string {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	addPhoto(m, fName)
	return m
}
Example #17
0
func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {

	ext, err := fileFilter(req, hdr)
	if err != nil {
		return "", err
	}
	name := getSha(mpf) + `.` + ext
	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, putFile(ctx, name, mpf)
}
Example #18
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
	defer src.Close()
	fName := getSha(src) + filepath.Ext(hdr.Filename)
	wd, _ := os.Getwd()
	m := Model(c)
	path := filepath.Join(wd, "assets", "imgs", m.Name, fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	return addPhoto("/imgs/"+m.Name+"/"+fName, c)
}
Example #19
0
func storeFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {
	ext, err := fileType(req, hdr) //get type of file based on file extension

	if err != nil {
		return "", err
	}

	name := getSha(mpf) + `.` + ext //give unique name to the file
	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, store(ctx, name, mpf)
}
Example #20
0
func cropField(field *gorm.Field, scope *gorm.Scope) (cropped bool) {
	if field.Field.CanAddr() {
		// TODO Handle scanner
		if media, ok := field.Field.Addr().Interface().(Media); ok && !media.Cropped() {
			option := parseTagOption(field.Tag.Get("media_library"))
			if media.GetFileHeader() != nil || media.NeedCrop() {
				var file multipart.File
				var err error
				if fileHeader := media.GetFileHeader(); fileHeader != nil {
					file, err = media.GetFileHeader().Open()
				} else {
					file, err = media.Retrieve(media.URL("original"))
				}

				if err != nil {
					scope.Err(err)
					return false
				}

				media.Cropped(true)

				if url := media.GetURL(option, scope, field, media); url == "" {
					scope.Err(errors.New("invalid URL"))
				} else {
					result, _ := json.Marshal(map[string]string{"Url": url})
					media.Scan(string(result))
				}

				if file != nil {
					defer file.Close()
					var handled = false
					for _, handler := range mediaHandlers {
						if handler.CouldHandle(media) {
							file.Seek(0, 0)
							if scope.Err(handler.Handle(media, file, option)) == nil {
								handled = true
							}
						}
					}

					// Save File
					if !handled {
						scope.Err(media.Store(media.URL(), option, file))
					}
				}
				return true
			}
		}
	}
	return false
}
Example #21
0
func SaveUploadedFile(file multipart.File, fileName string) (string, error) {
	tmpFile := "/tmp/" + fileName
	f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		log.Println("error uploading pic " + err.Error())
		return "", err
	}
	_, err = io.Copy(f, file)
	if err != nil {
		log.Println("failed to copy file  " + err.Error())
		return "", err
	}
	file.Close()
	f.Close()
	return tmpFile, err
}
Example #22
0
func decodePNG(f multipart.File) (*Image, error) {
	var image Image

	ic, err := png.DecodeConfig(f)
	_, err = f.Seek(0, 0)
	i, err := png.Decode(f)

	if err != nil {
		return nil, fmt.Errorf("Could not decode PNG file.")
	}

	image.Config = ic
	image.Image = i

	return &image, nil
}
Example #23
0
func createFileInFS(file multipart.File, extension string) (string, error) {
	defer file.Close()

	newfilename := randomString()
	imgfile, err := os.Create("tmp/" + newfilename + extension)
	if err != nil {
		return "", err
	}

	defer imgfile.Close()

	_, err = io.Copy(imgfile, file)
	if err != nil {
		return "", err
	}
	return newfilename, nil
}
Example #24
0
/** Move upload file to a folder. */
func MoveFile(uploadFile multipart.File, newPath string) error {
	/* Condition validation */
	if uploadFile == nil {
		return fmt.Errorf("Upload file is empty.")
	}
	defer uploadFile.Close()

	// Create new file
	output, err := os.Create(newPath)
	if err != nil {
		return err
	}
	defer output.Close()

	// Move file to new location
	_, err = io.Copy(output, uploadFile)
	return err
}
Example #25
0
func fileExceed(file multipart.File, limitSize int64) bool {
	var isExceed bool
	read_buf := make([]byte, 1024)
	var pos int64 = 0
	for {
		n, err := file.ReadAt(read_buf, pos)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if n == 0 {
			break
		}
		pos = pos + (int64)(n)
		if pos > limitSize {
			isExceed = true
			break
		}
	}
	return isExceed
}
Example #26
0
func judgeContentType(f multipart.File) (string, string, error) {
	f.Seek(0, 0)
	buf := make([]byte, 16)
	count, err := f.Read(buf)
	if err != nil {
		return "", "", err
	}

	if count >= 10 && string(buf[6:10]) == "JFIF" {
		return "image/jpeg", ".jpg", nil
	}
	if count >= 4 && string(buf[0:3]) == "GIF" {
		return "image/gif", ".gif", nil
	}
	if count >= 2 && string(buf[1:4]) == "PNG" {
		return "image/png", ".png", nil
	}

	return "application/octet-stream", "", nil
}
Example #27
0
func uploadFile(file multipart.File) string {
	clientid := os.Getenv("DROPBOX_KEY")
	clientsecret := os.Getenv("DROPBOX_TOKEN")
	token := os.Getenv("DROPBOX_ACCESS_TOKEN")

	DB := dropbox.NewDropbox()
	DB.SetAppInfo(clientid, clientsecret)
	DB.SetAccessToken(token)

	filename := fmt.Sprintf("%s.png", strconv.FormatInt(rand.Int63(), 32))
	filepath := fmt.Sprintf("/Public/Screenshots/%s", filename)

	var tmpstr []byte
	length, _ := file.Read(tmpstr)
	DB.FilesPut(file, int64(length), filepath, true, "")

	account, _ := DB.GetAccountInfo()
	uid := strconv.FormatInt(int64(account.UID), 10)

	return fmt.Sprintf("https://dl.dropbox.com/u/%s/Screenshots/%s", uid, filename)
}
Example #28
0
func writeToGridFile(file multipart.File, gridFile *mgo.GridFile) error {
	reader := bufio.NewReader(file)
	defer func() { file.Close() }()
	// make a buffer to keep chunks that are read
	buf := make([]byte, 1024)
	for {
		// read a chunk
		n, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			return errors.New("Could not read the input file")
		}
		if n == 0 {
			break
		}
		// write a chunk
		if _, err := gridFile.Write(buf[:n]); err != nil {
			return errors.New("Could not write to GridFs for " + gridFile.Name())
		}
	}
	gridFile.Close()
	return nil
}
Example #29
0
func isSupported(file multipart.File) (bool, string, error) {
	buff := make([]byte, 512)
	_, err := file.Read(buff)

	// Reset the file
	file.Seek(0, 0)

	if err != nil {
		return false, "", err
	}

	filetype := http.DetectContentType(buff)

	for _, i := range supportedFileTypes {
		if i == filetype {
			return true, filetype, nil
		}
	}

	log.Println("File not supported: " + filetype)
	return false, filetype, nil
}
Example #30
0
// ReadRequestFiles reads the files from the request, and calls ReaderToFile on them
func ReadRequestFiles(r *http.Request) (filenames []string, status int, err error) {
	defer r.Body.Close()
	err = r.ParseMultipartForm(1 << 20)
	if err != nil {
		status, err = 405, errors.New("cannot parse request as multipart-form: "+err.Error())
		return
	}
	if r.MultipartForm == nil || len(r.MultipartForm.File) == 0 {
		status, err = 405, errors.New("no files?")
		return
	}

	filenames = make([]string, 0, len(r.MultipartForm.File))
	var f multipart.File
	var fn string
	for _, fileHeaders := range r.MultipartForm.File {
		for _, fileHeader := range fileHeaders {
			if f, err = fileHeader.Open(); err != nil {
				status, err = 405, fmt.Errorf("error reading part %q: %s", fileHeader.Filename, err)
				return
			}
			Log.Debug("part", "filename", fileHeader.Filename)
			if fn, err = temp.ReaderToFile(f, fileHeader.Filename, ""); err != nil {
				f.Close()
				status, err = 500, fmt.Errorf("error saving %q: %s", fileHeader.Filename, err)
				return
			}
			f.Close()
			filenames = append(filenames, fn)
		}
	}
	if len(filenames) == 0 {
		status, err = 405, errors.New("no files??")
		return
	}
	status = 200
	return
}