func newAlbum(c *cli.Context, title string) *media.Album { a := media.NewAlbum() a.Title = title if c.IsSet("public") { a.Access = media.AccessPublic } return a }
func (cacher *ImageCacher) selectAlbum() (*media.Album, error) { var albums []media.Album const mkeyAlbumList = "ImageCacher.AlbumList" const mexpiration = 24 * time.Hour err := cacher.memc.CachedObjectWithExpiration(mkeyAlbumList, mexpiration, albums, func() (interface{}, error) { cacher.logger.Debugf("Listing all albums...") list, err := cacher.client.ListAlbums("") if err != nil { return nil, err } for _, a := range list { albums = append(albums, *a) } return albums, nil }, false) if err != nil { return nil, err } cacher.logger.Debugf("%d albums found on the account.", len(albums)) albumName := cacher.getAlbumName() for _, a := range albums { if a.Title == albumName && a.NumPhotos <= maxPhotosInAlbum && a.Access == media.AccessPublic { cacher.logger.Debugf("Choose %s for the photo album.", a.ID) return &a, nil } } // no album available, create a new one. album := media.NewAlbum() album.Title = albumName album.Summary = "Auto generated by ImageCacher (DO NOT DELETE)" album.Access = media.AccessPublic created, err := cacher.client.CreateAlbum("", album) if err != nil { return nil, fmt.Errorf("Failed to create an album %s: %v", albumName, err) } cacher.logger.Debugf("new album %s is created (albumID: %s).", albumName, created.ID) // invalidate cache cacher.memc.Delete(mkeyAlbumList) return created, nil }