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-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-17 16:19:25 +00:00
|
|
|
Self.scrub = async function(collection, remove, limit, dryRun) {
|
|
|
|
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-17 16:19:25 +00:00
|
|
|
const conn = await getConnection();
|
|
|
|
const lockName = 'salix.Image.scrub';
|
|
|
|
let lockObtained;
|
2023-04-17 12:33:07 +00:00
|
|
|
|
2023-04-17 16:19:25 +00:00
|
|
|
try {
|
|
|
|
const [row] = await query(conn,
|
|
|
|
`SELECT GET_LOCK(?, 0) hasLock`,
|
|
|
|
[lockName]
|
|
|
|
);
|
|
|
|
lockObtained = !!row.hasLock;
|
|
|
|
if (!lockObtained)
|
|
|
|
throw new UserError('Cannot obtain exclusive lock');
|
2023-04-17 12:33:07 +00:00
|
|
|
|
2023-04-17 16:19:25 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
if (!dryRun)
|
|
|
|
await fs.rename(srcFile, dstFile);
|
|
|
|
} else {
|
|
|
|
if (!dryRun)
|
|
|
|
await fs.unlink(srcFile);
|
|
|
|
}
|
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 {
|
|
|
|
if (lockObtained)
|
|
|
|
await query(conn, `DO RELEASE_LOCK(?)`, [lockName]);
|
|
|
|
conn.release();
|
2023-04-17 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 16:19:25 +00:00
|
|
|
// Promisified datasource functions
|
|
|
|
|
|
|
|
function getConnection() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Self.dataSource.connector.client.getConnection((err, conn) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(conn);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function query(conn, sql, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
conn.query(sql, params, (err, res) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-04-17 12:33:07 +00:00
|
|
|
};
|
|
|
|
};
|