const gql = require('graphql-tag'); const sleep = require('await-sleep'); const { environment, orgSlug, makeS3Locator, request, findMediaItem } = require('./common'); /** * Creates a new Asset in Connect with the given name. */ async function createAsset(name) { const mutation = ` mutation CreateAsset($input: AssetInput) { createAsset(input: $input) { id name } } `; const vars = { input: { name, }, }; const data = await request(mutation, vars); return data.createAsset; } /** * Attaches a media item to an asset */ async function attachMedia(fileId, assetId) { const mutation = ` mutation AssignFile($id: ID!, $input: FileInput) { updateFile(id: $id, input: $input) { id } } `; const vars = { id: fileId, input: { assetId, }, }; await request(mutation, vars); } /** * Outputs the list of files under the specified asset. */ async function listAssetFiles(assetId) { const query = ` query ListAssignedFiles($first: Int, $skip: Int, $search: String, $languages: [String], $designationId: ID, $assetId: ID, $filters: FileFilters, $orderBy: [FileOrderBy]) { files(first: $first, skip: $skip, search: $search, languages: $languages, designationId: $designationId, assetId: $assetId, filters: $filters, orderBy: $orderBy) { totalCount edges { node { id locatorUrl } } } } `; // we incrementally load all files using recursive paging let retrievedFiles = 0; let totalFiles = 1; // this is just temporary to get things going let pageSize = 10; // adjust this for bigger pages while (retrievedFiles < totalFiles) { const data = await request(query, { skip: retrievedFiles, first: pageSize, assetId }); const files = data.files; // update the paging state totalFiles = files.totalCount; retrievedFiles += files.edges.length; for (const edge of files.edges) { const file = edge.node; console.log(`${file.locatorUrl}`); } } } async function main() { // 1. create a new, empty, asset with a random name const asset = await createAsset(`my asset ${Date.now()}`); console.log(`Created asset "${asset.name}" with id "${asset.id}".`); // 2. find a media item (file) based on its storage url const file = await findMediaItem('s3://oz-zl-demo/demo_1523053276229/pokemon_test-assets/POK_TV_S1001_2997/POK_TV_S1001_2997-Master-EN-SD-4x3dvntsc_V2-BFF_S10E01_FR.mp4'); // 3. attach an existing media item await attachMedia(file.id, asset.id); // 4. list asset files await listAssetFiles(asset.id); } main().catch(err => { console.error(err.stack); process.exit(1); });