UNPKG

3.93 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const fs = require("fs-extra");
12const request = require("request");
13const retry = require("retry");
14function fetchDamAssets(uuids, options) {
15 return new Promise(resolve => {
16 const operation = retry.operation();
17 const damUrl = options.magnolia.url + options.magnolia.damJsonEndpoint;
18 request.get(damUrl, {
19 json: true,
20 headers: {
21 Authorization: options.magnolia.auth.header,
22 'User-Agent': 'Paperboy'
23 }
24 }, (err, res, body) => __awaiter(this, void 0, void 0, function* () {
25 if (operation.retry(err)) {
26 console.error('Attempt to get asset information failed, will retry in some time...');
27 return;
28 }
29 if (body && body.results && body.results.length > 0) {
30 const sanitizedAssetJson = uuids
31 .map(uuid => body.results.find((asset) => asset['jcr:uuid'] === uuid || asset['@id'] === uuid))
32 .map(json => json ? sanitizeDamJson(json) : null);
33 const assetsNeedingUpdate = sanitizedAssetJson.filter(asset => {
34 if (asset) {
35 const filePath = options.output.assets + asset.path;
36 const fileExists = fs.existsSync(filePath);
37 if (fileExists) {
38 return (new Date(fs.statSync(filePath).mtime).getTime() <
39 new Date(asset.lastModified).getTime());
40 }
41 }
42 return true;
43 });
44 yield Promise.all(assetsNeedingUpdate.map(asset => downloadAsset(options, asset)));
45 resolve(sanitizedAssetJson);
46 }
47 }));
48 });
49}
50exports.fetchDamAssets = fetchDamAssets;
51function sanitizeDamJson(damJson) {
52 const sanitized = {};
53 Object.keys(damJson).forEach((key) => __awaiter(this, void 0, void 0, function* () {
54 const sanitizedKey = key
55 .replace(/^@path/, 'path')
56 .replace(/^@/, '')
57 .replace(/^mgnl:/, '')
58 .replace(/^jcr:uuid/, 'id')
59 .replace(/^jcr:mimeType/, 'mimeType');
60 if (!sanitizedKey.match(/^jcr:/)) {
61 sanitized[sanitizedKey] = damJson[key];
62 }
63 }));
64 return sanitized;
65}
66function downloadAsset(options, asset) {
67 return new Promise(resolve => {
68 if (asset) {
69 const filePath = options.output.assets + asset.path;
70 const directory = filePath
71 .split('/')
72 .slice(0, -1)
73 .join('/');
74 fs.mkdirpSync(directory);
75 request
76 .get(options.magnolia.url + '/dam/jcr:' + asset.id, {
77 headers: {
78 Authorization: options.magnolia.auth.header,
79 'User-Agent': 'Paperboy'
80 }
81 })
82 .on('response', res => {
83 res.pipe(fs.createWriteStream(filePath));
84 res.on('end', () => {
85 resolve();
86 });
87 });
88 }
89 else {
90 resolve();
91 }
92 });
93}
94//# sourceMappingURL=dam.util.js.map
\No newline at end of file