Working With Albums
This guide describes methods for working with albums.
#
Album ActionsThis chapter describes methods for creating and modifying albums, excluding working with their contents.
#
Initializationimport CLPhotos from 'web_photos';
let photosApp;
const options = { logLevel: 'error' // 'trace' | 'debug' | 'info' | 'warn' | 'error'};
const config = { authToken: '', // user auth token baseUrl: '', // api base url required userAgent: '' // user agent name required}
try { photosApp = CLPhotos.init(options, config);} catch (e) { // catch photo sdk InternalError}
#
Get album listThere are several methods to get a list of albums, all of them return promises.
photosApp.albums.getVisibleAlbums()photosApp.albums.getSharedAlbums()photosApp.albums.getAlbums()
To get albums with the type 'simple' and 'shared', use the following code example.
photosApp.albums.getVisibleAlbums().then(({data}) => { const {_embedded: {albums}} = data; // IAlbumsSchema})
To get albums with the type 'shared' and 'hidden_shared', use the following code example.
photosApp.albums.getSharedAlbums().then(({data}) => { const {_embedded: {albums}} = data; // IAlbumsSchema})
To get all albums, use the following code example.
const albumId = 'id'; // insert id of certain albumphotosApp.albums.getAlbum(albumId).then(({data}) => { const albumItem = data; // IAlbumSchema})
To get a list of albums by page with the certain page size, use the following code example.
const pageSize = 20;const prams = { type: ['simple', 'shared']}const paginator = photosApp.albums.getAlbumsPaginator(pageSize, params);const albums = [];// get first pagepaginator.next() .then(({data}) => { if (data !== null) { items.push(data); // IItemSchema[] // get second page return paginator.next(); } })
#
Get albumThe following code example demonstrates getting an album.
const albumId = 'id'; // insert id of certain albumphotosApp.albums.getAlbum(albumId).then(({data}) => { const albumItem = data; // IAlbumSchema})
#
Create albumThe following code example demonstrates creating an album.
// Album can be one of the following types: // * 'simple' - Simple album. // * 'simple' - Album that has a link for viewing and / or editing // (depending on the isSharedForEdit parameter). // * 'hidden_shared' - Same as SHARED, but does not appear in the album list. // Used to share one or more photos to another user, // without creating a special album.const newAlbum = { type: 'simple', description: 'New album'}; // ICreateAlbumSchemaphotosApp.albums.createAlbum(newAlbum).then(({data}) => { const newAlbum = data; // IAlbumSchema});
The following code example demonstrates creating an album with photos.
// Album can be one of the following types: // * 'simple' - Simple album. // * 'simple' - Album that has a link for viewing and / or editing // (depending on the isSharedForEdit parameter). // * 'hidden_shared' - Same as SHARED, but does not appear in the album list. // Used to share one or more photos to another user, // without creating a special album.const newAlbum = { type: 'simple', description: 'New album'}; // ICreateAlbumSchemaconst items = [item1, item2]; // IItemSchema[]photosApp.albums.createAlbumWithItems(newAlbum, items).then(({data}) => { const newAlbum = data; // IAlbumSchema});
#
Edit albumThe following code example demonstrates editing an album.
const albumId = 'id'; // insert id of certain albumconst album = { description: 'renamed album'};photosApp.albums.updateAlbum(albumId, album).then(({data}) => { const newAlbum = data; // IAlbumSchema});
The following code example demonstrates renaming an album.
const albumId = 'id'; // insert id of certain albumphotosApp.albums.renameAlbum(albumId, 'renamed album').then(({data}) => { const newAlbum = data; // IAlbumSchema});
#
Delete albumsThe following code example demonstrates deleting an album.
const albumId = 'id'; // insert id of certain albumphotosApp.albums.removeAlbum(albumId).then(() => { // album is deleted});
The following code example demonstrates deleting several albums.
const albumIds = ['id1', 'id2']; // insert list of album idphotosApp.albums.removeAlbums(albumIds).then(() => { // albums are deleted});
#
Get album contentThe Cloudike Photos SDK provides data about photos and videos in an album to the client application as a paged list of PhotoItem objects. Working with this list is similar to the Timeline, with the difference that the list of album photos does not contain month separators. The following code example demonstrates getting an album content.
const albumId = 'id'; // insert id of certain albumconst prams = { limit: 20, offset: 0}photosApp.albums.getAlbumItems(albumId, params).then(({data}) => { const {_embedded: {items}} = data; // IItemSchema[]});
The following code example demonstrates adding photos to album.
const albumId = 'id'; // insert id of certain albumconst items = [item1, item2]; // IItemSchema[]photosApp.albums.addItemsToAlbum(albumId, items).then(() => { // photos added successfully});
The following code example demonstrates removing photos from album.
const albumId = 'id'; // insert id of certain albumconst items = [item1, item2]; // IItemSchema[]photosApp.albums.removeAlbumItems(albumId, items).then(() => { // photos removed succefuly});
The following code example demonstrates removing almost all items from an album except for excluded items
const albumId = 'id'; // insert id of certain albumconst excludeItems = [item1, item2]; // IItemSchema[]photosApp.albums.batchRemoveAll(albumId, excludeItems) .then(() => { const operations = data; // IOperationResult[] });
The following code example demonstrates getting photo item content from album.
const albumId = 'id'; // insert id of certain albumconst itemId = 'id' // insert id of certain photophotosApp.albums.getAlbumItem(albumId, itemId).then(({data}) => { const photo = data; // IItemSchema});
The following code example demonstrates getting photo item preview 'url' from album.
const albumId = 'id'; // insert id of certain albumconst itemId = 'id' // insert id of certain photophotosApp.albums.getAlbumItemPreviewLink(albumId, itemId).then(({data}) => { const {href: previewUrl} = data;});
#
Download album and photos from albumThe following code example demonstrates downloading album.
const albumId = 'id';const params = {}; // ICreateItemsDownloadSchemaphotosApp.albums.createAlbumZipStream(albumId, params) .then((downloadLink) => { // download items as .zip archive window.location.assign(downloadLink) })
The following code example demonstrates downloading photos from album.
const items = [item1, item2]; // IItemSchema[]photosApp.albums .createAlbumItemsZipStream(items) .then((downloadLink) => { // download items as .zip archive window.location.assign(downloadLink); });
The following code example demonstrates downloading one photo from album.
const itemId = 'id';photosApp.albums.getItemContentLink(itemId) .then(({data: contentLink}) => { return photosApp.albums.getItemContentByLink(contentLink) .then(({data}) => { const {_links: {data: {href: downloadLink}}} = data; // IContentSchema window.location.assign(downloadLink); }) })
#
Upload phots to albumThe following code example demonstrates uploading photos to album.
const albumId = 'id'; // insert id of certain albumconst files = [] // object from <input> tag FileListconst multipartUpload = true; // can be 'true' or 'false' default 'false'photosApp.albums.uploadItemsToAlbum(albumId, files, multipartUpload);