1 | import * as fs from 'fs-extra';
|
2 | import * as request from 'request';
|
3 | import * as retry from 'retry';
|
4 |
|
5 | import { MagnoliaSourceOptions } from './magnolia-source-options.interface';
|
6 |
|
7 | export function fetchDamAssets(uuids: string[], options?: MagnoliaSourceOptions): Promise<any> {
|
8 | return new Promise(resolve => {
|
9 | const operation = retry.operation();
|
10 | const damUrl = options.magnolia.url + options.magnolia.damJsonEndpoint;
|
11 |
|
12 | request.get(
|
13 | damUrl,
|
14 | {
|
15 | json: true,
|
16 | headers: {
|
17 | Authorization: options.magnolia.auth.header,
|
18 | 'User-Agent': 'Paperboy'
|
19 | }
|
20 | },
|
21 | async (err, res, body) => {
|
22 | if (operation.retry(err)) {
|
23 | console.error('Attempt to get asset information failed, will retry in some time...');
|
24 | return;
|
25 | }
|
26 |
|
27 | if (body && body.results && body.results.length > 0) {
|
28 | const sanitizedAssetJson = uuids
|
29 | .map(uuid => body.results.find((asset: any) => asset['jcr:uuid'] === uuid))
|
30 | .map(json => json ? sanitizeDamJson(json) : null);
|
31 |
|
32 | const assetsNeedingUpdate = sanitizedAssetJson.filter(asset => {
|
33 | if (asset) {
|
34 | const filePath = options.output.assets + asset.path;
|
35 | const fileExists = fs.existsSync(filePath);
|
36 |
|
37 | if (fileExists) {
|
38 | return (
|
39 | new Date(fs.statSync(filePath).mtime).getTime() <
|
40 | new Date(asset.lastModified).getTime()
|
41 | );
|
42 | }
|
43 | }
|
44 |
|
45 | return true;
|
46 | });
|
47 |
|
48 | await Promise.all(assetsNeedingUpdate.map(asset => downloadAsset(options, asset)));
|
49 |
|
50 | resolve(sanitizedAssetJson);
|
51 | }
|
52 | }
|
53 | );
|
54 | });
|
55 | }
|
56 |
|
57 | function sanitizeDamJson(damJson: any): any {
|
58 | const sanitized: any = {};
|
59 |
|
60 | Object.keys(damJson).forEach(async key => {
|
61 | const sanitizedKey = key
|
62 | .replace(/^@path/, 'path')
|
63 | .replace(/^@/, '')
|
64 | .replace(/^mgnl:/, '')
|
65 | .replace(/^jcr:uuid/, 'id')
|
66 | .replace(/^jcr:mimeType/, 'mimeType');
|
67 |
|
68 | if (!sanitizedKey.match(/^jcr:/)) {
|
69 | sanitized[sanitizedKey] = damJson[key];
|
70 | }
|
71 | });
|
72 |
|
73 | return sanitized;
|
74 | }
|
75 |
|
76 | function downloadAsset(options: MagnoliaSourceOptions, asset: any): Promise<void> {
|
77 | return new Promise(resolve => {
|
78 | if (asset) {
|
79 | const filePath = options.output.assets + asset.path;
|
80 | const directory = filePath
|
81 | .split('/')
|
82 | .slice(0, -1)
|
83 | .join('/');
|
84 |
|
85 | fs.mkdirpSync(directory);
|
86 |
|
87 | request
|
88 | .get(options.magnolia.url + '/dam/jcr:' + asset.id, {
|
89 | headers: {
|
90 | Authorization: options.magnolia.auth.header,
|
91 | 'User-Agent': 'Paperboy'
|
92 | }
|
93 | })
|
94 | .on('response', res => {
|
95 | res.pipe(fs.createWriteStream(filePath));
|
96 |
|
97 | res.on('end', () => {
|
98 | resolve();
|
99 | });
|
100 | });
|
101 | } else {
|
102 | resolve();
|
103 | }
|
104 | });
|
105 | }
|