Groups & archives
A file group is an immutable, ordered bundle of files behind one id: a uuid with a count suffix, like :uuid~3.
Working with group ids
import { formatGroupId, parseGroupId } from '@uploadcare/cdn-url/group'
const group = parseGroupId('c2499162-eb07-4b93-b31e-94a89a47e858~3')
// → { uuid: 'c2499162-…', count: 3 }
formatGroupId(group) // → 'c2499162-…~3'parseGroupId throws a TypeError on malformed ids in every bundle flavor, which makes it handy for validating stored values.
Addressing the group and its files
Group URLs need the same CDN base as file URLs — the first argument of groupUrl/nthUrl/archiveUrl, or the one bound into a fluent base(...). There is no string-level tab here: tinyParse reads a group element's nth/1/ as part of the chain, so replacing the chain drops the addressing.
import { groupUrl, nthUrl } from '@uploadcare/cdn-url/group'
import { preview } from '@uploadcare/cdn-url/ops'
groupUrl('https://1s4oyld5dc.ucarecd.net', group)
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/ (lists the files)
nthUrl('https://1s4oyld5dc.ucarecd.net', group, 0)
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/nth/0/ (first file, zero-based)
nthUrl('https://1s4oyld5dc.ucarecd.net', group, 1, [preview(400, 400)])
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/nth/1/-/preview/400x400/import { CdnUrl } from '@uploadcare/cdn-url/builder'
import { preview } from '@uploadcare/cdn-url/ops'
new CdnUrl({ cdnBase: 'https://1s4oyld5dc.ucarecd.net', group }).href
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/
new CdnUrl({
cdnBase: 'https://1s4oyld5dc.ucarecd.net',
group,
nth: 1,
operations: [preview(400, 400)]
}).href
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/nth/1/-/preview/400x400/import { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'
const myCdn = cdn.base(prefixedCdnBase('demopublickey'))
myCdn.group(group).href
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/
myCdn.group(group).nth(1).preview(400, 400).href
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/nth/1/-/preview/400x400/Two rules the library enforces for you:
- Group roots can't carry operations. Transformations go on
nthelements only, and the parsedgroupshape doesn't even have anoperationsfield. - The index is validated against the count, so
nthUrl(cdnBase, group, 3)on a~3group throws aRangeErrorin development.
Rendering a gallery
const thumbs = Array.from({ length: group.count }, (_, i) =>
nthUrl('https://1s4oyld5dc.ucarecd.net', group, i, [preview(300, 300)])
)Archives
Hand the whole group to the user as one download:
import { archiveUrl } from '@uploadcare/cdn-url/group'
archiveUrl('https://1s4oyld5dc.ucarecd.net', group, 'zip')
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/archive/zip/
archiveUrl('https://1s4oyld5dc.ucarecd.net', group, 'tar', 'photos.tar')
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/archive/tar/photos.tarimport { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'
const myCdn = cdn.base(prefixedCdnBase('demopublickey'))
myCdn.group(group).archive('zip')
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/archive/zip/
myCdn.group(group).archive('tar', 'photos.tar')
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/archive/tar/photos.tarThere is no builder tab here. CdnUrl addresses one URL and has no archive method, so archives come from the group entry or a fluent group chain.
An archive URL parses back like any other: parseCdnUrl returns kind: 'archive' with group, archive (the format) and filename, and cdn.parse gives a chain you can rebase or rename.
import { parseArchiveUrl } from '@uploadcare/cdn-url'
parseArchiveUrl(stored)
// → { kind: 'archive', group: { uuid, count: 3 }, archive: 'tar', filename: 'photos.tar', … }
cdn.parse(stored).filename('all-photos.tar').href
// → https://1s4oyld5dc.ucarecd.net/:uuid~3/archive/tar/all-photos.tarArchive fine print: you get originals only (transformations are discarded), the uncompressed limit is 2 GB, and the request 404s if any file in the group was removed.