0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

🐛 Fixed removal of temp files left behind by importer

refs #10174

- Improved importer cleanUp method usage, so the cleanup is called in cases when there is an error during an import stage
- Simplified files to clean up tracking as removal of files is now partially handled in uploader middleware
This commit is contained in:
notanengineercom 2019-01-30 01:28:16 +00:00 committed by Naz Gargol
parent 631716053a
commit 5bd509c873

View file

@ -31,8 +31,8 @@ defaults = {
function ImportManager() {
this.importers = [ImageImporter, DataImporter];
this.handlers = [ImageHandler, JSONHandler, MarkdownHandler];
// Keep track of files to cleanup at the end
this.filesToDelete = [];
// Keep track of file to cleanup at the end
this.fileToDelete = null;
}
/**
@ -102,22 +102,23 @@ _.extend(ImportManager.prototype, {
* @returns {Function}
*/
cleanUp: function () {
var filesToDelete = this.filesToDelete;
return function (result) {
_.each(filesToDelete, function (fileToDelete) {
fs.remove(fileToDelete, function (err) {
if (err) {
common.logging.error(new common.errors.GhostError({
err: err,
context: common.i18n.t('errors.data.importer.index.couldNotCleanUpFile.error'),
help: common.i18n.t('errors.data.importer.index.couldNotCleanUpFile.context')
}));
}
});
});
var self = this;
return result;
};
if (self.fileToDelete === null) {
return;
}
fs.remove(self.fileToDelete, function (err) {
if (err) {
common.logging.error(new common.errors.GhostError({
err: err,
context: common.i18n.t('errors.data.importer.index.couldNotCleanUpFile.error'),
help: common.i18n.t('errors.data.importer.index.couldNotCleanUpFile.context')
}));
}
self.fileToDelete = null;
});
},
/**
* Return true if the given file is a Zip
@ -168,8 +169,9 @@ _.extend(ImportManager.prototype, {
* @returns {Promise[]} Files
*/
extractZip: function (filePath) {
var tmpDir = path.join(os.tmpdir(), uuid.v4());
this.filesToDelete.push(tmpDir);
const tmpDir = path.join(os.tmpdir(), uuid.v4());
this.fileToDelete = tmpDir;
return Promise.promisify(extract)(filePath, {dir: tmpDir}).then(function () {
return tmpDir;
});
@ -293,9 +295,6 @@ _.extend(ImportManager.prototype, {
loadFile: function (file) {
var self = this,
ext = path.extname(file.name).toLowerCase();
this.filesToDelete.push(file.path);
return this.isZip(ext) ? self.processZip(file) : self.processFile(file, ext);
},
/**
@ -367,10 +366,8 @@ _.extend(ImportManager.prototype, {
return self.doImport(importData, importOptions);
}).then(function (importData) {
// Step 4: Report on the import
return self.generateReport(importData)
// Step 5: Cleanup any files
.finally(self.cleanUp());
});
return self.generateReport(importData);
}).finally(() => self.cleanUp()); // Step 5: Cleanup any files
}
});