luci: Fix save very long textareas to file

This commit is contained in:
remittor
2026-01-16 11:34:09 +03:00
parent c575592f9e
commit 5a7a88ccbd

View File

@@ -370,40 +370,56 @@ return baseclass.extend({
]); ]);
}, },
handleSaveAdv: async function(ev) { writeAdv: async function(fileName, data, chunkSize = 8000)
let txt = document.getElementById('widget.modal_content'); {
let value = txt.value.trim().replace(/\r\n/g, '\n'); let tmpFile = fileName + '.tmp';
if (value.length > 0) {
value += '\n';
}
let data = value.replace(/'/g, `'\"'\"'`);
let chunkSize = 8000;
try { try {
for (let wsize = -1; wsize < data.length; wsize += chunkSize) { for (let wsize = 0; wsize <= data.length; wsize += chunkSize) {
let chunk = ''; let chunk = data.slice(wsize, wsize + chunkSize);
if (wsize < 0) { if (wsize > 0 && chunk.length == 0) {
wsize = 0; break; // EOF
} }
if (data.length > 0) { chunk = chunk.replace(/'/g, `'\"'\"'`);
chunk = data.slice(wsize, wsize + chunkSize); let teeArg = (wsize === 0) ? '' : '-a';
} let cmd = `printf %s '${chunk}' | tee ${teeArg} '${tmpFile}'`;
let teeArg = (wsize === 0) ? '' : '-a ';
let cmd = `printf %s '${chunk}' | tee ${teeArg} '${this.file}'`;
let res = await fs.exec('/bin/busybox', [ 'sh', '-c', cmd ]); let res = await fs.exec('/bin/busybox', [ 'sh', '-c', cmd ]);
if (res.code !== 0) { if (res.code !== 0) {
throw new Error('tee failed, rc = ' + res.code); throw new Error('tee failed, rc = ' + res.code);
} }
} }
let res = await fs.exec('/bin/busybox', [ 'mv', '-f', tmpFile, fileName ]);
if (res.code != 0) {
throw new Error('mv failed, rc = ' + res.code);
}
} catch(e) {
try {
await fs.exec('/bin/busybox', [ 'rm', '-f', tmpFile ]);
} catch(e2) {
// nothing
}
throw e;
}
return fs.stat(fileName);
},
handleSaveAdv: async function(ev)
{
let txt = document.getElementById('widget.modal_content');
let value = txt.value.trim().replace(/\r\n/g, '\n');
if (value.length > 0) {
value += '\n';
}
return this.writeAdv(this.file, value).then(async rc => {
txt.value = value; txt.value = value;
ui.addNotification(null, E('p', _('Contents have been saved.')), 'info'); ui.addNotification(null, E('p', _('Contents have been saved.')), 'info');
if (this.callback) { if (this.callback) {
return this.callback(rc); return this.callback(rc);
} }
} catch(e) { }).catch(e => {
ui.addNotification(null, E('p', _('Unable to save the contents') + ': %s'.format(e.message))); ui.addNotification(null, E('p', _('Unable to save the contents') + ': %s'.format(e.message)));
} finally { }).finally(() => {
ui.hideModal(); ui.hideModal();
} });
}, },
error: function(e) { error: function(e) {