@@ -34,6 +34,7 @@ const fs = require("fs");
3434const Session = require ( '../server/api/models/session' ) ;
3535const Output = require ( '../server/api/models/output.js' )
3636const mongoose = require ( "mongoose" ) ;
37+ const { JsonStreamStringify } = require ( 'json-stream-stringify' ) . default || require ( 'json-stream-stringify' ) ;
3738
3839function readDirRecursive ( dir ) {
3940 let results = [ ] ;
@@ -115,7 +116,7 @@ function dropOutput(id) {
115116 logger . debug ( `Collection output${ id } dropped successfully.` )
116117}
117118
118- async function checkMaximumSpaceOverflow ( ) {
119+ async function checkMaximumSpaceOverflow ( ignoringOutputId ) {
119120
120121 let collections = await mongoose . connection . db . listCollections ( ) . toArray ( ) ;
121122 const db = mongoose . connection . db ;
@@ -248,25 +249,28 @@ async function checkMaximumSpaceOverflow() {
248249 folder = file . path
249250 else
250251 folder = getFolderFromSession ( file . path )
251- logger . debug ( `Deleting file ${ file . path } and folder ${ folder } for cleanup...` )
252- fs . rmSync ( folder , { recursive : true , force : true } ) ;
253- logger . info ( `File ${ file . path } and folder ${ folder } deleted.` ) ;
254- let orphanSession //"./output/" + folder.split("/").join("") + ".json"
255- if ( pathIsOutput ( file . path ) )
256- orphanSession = getSessionFromOutputPath ( file . path )
257- else if ( file . type === "folder" )
258- orphanSession = getSessionFromOutputFolderPath ( file . path )
259- else
260- orphanSession = file . path
261- logger . debug ( `Checking for orphan session with id ${ orphanSession } linked to deleted file...` )
262- if ( fs . existsSync ( orphanSession ) && orphanSession !== "./output/results.json" )
263- try {
264- fs . unlinkSync ( orphanSession ) ;
265- } catch ( error ) {
266- logger . error ( `Error deleting orphan session file ${ orphanSession } :` , error )
267- }
268- else
269- logger . debug ( `No orphan session file ${ orphanSession } found for deleted file.` )
252+ if ( ! ignoringOutputId || ( ignoringOutputId && ! folder . includes ( ignoringOutputId ) ) ) {
253+ logger . debug ( `Deleting file ${ file . path } and folder ${ folder } for cleanup...` )
254+ fs . rmSync ( folder , { recursive : true , force : true } ) ;
255+ logger . info ( `File ${ file . path } and folder ${ folder } deleted.` ) ;
256+
257+ let orphanSession //"./output/" + folder.split("/").join("") + ".json"
258+ if ( pathIsOutput ( file . path ) )
259+ orphanSession = getSessionFromOutputPath ( file . path )
260+ else if ( file . type === "folder" )
261+ orphanSession = getSessionFromOutputFolderPath ( file . path )
262+ else
263+ orphanSession = file . path
264+ logger . debug ( `Checking for orphan session with id ${ orphanSession } linked to deleted file...` )
265+ if ( fs . existsSync ( orphanSession ) && orphanSession !== "./output/results.json" )
266+ try {
267+ fs . unlinkSync ( orphanSession ) ;
268+ } catch ( error ) {
269+ logger . error ( `Error deleting orphan session file ${ orphanSession } :` , error )
270+ }
271+ else
272+ logger . debug ( `No orphan session file ${ orphanSession } found for deleted file.` )
273+ }
270274 //fs.unlinkSync(filePath);
271275 //logger.info(`File ${file.file} deleted.`);
272276 } catch ( err ) {
@@ -711,20 +715,13 @@ const sendOutput = async (config, res) => {
711715 if ( res . dmm . source . data && res . dmm . source . url )
712716 res . dmm . source . data = undefined
713717 if ( config . sessionLocation . filesystem ) {
714- const JsonStreamStringify = require ( 'json-stream-stringify' ) ;
715-
716- const writeStream = fs . createWriteStream ( './output/output' + outputId + '.json' ) ;
717- const jsonStream = new JsonStreamStringify ( res . dmm ) ;
718-
719- jsonStream . pipe ( writeStream ) ;
720-
721- writeStream . on ( 'finish' , ( ) => {
722- logger . debug ( 'File output is created successfully.' ) ;
723- outputDataTempWriting . value = 'File output is created successfully.' ;
724- } ) ;
725-
726- writeStream . on ( 'error' , ( err ) => {
727- throw err ;
718+ await new Promise ( ( resolve , reject ) => {
719+ const writeStream = fs . createWriteStream ( './output/output' + outputId + '.json' ) ;
720+ const jsonStream = new JsonStreamStringify ( res . dmm ) ;
721+ jsonStream . pipe ( writeStream ) ;
722+ writeStream . on ( 'finish' , resolve ) ;
723+ writeStream . on ( 'error' , reject ) ;
724+ jsonStream . on ( 'error' , reject ) ;
728725 } ) ;
729726 }
730727 if ( config . sessionLocation . mongo )
@@ -735,10 +732,10 @@ const sendOutput = async (config, res) => {
735732 //logger.error(res.dmm)
736733 outputDataTempWriting . value = 'Error during output file creation.'
737734 }
738- if ( config . sessionLocation . filesystem )
739- await finish ( outputDataTempWriting )
735+ // if (config.sessionLocation.filesystem)
736+ // await finish(outputDataTempWriting)
740737 if ( ! config . manualCheckMaximumSpaceOverflow )
741- await checkMaximumSpaceOverflow ( )
738+ await checkMaximumSpaceOverflow ( outputId )
742739 //const deleteSession =
743740 logger . debug ( res . dmm . outputFile [ res . dmm . outputFile . length - 1 ] )
744741 res . dmm . deleteSession ( )
0 commit comments