|
| 1 | +import { createC2pa, selectProducer } from 'c2pa'; |
| 2 | +import wasmSrc from 'c2pa/dist/assets/wasm/toolkit_bg.wasm?url'; |
| 3 | +import workerSrc from 'c2pa/dist/c2pa.worker.js?url'; |
| 4 | +import { parseISO } from 'date-fns'; |
| 5 | + |
| 6 | +const sampleImage = |
| 7 | + 'https://raw.githubusercontent.com/contentauth/c2pa-js/main/tools/testing/fixtures/images/CAICAI.jpg'; |
| 8 | + |
| 9 | +(async () => { |
| 10 | + let output: string[] = []; |
| 11 | + |
| 12 | + const c2pa = await createC2pa({ |
| 13 | + wasmSrc, |
| 14 | + workerSrc, |
| 15 | + }); |
| 16 | + |
| 17 | + const { manifestStore, source } = await c2pa.read(sampleImage); |
| 18 | + const activeManifest = manifestStore?.activeManifest; |
| 19 | + if (activeManifest) { |
| 20 | + // Get thumbnail |
| 21 | + // Note: You would normally call `dispose()` when working with a |
| 22 | + // component-based UI library (e.g. on component un-mount) |
| 23 | + // @ts-expect-error noUnusedLocals |
| 24 | + const { url, dispose } = source.thumbnail.getUrl(); |
| 25 | + |
| 26 | + // Get properties |
| 27 | + const properties: Record<string, string | undefined> = { |
| 28 | + title: activeManifest.title, |
| 29 | + format: activeManifest.format, |
| 30 | + claimGenerator: activeManifest.claimGenerator.split('(')[0]?.trim(), |
| 31 | + producer: selectProducer(activeManifest)?.name ?? 'Unknown', |
| 32 | + thumbnail: `<img src="${url}" class="thumbnail" />`, |
| 33 | + ingredients: (activeManifest.ingredients ?? []) |
| 34 | + .map((i) => i.title) |
| 35 | + .join(', '), |
| 36 | + signatureIssuer: activeManifest.signatureInfo?.issuer, |
| 37 | + signatureDate: activeManifest.signatureInfo?.time |
| 38 | + ? parseISO(activeManifest.signatureInfo.time).toString() |
| 39 | + : 'No date available', |
| 40 | + }; |
| 41 | + |
| 42 | + output = Object.keys(properties).map((key) => { |
| 43 | + return ` |
| 44 | + <tr> |
| 45 | + <td>${key}</td> |
| 46 | + <td>${properties[key]}</td> |
| 47 | + </tr> |
| 48 | + `; |
| 49 | + }); |
| 50 | + } else { |
| 51 | + output.push(` |
| 52 | + <tr> |
| 53 | + <td colspan="2">No provenance data found</td> |
| 54 | + </tr> |
| 55 | + `); |
| 56 | + } |
| 57 | + |
| 58 | + document.querySelector('#results tbody')!.innerHTML = output.join(''); |
| 59 | +})(); |
0 commit comments