1const crypto = require('crypto');
2const Apify = require('apify');
3const moment = require('moment');
4
5const { utils: { log } } = Apify;
6
7const KEY_VALUE_STORE_NAME = 'DELETE-NAMED-STORAGES-SECURITY-STORE';
8
9exports.filterStoragesByPattern = function (storages, storageNamePattern) {
10 if (!storageNamePattern) return storages;
11 const pattern = new RegExp(storageNamePattern);
12 const storagesMatchingPattern = storages.filter((storage) => {
13 return pattern.test(storage.name);
14 });
15 log.debug(`Removed ${storages.length - storagesMatchingPattern.length} storages because their name did not match the provided pattern.`);
16 return storagesMatchingPattern;
17};
18
19exports.filterStoragesByDate = function (storages, date) {
20 if (!date) return storages;
21 const day = moment(date);
22 const storagesCreatedAtDate = storages.filter((storage) => {
23 return day.isSame(storage.createdAt, 'day');
24 });
25 log.debug(`Removed ${storages.length - storagesCreatedAtDate.length} storages because they were created at a different date.`);
26 return storagesCreatedAtDate;
27};
28
29exports.checkIfSafeToDelete = async function (storages) {
30 const kvs = await Apify.openKeyValueStore(KEY_VALUE_STORE_NAME);
31 const hash = hashStorages(storages);
32 const storagesToBeDeleted = await kvs.getValue(hash);
33 if (storagesToBeDeleted) {
34 log.info('Destruction credentials accepted. The selected storages will now be destroyed.');
35 return true;
36 }
37 await kvs.setValue(hash, storages);
38 log.warning('Are you sure you want to do this? Please confirm destruction by running this actor again with the same input.');
39 const link = kvs.getPublicUrl(hash);
40 log.info(`You can verify the storages to be deleted at: ${link}`);
41 return false;
42};
43
44function hashStorages(storages) {
45 const data = JSON.stringify(storages);
46 return crypto.createHash('md5')
47 .update(data)
48 .digest('hex');
49}