Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make duplicating rundowns safer #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion routes/routes-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ']';
Expand Down
54 changes: 36 additions & 18 deletions utils/spx_server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = /\((?<existingCopyNum>[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) {
Expand Down