refactor: delete file
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
579ff3aa4a
commit
c10e4c04b6
|
@ -1,63 +0,0 @@
|
|||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('deleteOldFiles', {
|
||||
description: 'Delete files that are 6 months old from dms',
|
||||
accessType: 'WRITE',
|
||||
returns: {
|
||||
type: 'object',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/deleteOldFiles`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.deleteOldFiles = async(ctx, options) => {
|
||||
const tx = await Self.beginTransaction({});
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction)
|
||||
myOptions.transaction = tx;
|
||||
|
||||
try {
|
||||
const models = Self.app.models;
|
||||
const sixMonthsAgo = new Date();
|
||||
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
|
||||
|
||||
const claimDmsType = await models.DmsType.findOne({where: {code: 'claim'}}, myOptions);
|
||||
const oldDms = await models.Dms.find({
|
||||
where: {
|
||||
and: [{
|
||||
created: {lt: sixMonthsAgo}}, {
|
||||
dmsTypeFk: claimDmsType.id
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
for (let dms of oldDms) {
|
||||
const ClaimContainer = models.ClaimContainer;
|
||||
const pathHash = await ClaimContainer.getHash(dms.id);
|
||||
const claimContainer = await ClaimContainer.container(pathHash);
|
||||
const dstFile = path.join(claimContainer.client.root, pathHash, dms.file);
|
||||
await fs.unlink(dstFile);
|
||||
await models.ClaimDms.removeFile(ctx, dms.id);
|
||||
}
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return oldDms;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
|
||||
if (fs.existsSync(srcFile))
|
||||
await fs.unlink(srcFile);
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue