diff --git a/routes/routes-application.js b/routes/routes-application.js index da03a7ac..677ca452 100644 --- a/routes/routes-application.js +++ b/routes/routes-application.js @@ -1591,7 +1591,14 @@ 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 {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(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) { let errmsg = 'Server error in /gc/duplicateRundown [' + error + ']'; diff --git a/utils/spx_server_functions.js b/utils/spx_server_functions.js index ba05511d..9c613523 100644 --- a/utils/spx_server_functions.js +++ b/utils/spx_server_functions.js @@ -118,25 +118,43 @@ 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); + 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)) { + return createCopyPath( + existingCopyNum + ? name.replace(/^(.*) \(([0-9]+)\)$/, `$1 (${Number(existingCopyNum) + 1})`) + : `${name} (2)`) + } else { + copyNum = existingCopyNum + 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({path: copyPath, copyNum}); + }); + } catch (error) { + logger.error('spx.duplicateFile - Error while duplicating: ' + fileRefe + ': ' + error); + reject(); + } + }) }, // duplicateFile fileNameFromPath: function (filepath) {