(231401) master to test #1466
|
@ -0,0 +1,70 @@
|
|||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('scrub', {
|
||||
description: 'Cleans collection images directory',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'collection',
|
||||
type: 'string',
|
||||
description: 'The collection name',
|
||||
required: true
|
||||
}, {
|
||||
arg: 'limit',
|
||||
type: 'number',
|
||||
description: 'Maximun number of image to clean'
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: 'number',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/scrub`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.scrub = async function(collection, limit) {
|
||||
const models = Self.app.models;
|
||||
const container = await models.ImageContainer.container(
|
||||
collection
|
||||
);
|
||||
const rootPath = container.client.root;
|
||||
|
||||
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 models.Image.count({
|
||||
collectionFk: collection,
|
||||
name: imageName
|
||||
});
|
||||
const exists = count > 0;
|
||||
if (!exists) {
|
||||
cleanCount++;
|
||||
const srcDir = path.join(sizeDir, image);
|
||||
const dstDir = path.join(scrubSizeDir, image);
|
||||
await fs.mkdir(scrubSizeDir, {recursive: true});
|
||||
await fs.rename(srcDir, dstDir);
|
||||
|
||||
if (limit && cleanCount == limit)
|
||||
break mainLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cleanCount;
|
||||
};
|
||||
};
|
|
@ -12,13 +12,13 @@ module.exports = Self => {
|
|||
type: 'Number',
|
||||
description: 'The entity id',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
}, {
|
||||
arg: 'collection',
|
||||
type: 'string',
|
||||
description: 'The collection name',
|
||||
required: true
|
||||
}],
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: 'Object',
|
||||
root: true
|
||||
|
|
|
@ -5,6 +5,7 @@ const gm = require('gm');
|
|||
module.exports = Self => {
|
||||
require('../methods/image/download')(Self);
|
||||
require('../methods/image/upload')(Self);
|
||||
require('../methods/image/scrub')(Self);
|
||||
|
||||
Self.resize = async function({collectionName, srcFile, fileName, entityId}) {
|
||||
const models = Self.app.models;
|
||||
|
|
Loading…
Reference in New Issue