예제 #1
5
파일: web.go 프로젝트: joshuarubin/goscribe
func s3Upload(basePath string, data *multipart.FileHeader) (string, error) {
	if s3Bucket == nil {
		return "", fmt.Errorf("upload unavailable")
	}

	fileType := data.Header.Get("Content-Type")

	file, err := data.Open()
	defer file.Close()

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

	fileData, err := ioutil.ReadAll(file)
	if err != nil {
		return "", err
	}

	h := sha1.New()
	if _, err := h.Write(fileData); err != nil {
		return "", err
	}

	filePath := fmt.Sprintf("/%s/%x", basePath, h.Sum(nil))

	if err := s3Bucket.Put(filePath, fileData, fileType, s3.PublicRead); err != nil {
		return "", err
	}

	return s3BaseURL + filePath, nil
}
예제 #2
1
파일: server.go 프로젝트: kiloe/blobstore
// Copy form file to Blob
func upload(f *multipart.FileHeader) (b *blob.Blob, err error) {
	// Open
	upload, err := f.Open()
	if err != nil {
		return
	}
	defer upload.Close()
	// Create blob
	b = blob.New()
	// Set filename from request
	b.Name = f.Filename
	// Set content-type from request
	if ct := f.Header.Get("Content-Type"); ct != "" {
		b.ContentType = ct
	}
	// Guess content-type from extension if missing
	if b.ContentType == "" || b.ContentType == ApplicationOctetStream {
		if ext := filepath.Ext(b.Name); ext != "" {
			b.ContentType = mime.TypeByExtension(ext)
		}
	}
	if b.ContentType == "" {
		b.ContentType = ApplicationOctetStream
	}
	// Write
	err = b.WriteFrom(upload)
	if err != nil {
		return
	}
	return
}
예제 #3
1
func headerData(h *multipart.FileHeader) string {
	file, err := h.Open()
	if err != nil {
		return "ERROR: " + err.Error()
	}
	data, err := ioutil.ReadAll(file)
	file.Close()
	if err != nil {
		return "ERROR: " + err.Error()
	}
	return string(data)
}
예제 #4
0
파일: files.go 프로젝트: hypertornado/prago
func loadFile(folder, path string, header *multipart.FileHeader) error {
	if !strings.HasPrefix(path, folder) {
		return errors.New("folder path should be prefix of path")
	}
	err := os.MkdirAll(folder, 0777)
	if err != nil {
		return err
	}

	inFile, err := header.Open()
	if err != nil {
		return err
	}

	outFile, err := os.Create(path)
	if err != nil {
		return err
	}

	io.Copy(outFile, inFile)

	defer outFile.Close()
	defer inFile.Close()
	return nil
}
예제 #5
0
func openFileHeader(t *testing.T, fh *multipart.FileHeader) multipart.File {
	var r, err = fh.Open()
	if err != nil {
		t.Error(err)
	}
	return r
}
예제 #6
0
파일: file.go 프로젝트: x4rMa/admin-1
func (f *FileField) HandleFile(file *multipart.FileHeader) (string, error) {
	reader, err := file.Open()
	if err != nil {
		return "", err
	}

	filename := file.Filename
	if len(f.UploadTo) > 0 {
		_, err := os.Stat(f.UploadTo)
		if err != nil {
			os.MkdirAll(f.UploadTo, 0777)
		}
		filename = fmt.Sprintf("%v/%v", f.UploadTo, file.Filename)
	}

	dst, err := os.Create(filename)
	if err != nil {
		return "", err
	}

	io.Copy(dst, reader)
	dst.Close()
	reader.Close()
	return filename, nil
}
예제 #7
0
파일: files.go 프로젝트: intfrr/sendto
// SaveFile saves files to disk
func (m *File) saveFile(fh *multipart.FileHeader) error {

	// Retreive the form image data by opening the referenced tmp file.
	f, err := fh.Open()
	if err != nil {
		return err
	}

	// If we have no path, set it to a default value /files/documents/id/name
	if len(m.Path) == 0 {
		err = m.NewFilePath(fh)
		if err != nil {
			return err
		}
	}

	// Make sure our path exists first
	err = file.CreatePathTo(m.Path)
	if err != nil {
		return err
	}

	// Write out our file to disk
	return file.Save(f, m.Path)

}
예제 #8
0
func (c *appContext) uploadPic(a *multipart.FileHeader) (string, string) {
	log.Println("In upload pic territory")

	bucket := c.bucket

	file, err := a.Open()
	defer file.Close()
	if err != nil {
		panic(err.Error())
	}

	if err != nil {
		panic(err)
	}

	buf, _ := ioutil.ReadAll(file)

	fn := uuid.New()
	fname := "places/" + fn + path.Ext(a.Filename)
	thumbname := "placesthumb/" + fn + path.Ext(a.Filename)
	log.Println(fname)

	b := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + fname
	d := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + thumbname

	filetype := http.DetectContentType(buf)

	err = bucket.Put(fname, buf, filetype, s3.PublicRead)

	if err != nil {
		log.Println("bucket put error for main image")
		panic(err.Error())

	}
	log.Print("added a full image")
	img, err := jpeg.Decode(bytes.NewReader(buf))

	if err != nil {
		log.Println(err.Error())
	}

	m := resize.Resize(200, 200, img, resize.Lanczos2)

	buf2 := new(bytes.Buffer)
	err = jpeg.Encode(buf2, m, nil)
	if err != nil {
		fmt.Println(err.Error())
	}
	thumb := buf2.Bytes()
	filetype2 := http.DetectContentType(thumb)
	err = bucket.Put(thumbname, thumb, filetype2, s3.PublicRead)

	if err != nil {
		log.Println("bucket put error for thumbnail")
		panic(err.Error())
	}
	log.Println("uploaded one thumb image")
	return b, d

}
예제 #9
0
func saveFile(header *multipart.FileHeader, destination string) error {
	file, err := header.Open()
	if err != nil {
		return err
	}
	defer file.Close()

	dir := path.Dir(destination)
	if err = os.MkdirAll(dir, os.ModeDir|0700); err != nil {
		return err
	}

	var output *os.File
	if output, err = os.OpenFile(destination, os.O_CREATE|os.O_WRONLY, 0600); err != nil {
		fmt.Println("test:", err)
		return err
	}

	var n int64
	if n, err = io.Copy(output, file); err != nil {
		return err
	}
	log.Println("written", n, "bytes of data to file:", destination)
	return nil
}
예제 #10
0
파일: slack.go 프로젝트: tomyhero/billie
func upSlack(api *slack.Client, attachment *multipart.FileHeader) (string, error) {
	upFile, err := ioutil.TempFile("", "upSlack_")
	defer os.Remove(upFile.Name())

	f, err := attachment.Open()
	if err != nil {
		return "", fmt.Errorf("attached file open error: %v", err)
	}

	// file save
	written, err := io.Copy(upFile, f)
	if err != nil {
		return "", fmt.Errorf("file save error: %v, written: %d", err, written)
	}

	fileInfo, err := api.UploadFile(slack.FileUploadParameters{
		File:     upFile.Name(),
		Filename: attachment.Filename,
	})
	if err != nil {
		return "", fmt.Errorf("file upload error: %v", err)
	}

	return fileInfo.URL, nil
}
예제 #11
0
파일: upload.go 프로젝트: hawx/alexandria
func (h uploadHandler) doUpload(fileheader *multipart.FileHeader) error {
	file, err := fileheader.Open()
	defer file.Close()

	if err != nil {
		return err
	}

	contentType := fileheader.Header["Content-Type"][0]

	edition := &models.Edition{
		Id:          uuid.New(),
		ContentType: contentType,
	}

	dstPath := path.Join(h.bookPath, edition.Path())

	dst, err := os.Create(dstPath)
	if err != nil {
		return err
	}

	defer dst.Close()

	if _, err := io.Copy(dst, file); err != nil {
		return err
	}

	opened, err := fileheader.Open()
	if err != nil {
		return err
	}

	if contentType != models.MOBI && contentType != models.EPUB {
		return errors.New("Format not supported: " + contentType)
	}

	metaFunc := metadata.Epub
	if contentType == models.MOBI {
		metaFunc = metadata.Mobi
	}

	meta, _ := metaFunc(opened)

	newBook := models.Book{
		Id:       uuid.New(),
		Added:    time.Now(),
		Title:    meta.Title,
		Author:   meta.Author,
		Editions: models.Editions{edition},
	}

	h.db.Save(newBook)
	h.es.Add(newBook)

	go h.convert(contentType, newBook)

	return nil
}
예제 #12
0
파일: http.go 프로젝트: dullgiulio/dododada
func (s *server) postFile(f *multipart.FileHeader) error {
	fh, err := f.Open()
	if err != nil {
		return err
	}
	defer fh.Close()
	return s.cons.readAll(fh)
}
예제 #13
0
func readMultipart(upload *multipart.FileHeader) ([]byte, error) {
	file, err := upload.Open()
	if err != nil {
		return nil, err
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	return ioutil.ReadAll(reader)
}
예제 #14
0
func uploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
	file, err := imageData.Open()
	if err != nil {
		return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "")
	}
	defer file.Close()

	buf := bytes.NewBuffer(nil)
	io.Copy(buf, file)

	// make sure the file is an image and is within the required dimensions
	if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil {
		return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, err.Error())
	} else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight {
		data := buf.Bytes()
		newbuf := bytes.NewBuffer(nil)
		if info, err := model.GetInfoForBytes(imageData.Filename, data); err != nil {
			return err
		} else if info.MimeType == "image/gif" {
			if gif_data, err := gif.DecodeAll(bytes.NewReader(data)); err != nil {
				return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_decode_error", nil, "")
			} else {
				resized_gif := resizeEmojiGif(gif_data)
				if err := gif.EncodeAll(newbuf, resized_gif); err != nil {
					return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "")
				}
				if err := app.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
					return err
				}
			}
		} else {
			if img, _, err := image.Decode(bytes.NewReader(data)); err != nil {
				return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.decode_error", nil, "")
			} else {
				resized_image := resizeEmoji(img, config.Width, config.Height)
				if err := png.Encode(newbuf, resized_image); err != nil {
					return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "")
				}
				if err := app.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
					return err
				}
			}
		}
	} else {
		if err := app.WriteFile(buf.Bytes(), getEmojiImagePath(id)); err != nil {
			return err
		}
	}

	return nil
}
예제 #15
0
func WebQueueStage(file *multipart.FileHeader, uid uint32) (*PackageInfo, error) {
	return builder.Stage(file.Filename, uid, func(b *PackageBuilder, writer io.Writer) error {
		f, err := file.Open()

		if err != nil {
			return err
		}

		defer f.Close()
		_, err = io.Copy(writer, f)

		return err
	})
}
예제 #16
0
파일: file.go 프로젝트: Laller/chill
func copy(fh *multipart.FileHeader, path, fname string) error {
	buf := new(bytes.Buffer)
	file, err := fh.Open()
	if err != nil {
		return err
	}
	_, err = buf.ReadFrom(file)
	if err != nil {
		return err
	}
	err = os.MkdirAll(path, 0644)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(filepath.Join(path, fname), buf.Bytes(), os.FileMode(0644))
}
예제 #17
0
파일: mfs.go 프로젝트: icsnju/apt-mesos
func (fe *MfsFileExplorer) Upload(path string, file *multipart.FileHeader) error {
	fi, err := file.Open()
	defer fi.Close()
	if err != nil {
		return err
	}

	fileNew := NormalizePath(path + "/" + file.Filename)
	fo, err := os.OpenFile(fileNew, os.O_WRONLY|os.O_CREATE, 0777)
	defer fo.Close()
	if err != nil {
		return err
	}

	io.Copy(fo, fi)
	return nil
}
예제 #18
0
파일: file.go 프로젝트: jango2015/wk
func fileInfo(header *multipart.FileHeader) FileInfo {
	info := FileInfo{
		Name: header.Filename,
		Type: header.Header.Get("Content-Type"),
	}

	if f, err := header.Open(); err == nil {
		info.Size, err = f.Seek(0, os.SEEK_END)
		if err != nil {
			log.Println("Parse file info, seek error", err)
		}
		f.Close()
	} else {
		log.Println("Parse file info, open error", err)
	}
	return info
}
예제 #19
0
파일: main.go 프로젝트: madcat/blserver
func saveUploadForQueue(header *multipart.FileHeader, qid string) error {
	file, err := header.Open()
	defer file.Close()
	if err != nil {
		return err
	}
	os.MkdirAll("tmp/"+qid, 0777)
	dst, err := os.Create("tmp/" + qid + "/" + header.Filename)
	defer dst.Close()
	if err != nil {
		return err
	}
	if _, err := io.Copy(dst, file); err != nil {
		return err
	}
	return nil
}
예제 #20
0
func saveFile(fil *multipart.FileHeader, userPath string) {
	file, err := fil.Open()
	if err != nil {
		log.Println(err)
		return
	}
	defer file.Close()
	dest, err := os.Create(userPath + "/" + fil.Filename)
	if err != nil {
		log.Println(err)
		return
	}
	defer dest.Close()
	if _, err := io.Copy(dest, file); err != nil {
		log.Println(err)
		return
	}
}
예제 #21
0
func unpackFileHeaderData(fh *multipart.FileHeader, t *testing.T) (data string) {
	if fh == nil {
		return
	}

	f, err := fh.Open()
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	var fb bytes.Buffer
	_, err = fb.ReadFrom(f)
	if err != nil {
		t.Error(err)
	}
	return fb.String()
}
예제 #22
0
func fileHeaderContent(fh *multipart.FileHeader) string {
	if fh == nil {
		return ""
	}

	f, err := fh.Open()
	defer f.Close()
	if err != nil {
		panic("error opening FileHeader. Error:" + err.Error())
	}

	var b bytes.Buffer
	_, err = b.ReadFrom(f)
	if err != nil {
		panic("error reading from FileHeader. Error:" + err.Error())
	}

	return b.String()
}
예제 #23
0
파일: file_test.go 프로젝트: yzl11/vessel
func unpackFileHeaderData(fh *multipart.FileHeader) string {
	if fh == nil {
		return ""
	}

	f, err := fh.Open()
	if err != nil {
		panic("Could not open file header:" + err.Error())
	}
	defer f.Close()

	var fb bytes.Buffer
	_, err = fb.ReadFrom(f)
	if err != nil {
		panic("Could not read from file header:" + err.Error())
	}

	return fb.String()
}
예제 #24
0
// SaveMultipartFile saves multipart file fh under the given filename path.
func SaveMultipartFile(fh *multipart.FileHeader, path string) error {
	f, err := fh.Open()
	if err != nil {
		return err
	}
	defer f.Close()

	if ff, ok := f.(*os.File); ok {
		return os.Rename(ff.Name(), path)
	}

	ff, err := os.Create(path)
	if err != nil {
		return err
	}
	defer ff.Close()
	_, err = copyZeroAlloc(ff, f)
	return err
}
예제 #25
0
func UploadFile(realFile *multipart.FileHeader) (file *File, err error) {
	var source multipart.File
	if source, err = realFile.Open(); err != nil {
		return
	}
	defer source.Close()
	var img image.Config
	if img, _, err = image.DecodeConfig(source); err != nil {
		return
	}
	source.Seek(0, 0)
	hasher := sha1.New()
	io.Copy(hasher, source)
	fileKey := hex.EncodeToString(hasher.Sum(nil))
	file = &File{Key: fileKey}
	has, _ := engine.Get(file)
	if !has {
		var dst *os.File
		var fileType = realFile.Header.Get("content-type")
		var ext, ok = config.FILE_EXTS[fileType]
		if !ok {
			ext = ".jpg"
		}
		if dst, err = os.Create(config.UPLOADPATH + fileKey + ext); err != nil {
			return
		}
		defer dst.Close()
		source.Seek(0, 0)
		if _, err = io.Copy(dst, source); err != nil {
			return
		}

		file.Width = img.Width
		file.Height = img.Height
		file.Type = fileType

		if _, err = engine.Insert(file); err != nil {
			return
		}
	}
	return
}
예제 #26
0
func saveMediaFile(header *multipart.FileHeader, name string) error {
	ext := path.Ext(header.Filename)
	mimeType := mime.TypeByExtension(ext)
	isImage := strings.HasPrefix(mimeType, "image")
	isVideo := strings.HasPrefix(mimeType, "video")
	if (isImage || isVideo) == false {
		return nil
	}

	file, err := header.Open()
	if err != nil {
		return err
	}
	defer file.Close()

	// Create the 'content' directory if doesn't exist
	if err = prepareContentDirectory(); err != nil {
		return err
	}

	var filepath string
	if name != "" {
		filepath = "content/" + name + ext
	} else {
		filepath = "content/" + header.Filename
	}
	output, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		os.Remove(filepath)
		return err
	}
	defer output.Close()

	if _, err = io.Copy(output, file); err != nil {
		os.Remove(filepath)
		return err
	}

	return nil
}
예제 #27
0
func (uf *UploadFile) uploadFileToLocal(fileHeader *multipart.FileHeader) (err error) {
	fullpath, name := uf.getPath(fileHeader.Filename)

	file, err := fileHeader.Open()
	if err != nil {
		return err
	}
	defer file.Close()

	if err := uf.saveToLocal(file, fullpath); err != nil {
		return err
	}

	uf.files = append(uf.files, models.File{
		Name:  fileHeader.Filename,
		Url:   uf.cdn + "/" + path.Join(uf.url, name),
		Type:  uint(uf.fileType),
		RelId: uf.relId,
	})

	return nil
}
예제 #28
0
func ExtractZip(f *multipart.FileHeader, d string) error {
	file, err := f.Open()
	defer file.Close()
	if err != nil {
		return err
	}
	size, err := file.Seek(0, 2)
	if err != nil {
		return err
	}
	r, err := zip.NewReader(file, size)
	if err != nil {
		return err
	}
	for _, f := range r.File {
		err := CopyZipFile(f, d, f.Name)
		if err != nil {
			return err
		}
	}
	return nil
}
예제 #29
0
// Create inserts a new image record in the database and returns the id
func Create(params map[string]string, fh *multipart.FileHeader) (int64, error) {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Update/add some params by default
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	id, err := Query().Insert(params)

	if fh != nil && id != 0 {
		// Retreive the form image data by opening the referenced tmp file
		f, err := fh.Open()
		if err != nil {
			return id, err
		}

		// Now retrieve the image concerned, and save the file representations
		image, err := Find(id)
		if err != nil {
			return id, err
		}

		// Save files to disk using the passed in file data (if any)
		err = image.SaveImageRepresentations(f)
		if err != nil {
			return id, err
		}
	}

	return id, err
}
예제 #30
0
func uploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
	file, err := imageData.Open()
	if err != nil {
		return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "")
	}
	defer file.Close()

	buf := bytes.NewBuffer(nil)
	io.Copy(buf, file)

	// make sure the file is an image and is within the required dimensions
	if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil {
		return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, err.Error())
	} else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight {
		return model.NewLocAppError("uploadEmojiImage", "api.emoji.upload.large_image.app_error", nil, "")
	}

	if err := WriteFile(buf.Bytes(), getEmojiImagePath(id)); err != nil {
		return err
	}

	return nil
}