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