Exemplo n.º 1
0
func (r *Copy) Execute() {
	defer r.progressBar.Finish()
	defer r.response.Body.Close()
	r.progressBar.Start()
	media, err := r.client.UploadMedia("", r.AlbumID, media.NewUploadMediaInfo(r.Title), r.ContentType, r.ContentLength, r.reader)
	r.Media = media
	r.UploadError = err
}
Exemplo n.º 2
0
// Cache creates a cache into `albumName``
func (cacher *ImageCacher) Cache(url string) (*models.ImageCache, error) {
	imageCacheKind := cacher.kind
	get := imageCacheKind.Get().Key(url)
	_, v := get.MustOne(cacher.req)
	if v != nil {
		return v.(*models.ImageCache), nil
	}
	cacher.logger.Debugf("Selecting album ...")
	album, err := cacher.selectAlbum()
	if err != nil {
		return nil, err
	}
	cacher.logger.Infof("Start caching %q", url)
	resp, err := cacher.openImageStream(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	imageID := wcg.NewUUID()
	mediaInfo := media.NewUploadMediaInfo(string(imageID))
	mediaInfo.Summary = url

	var photo *media.Media
	err = retry.Do(func() error {
		var err error
		photo, err = cacher.client.UploadMedia(
			"",
			album.ID,
			mediaInfo,
			resp.Header.Get("content-type"),
			resp.ContentLength,
			resp.Body,
		)
		return err
	}, retry.Interval(5*time.Second), retry.MaxRetries(5))
	if err != nil {
		return nil, fmt.Errorf("Failed to upload a media into album:%s: %v", album.ID, err)
	}
	// select content URL
	if len(photo.Contents) == 0 {
		return nil, fmt.Errorf("No contents are available google photo.")
	}
	image := &models.ImageCache{
		URL:           url,
		GPhotoAlbumID: photo.AlbumID,
		GPhotoID:      photo.ID,
		Width:         photo.Width,
		Height:        photo.Height,
	}
	// select better content based on 'width'
	for _, c := range photo.Contents {
		if image.GPhotoURL == "" || c.Width > image.Width {
			image.GPhotoURL = c.URL
			image.Width = c.Width
			image.Height = c.Height
		}
	}
	_, v = imageCacheKind.Put().Key(url).MustUpdate(cacher.req, image)
	cacher.logger.Infof(
		"created - GPHOTO Path: %s/%s URL: %s (from %s)",
		image.GPhotoAlbumID,
		image.GPhotoID,
		image.GPhotoURL,
		image.URL,
	)
	return v.(*models.ImageCache), nil
}