2023-04-17 12:33:07 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2023-04-17 16:19:25 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-04-17 12:33:07 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('scrub', {
|
2023-04-17 16:19:25 +00:00
|
|
|
description: 'Deletes images without database reference',
|
2023-04-17 12:33:07 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'collection',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The collection name',
|
|
|
|
required: true
|
2023-04-17 16:19:25 +00:00
|
|
|
}, {
|
|
|
|
arg: 'remove',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Delete instead of move images to trash'
|
2023-04-17 12:33:07 +00:00
|
|
|
}, {
|
|
|
|
arg: 'limit',
|
2023-04-17 16:19:25 +00:00
|
|
|
type: 'integer',
|
|
|
|
description: 'Maximum number of images to clean'
|
|
|
|
}, {
|
|
|
|
arg: 'dryRun',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Simulate actions'
|
2023-04-18 19:27:09 +00:00
|
|
|
}, {
|
|
|
|
arg: 'skipLock',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Wether to skip exclusive lock'
|
2023-04-17 12:33:07 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
2023-04-17 16:19:25 +00:00
|
|
|
type: 'integer',
|
2023-04-17 12:33:07 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/scrub`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-18 19:27:09 +00:00
|
|
|
Self.scrub = async function(collection, remove, limit, dryRun, skipLock) {
|
2023-04-17 16:19:25 +00:00
|
|
|
const $ = Self.app.models;
|
|
|
|
|
|
|
|
const env = process.env.NODE_ENV;
|
|
|
|
dryRun = dryRun || (env && env !== 'production');
|
|
|
|
|
|
|
|
const instance = await $.ImageCollection.findOne({
|
|
|
|
fields: ['id'],
|
|
|
|
where: {name: collection}
|
|
|
|
});
|
|
|
|
if (!instance)
|
|
|
|
throw new UserError('Collection does not exist');
|
|
|
|
|
|
|
|
const container = await $.ImageContainer.container(collection);
|
2023-04-17 12:33:07 +00:00
|
|
|
const rootPath = container.client.root;
|
|
|
|
|
2023-04-18 19:27:09 +00:00
|
|
|
let tx;
|
|
|
|
let opts;
|
2023-04-17 16:19:25 +00:00
|
|
|
const lockName = 'salix.Image.scrub';
|
2023-04-18 19:27:09 +00:00
|
|
|
|
|
|
|
if (!skipLock) {
|
|
|
|
tx = await Self.beginTransaction({timeout: null});
|
|
|
|
opts = {transaction: tx};
|
|
|
|
|
|
|
|
const [row] = await Self.rawSql(
|
|
|
|
`SELECT GET_LOCK(?, 10) hasLock`, [lockName], opts);
|
|
|
|
if (!row.hasLock)
|
|
|
|
throw new UserError('Cannot obtain exclusive lock');
|
|
|
|
}
|
2023-04-17 12:33:07 +00:00
|
|
|
|
2023-04-17 16:19:25 +00:00
|
|
|
try {
|
|
|
|
const now = Date.vnNew().toJSON();
|
|
|
|
const scrubDir = path.join(rootPath, '.scrub', now);
|
|
|
|
|
|
|
|
const collectionDir = path.join(rootPath, collection);
|
|
|
|
const sizes = await fs.readdir(collectionDir);
|
|
|
|
let cleanCount = 0;
|
|
|
|
|
|
|
|
mainLoop: for (const size of sizes) {
|
|
|
|
const sizeDir = path.join(collectionDir, size);
|
|
|
|
const scrubSizeDir = path.join(scrubDir, collection, size);
|
|
|
|
const images = await fs.readdir(sizeDir);
|
|
|
|
for (const image of images) {
|
|
|
|
const imageName = path.parse(image).name;
|
|
|
|
const count = await Self.count({
|
|
|
|
collectionFk: collection,
|
|
|
|
name: imageName
|
2023-04-18 15:23:38 +00:00
|
|
|
}, opts);
|
2023-04-17 16:19:25 +00:00
|
|
|
const exists = count > 0;
|
|
|
|
let scrubDirCreated = false;
|
|
|
|
if (!exists) {
|
|
|
|
const srcFile = path.join(sizeDir, image);
|
|
|
|
if (remove !== true) {
|
|
|
|
if (!scrubDirCreated) {
|
|
|
|
if (!dryRun)
|
|
|
|
await fs.mkdir(scrubSizeDir, {recursive: true});
|
|
|
|
scrubDirCreated = true;
|
|
|
|
}
|
|
|
|
const dstFile = path.join(scrubSizeDir, image);
|
2023-04-18 15:25:37 +00:00
|
|
|
if (!dryRun) await fs.rename(srcFile, dstFile);
|
2023-04-17 16:19:25 +00:00
|
|
|
} else {
|
2023-04-18 15:25:37 +00:00
|
|
|
try {
|
|
|
|
if (!dryRun) await fs.unlink(srcFile);
|
|
|
|
} catch (err) {
|
2023-04-18 19:27:09 +00:00
|
|
|
console.error(err.message);
|
2023-04-18 15:23:38 +00:00
|
|
|
}
|
2023-04-17 16:19:25 +00:00
|
|
|
}
|
2023-04-17 12:33:07 +00:00
|
|
|
|
2023-04-17 16:19:25 +00:00
|
|
|
cleanCount++;
|
|
|
|
if (limit && cleanCount == limit)
|
|
|
|
break mainLoop;
|
|
|
|
}
|
2023-04-17 12:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-17 16:19:25 +00:00
|
|
|
|
|
|
|
return cleanCount;
|
|
|
|
} finally {
|
2023-04-18 19:27:09 +00:00
|
|
|
if (!skipLock) {
|
|
|
|
try {
|
|
|
|
await Self.rawSql(`DO RELEASE_LOCK(?)`, [lockName], opts);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
2023-04-18 18:53:49 +00:00
|
|
|
}
|
2023-04-17 16:19:25 +00:00
|
|
|
}
|
2023-04-17 12:33:07 +00:00
|
|
|
};
|
|
|
|
};
|