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