mirror of
https://github.com/cryptpad/cryptpad.git
synced 2026-09-14 11:05:41 +05:00
fix(unarchiveChannel): remove regexp + manage multiline metadata
- More robust implementation than regexps - Only parse the first line (document can only be created upon creation) - https://docs.cryptpad.org/en/user_guide/apps/general.html
This commit is contained in:
parent
1dcdf1b349
commit
4eeaa08ab5
@ -873,23 +873,26 @@ var unarchiveChannel = function (env, channelName, cb) {
|
||||
// remove the `expire` date if it is overdue (probably mistakenly
|
||||
// expired pad)
|
||||
Fs.readFile(metadataPath, (readError, content) => {
|
||||
content = content.toString();
|
||||
const expireExpr = /"expire":(\d+),/;
|
||||
if (readError) {
|
||||
return CB(readError.code);
|
||||
}
|
||||
let expireDate = expireExpr.exec(content);
|
||||
if (expireDate) {
|
||||
if (expireDate.length !== 2) {
|
||||
return CB('INVALID_METADATA');
|
||||
}
|
||||
expireDate = parseInt(expireDate[1]);
|
||||
try {
|
||||
let metadataArray = content.toString().split('\n');
|
||||
let metadata = JSON.parse(metadataArray[0]);
|
||||
let expireDate = metadata?.expire;
|
||||
// check that it’s indeed expired
|
||||
if (expireDate && +new Date() >= expireDate) {
|
||||
return Fs.writeFile(metadataPath, content.replace(expireExpr, ''), CB);
|
||||
delete metadata.expire;
|
||||
metadataArray[0] = JSON.stringify(metadata);
|
||||
return Fs.writeFile(metadataPath, metadataArray.join('\n'), CB);
|
||||
}
|
||||
CB();
|
||||
}
|
||||
CB();
|
||||
catch(err) {
|
||||
// Most probably a JSON.parse error: invalid metadata
|
||||
// The file would have been restored “as is” at this point
|
||||
CB('INVALID_METADATA');
|
||||
};
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user