Merge pull request '4091-dms_clean_fix2' (!1091) from 4091-dms_clean_fix2 into dev
gitea/salix/pipeline/head Build queued... Details

Reviewed-on: #1091
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2022-10-21 10:36:45 +00:00
commit ceeab73cf2
1 changed files with 33 additions and 44 deletions

View File

@ -17,61 +17,50 @@ module.exports = Self => {
});
Self.deleteTrashFiles = async options => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
if (process.env.NODE_ENV == 'test')
throw new UserError(`Action not allowed on the test environment`);
try {
if (process.env.NODE_ENV == 'test')
throw new UserError(`Action not allowed on the test environment`);
const models = Self.app.models;
const DmsContainer = models.DmsContainer;
const models = Self.app.models;
const DmsContainer = models.DmsContainer;
const trashDmsType = await models.DmsType.findOne({
where: {code: 'trash'}
}, myOptions);
const trashDmsType = await models.DmsType.findOne({
where: {code: 'trash'}
}, myOptions);
const date = new Date();
date.setMonth(date.getMonth() - 4);
const date = new Date();
date.setMonth(date.getMonth() - 4);
const dmsToDelete = await models.Dms.find({
where: {
and: [
{dmsTypeFk: trashDmsType.id},
{created: {lt: date}}
]
}
}, myOptions);
for (let dms of dmsToDelete) {
const pathHash = DmsContainer.getHash(dms.id);
const dmsContainer = await DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file);
try {
await fs.unlink(dstFile);
} catch (err) {
continue;
}
const dstFolder = path.join(dmsContainer.client.root, pathHash);
try {
await fs.rmdir(dstFolder);
} catch (err) {}
await dms.destroy(myOptions);
const dmsToDelete = await models.Dms.find({
where: {
and: [
{dmsTypeFk: trashDmsType.id},
{created: {lt: date}}
]
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
}, myOptions);
throw e;
for (let dms of dmsToDelete) {
const pathHash = DmsContainer.getHash(dms.id);
const dmsContainer = await DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file);
try {
await fs.unlink(dstFile);
} catch (err) {
continue;
}
const dstFolder = path.join(dmsContainer.client.root, pathHash);
try {
await fs.rmdir(dstFolder);
} catch (err) {
continue;
}
await dms.destroy(myOptions);
}
};
};