From 9f3f378b2f0cf7b6a0016211944c62bc32bd4787 Mon Sep 17 00:00:00 2001 From: Daniel Shields Date: Thu, 25 Jan 2024 15:07:14 +0000 Subject: [PATCH 1/2] Make duplicating rundowns safer Makes itemIDs unique when duplicating by appending '_' to the end Uses the same file name system as windows for when copying rundown files to ensure overwrites never happen --- routes/routes-application.js | 8 +++++- utils/spx_server_functions.js | 52 +++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/routes/routes-application.js b/routes/routes-application.js index da03a7ac..7d622e47 100644 --- a/routes/routes-application.js +++ b/routes/routes-application.js @@ -1591,7 +1591,13 @@ router.post('/gc/duplicateRundown', spxAuth.CheckLogin, async (req, res) => { let filename = req.body.filename; let filerefe = path.normalize(path.join(config.general.dataroot,foldname, 'data', filename)); try { - spx.duplicateFile(filerefe, ' copy') ; + const newFilePath = await spx.duplicateFile(filerefe); + + // for each template in the duplicated rundown, append "_" to the itemID to avoid conflicts with the old rundown + const newRundown = spx.GetJsonData(newFilePath) + newRundown?.templates?.forEach((_, index) => newRundown.templates[index].itemID += '_') + await spx.writeFile(newFilePath,newRundown); + res.status(200).send('Item duplicated.'); // ok 200 AJAX RESPONSE } catch (error) { let errmsg = 'Server error in /gc/duplicateRundown [' + error + ']'; diff --git a/utils/spx_server_functions.js b/utils/spx_server_functions.js index ba05511d..f8bc91e3 100644 --- a/utils/spx_server_functions.js +++ b/utils/spx_server_functions.js @@ -118,25 +118,41 @@ module.exports = { } }, // checkServerConnections - duplicateFile: function (fileRefe, suffix) { - // TODO: Tämä on kesken! - try { - return new Promise(resolve => { - let fldrname = path.dirname(fileRefe); - let extename = path.extname(fileRefe); - let basename = path.basename(fileRefe, extename); - let copyfile = path.normalize(path.join(fldrname, basename + suffix + extename)); - fs.copyFile(fileRefe, copyfile, (err) => { - if (err) throw err; - logger.info('Rundown file ' + fileRefe + ' was copied to ' + copyfile + '.'); - resolve() - return true - }); - }) - } catch (error) { - logger.error('spx.duplicateFile - Error while duplicating: ' + fileRefe + ': ' + error); - return false + duplicateFile: function (fileRefe, suffix = ' - Copy') { + const fldrname = path.dirname(fileRefe); + const extename = path.extname(fileRefe); + const basename = path.basename(fileRefe, extename); + + const createCopyPath = (name) => { + const copyFilePath = path.normalize(path.join(fldrname, `${name}${extename}`)); + + if (fs.existsSync(copyFilePath)) { + const { groups } = /\((?[0-9]+)\)$/.exec(name) ?? {} + const { copyNum } = groups ?? {} + + return createCopyPath( + copyNum + ? name.replace(/^(.*) \(([0-9]+)\)$/, `$1 (${Number(copyNum) + 1})`) + : `${name} (1)`) + } else { + return copyFilePath + } } + + return new Promise((resolve, reject) => { + try { + const copyPath = createCopyPath(`${basename}${suffix}`) + + fs.copyFile(fileRefe, copyPath, (err) => { + if (err) throw err; + logger.info('Rundown file ' + fileRefe + ' was copied to ' + copyPath + '.'); + resolve(copyPath); + }); + } catch (error) { + logger.error('spx.duplicateFile - Error while duplicating: ' + fileRefe + ': ' + error); + reject(); + } + }) }, // duplicateFile fileNameFromPath: function (filepath) { From b4d2d458fc99d88a1f602b7dc13ed0af95d643c3 Mon Sep 17 00:00:00 2001 From: Daniel Shields Date: Thu, 25 Jan 2024 15:34:36 +0000 Subject: [PATCH 2/2] Make copy numbers start at 2 (a la windows) and generate new itemIDs based on copy number --- routes/routes-application.js | 9 +++++---- utils/spx_server_functions.js | 16 +++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/routes/routes-application.js b/routes/routes-application.js index 7d622e47..677ca452 100644 --- a/routes/routes-application.js +++ b/routes/routes-application.js @@ -1591,12 +1591,13 @@ router.post('/gc/duplicateRundown', spxAuth.CheckLogin, async (req, res) => { let filename = req.body.filename; let filerefe = path.normalize(path.join(config.general.dataroot,foldname, 'data', filename)); try { - const newFilePath = await spx.duplicateFile(filerefe); + const {path, copyNum} = await spx.duplicateFile(filerefe); // for each template in the duplicated rundown, append "_" to the itemID to avoid conflicts with the old rundown - const newRundown = spx.GetJsonData(newFilePath) - newRundown?.templates?.forEach((_, index) => newRundown.templates[index].itemID += '_') - await spx.writeFile(newFilePath,newRundown); + const newRundown = spx.GetJsonData(path) + const appendString = `_copy${copyNum || 1}` + newRundown?.templates?.forEach((_, index) => newRundown.templates[index].itemID += appendString) + await spx.writeFile(path,newRundown); res.status(200).send('Item duplicated.'); // ok 200 AJAX RESPONSE } catch (error) { diff --git a/utils/spx_server_functions.js b/utils/spx_server_functions.js index f8bc91e3..9c613523 100644 --- a/utils/spx_server_functions.js +++ b/utils/spx_server_functions.js @@ -122,19 +122,21 @@ module.exports = { const fldrname = path.dirname(fileRefe); const extename = path.extname(fileRefe); const basename = path.basename(fileRefe, extename); + let copyNum const createCopyPath = (name) => { const copyFilePath = path.normalize(path.join(fldrname, `${name}${extename}`)); + const { groups } = /\((?[0-9]+)\)$/.exec(name) ?? {}; + const { existingCopyNum } = groups ?? {}; + if (fs.existsSync(copyFilePath)) { - const { groups } = /\((?[0-9]+)\)$/.exec(name) ?? {} - const { copyNum } = groups ?? {} - return createCopyPath( - copyNum - ? name.replace(/^(.*) \(([0-9]+)\)$/, `$1 (${Number(copyNum) + 1})`) - : `${name} (1)`) + existingCopyNum + ? name.replace(/^(.*) \(([0-9]+)\)$/, `$1 (${Number(existingCopyNum) + 1})`) + : `${name} (2)`) } else { + copyNum = existingCopyNum return copyFilePath } } @@ -146,7 +148,7 @@ module.exports = { fs.copyFile(fileRefe, copyPath, (err) => { if (err) throw err; logger.info('Rundown file ' + fileRefe + ' was copied to ' + copyPath + '.'); - resolve(copyPath); + resolve({path: copyPath, copyNum}); }); } catch (error) { logger.error('spx.duplicateFile - Error while duplicating: ' + fileRefe + ': ' + error);