示例#1
0
文件: forms.go 项目: namdp06/picfit
func (f *MultipartForm) Upload(storage gostorages.Storage) (*image.ImageFile, error) {
	var fh io.ReadCloser

	fh, err := f.Data.Open()

	if err != nil {
		return nil, err
	}

	defer fh.Close()

	dataBytes := bytes.Buffer{}

	_, err = dataBytes.ReadFrom(fh)

	if err != nil {
		return nil, err
	}

	err = storage.Save(f.Data.Filename, gostorages.NewContentFile(dataBytes.Bytes()))

	if err != nil {
		return nil, err
	}

	return &image.ImageFile{
		Filepath: f.Data.Filename,
		Storage:  storage,
	}, nil
}
示例#2
0
func (f *MultipartForm) Upload(storage gostorages.Storage) (*image.ImageFile, error) {
	var fh io.ReadCloser

	fh, err := f.Data.Open()

	if err != nil {
		return nil, err
	}

	defer fh.Close()

	dataBytes := bytes.Buffer{}

	_, err = dataBytes.ReadFrom(fh)

	if err != nil {
		return nil, err
	}

	dataHash := fmt.Sprintf("%x", md5.Sum(dataBytes.Bytes()))
	ext := filepath.Ext(f.Data.Filename)
	filename := path.Join(dataHash[:2], dataHash[2:4], dataHash[4:]+ext)

	err = storage.Save(filename, gostorages.NewContentFile(dataBytes.Bytes()))

	if err != nil {
		return nil, err
	}

	return &image.ImageFile{
		Filepath: filename,
		Storage:  storage,
	}, nil
}
示例#3
0
文件: http.go 项目: StreetEasy/picfit
func (s *HTTPStorage) Open(filepath string) (gostorages.File, error) {
	u, err := url.Parse(s.URL(filepath))

	if err != nil {
		return nil, err
	}

	content, err := s.OpenFromURL(u)

	if err != nil {
		return nil, err
	}

	return gostorages.NewContentFile(content), nil
}
示例#4
0
文件: forms.go 项目: train860/picfit
func (f *MultipartForm) Upload(storage gostorages.Storage) (*image.ImageFile, error) {
	var fh io.ReadCloser

	fh, err := f.Data.Open()

	if err != nil {
		return nil, err
	}

	defer fh.Close()

	dataBytes := bytes.Buffer{}

	_, err = dataBytes.ReadFrom(fh)

	if err != nil {
		return nil, err
	}
	//file storage path=>16进制(yMd)/16进制(hhm)
	//if not exists,create one
	now := time.Now()
	dir1, _ := strconv.ParseInt(now.Format("0612"), 10, 64)
	dir2, _ := strconv.ParseInt(now.Format("1504"), 10, 64)
	ext := strconv.FormatInt(dir1, 16) + "/" + strconv.FormatInt(dir2, 16) + "/"

	h := md5.New()
	h.Write(dataBytes.Bytes())
	filename := hex.EncodeToString(h.Sum(nil)) + path.Ext(f.Data.Filename)

	err = storage.Save(ext+filename, gostorages.NewContentFile(dataBytes.Bytes()))

	if err != nil {
		return nil, err
	}

	return &image.ImageFile{
		Filepath: filename,
		Storage:  storage,
	}, nil
}
示例#5
0
文件: file.go 项目: train860/picfit
func (i *ImageFile) Save() error {
	return i.Storage.Save(i.Filepath, gostorages.NewContentFile(i.Content()))
}