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 }
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 }
func FromStorage(storage gostorages.Storage, filepath string) (*ImageFile, error) { var file *ImageFile var err error f, err := storage.Open(filepath) if err != nil { return nil, err } defer f.Close() if err != nil { return nil, err } modifiedTime, err := storage.ModifiedTime(filepath) if err != nil { return nil, err } file = &ImageFile{ Filepath: filepath, Storage: storage, } contentType := file.ContentType() headers := map[string]string{ "Last-Modified": modifiedTime.Format(gostorages.LastModifiedFormat), "Content-Type": contentType, } buf, err := ioutil.ReadAll(f) if err != nil { return nil, err } file.Source = buf file.Headers = headers return file, err }
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 }