feat(nodejs): opening and saving document from buffer

This commit is contained in:
sshakndr
2024-03-29 17:16:44 +07:00
committed by Sergey Linnik
parent 58331f3dd1
commit 0e72234625
2 changed files with 18 additions and 1 deletions

View File

@ -87,6 +87,7 @@ app.use(favicon(`${__dirname}/public/images/favicon.ico`)); // use favicon
app.use(bodyParser.json()); // connect middleware that parses json
app.use(bodyParser.urlencoded({ extended: false })); // connect middleware that parses urlencoded bodies
app.use(bodyParser.raw({ type: '*/*' }));
app.get('/', (req, res) => { // define a handler for default page
try {
@ -715,6 +716,11 @@ app.post('/track', async (req, res) => { // define a handler for tracking file c
let fName = fileUtility.getFileName(req.query.filename);
let version = 0;
if (req.headers['content-type'] === 'application/octet-stream') {
fileSystem.writeFileSync(req.DocManager.storagePath(fName, uAddress), req.body);
return;
}
// track file changes
const processTrack = async function processTrack(response, bodyTrack, fileNameTrack, userAddressTrack) {
// callback file saving process
@ -1204,11 +1210,12 @@ app.get('/editor', (req, res) => { // define a handler for editing document
// file config data
const argss = {
documentData: fileSystem.readFileSync(req.DocManager.storagePath(fileName, userAddress)).toString('base64'),
apiUrl: siteUrl + configServer.get('apiUrl'),
file: {
name: fileName,
ext: fileUtility.getFileExtension(fileName, true),
uri: url,
uri: '_data_',
directUrl: !userDirectUrl ? null : directUrl,
uriUser: directUrl,
created: new Date().toDateString(),

View File

@ -77,8 +77,17 @@
var onAppReady = function () { // the application is loaded into the browser
innerAlert("Document editor ready");
const b64ToBuffer = (b64) => Uint8Array.from(atob(b64), c => c.charCodeAt(0));
if (config.document.url === "_data_") docEditor.openDocument(b64ToBuffer("<%= documentData %>"));
};
var onSaveDocument = function (event) {
let xhr = new XMLHttpRequest();
xhr.open("POST", config.editorConfig.callbackUrl);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(event.data);
}
var onDocumentStateChange = function (event) { // the document is modified
var title = document.title.replace(/^\*/g, "");
document.title = (event.data ? "*" : "") + title;
@ -454,6 +463,7 @@
"onRequestSelectDocument": onRequestSelectDocument,
"onRequestSelectSpreadsheet": onRequestSelectSpreadsheet,
"onRequestOpen": onRequestOpen,
"onSaveDocument": onSaveDocument,
};
if (<%- JSON.stringify(editor.userid) %> != null) {