Добавлена синхронизация файлов обоев

This commit is contained in:
Book Pauk
2022-07-14 20:14:40 +07:00
parent 26468b996a
commit c7a17b0a76
8 changed files with 98 additions and 8 deletions

View File

@@ -25,6 +25,10 @@ class WebSocketController {
ws.on('message', (message) => {
this.onMessage(ws, message.toString());
});
ws.on('error', (err) => {
log(LM_ERR, err);
});
});
setTimeout(() => { this.periodicClean(); }, cleanPeriod);
@@ -70,6 +74,8 @@ class WebSocketController {
await this.readerRestoreCachedFile(req, ws); break;
case 'reader-storage':
await this.readerStorageDo(req, ws); break;
case 'upload-file-buf':
await this.uploadFileBuf(req, ws); break;
default:
throw new Error(`Action not found: ${req.action}`);
@@ -168,6 +174,15 @@ class WebSocketController {
this.send(await this.readerStorage.doAction(req.body), req, ws);
}
async uploadFileBuf(req, ws) {
if (!req.buf)
throw new Error(`key 'buf' is empty`);
this.send({url: await this.readerWorker.saveFileBuf(req.buf)}, req, ws);
}
}
module.exports = WebSocketController;

View File

@@ -219,6 +219,19 @@ class ReaderWorker {
return `disk://${hash}`;
}
async saveFileBuf(buf) {
const hash = await utils.getBufHash(buf, 'sha256', 'hex');
const outFilename = `${this.config.uploadDir}/${hash}`;
if (!await fs.pathExists(outFilename)) {
await fs.writeFile(outFilename, buf);
} else {
await utils.touchFile(outFilename);
}
return `disk://${hash}`;
}
async restoreRemoteFile(filename) {
const basename = path.basename(filename);
const targetName = `${this.config.tempPublicDir}/${basename}`;

View File

@@ -34,6 +34,12 @@ function getFileHash(filename, hashName, enc) {
});
}
function getBufHash(buf, hashName, enc) {
const hash = crypto.createHash(hashName);
hash.update(buf);
return hash.digest(enc);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@@ -129,6 +135,7 @@ module.exports = {
fromBase36,
bufferRemoveZeroes,
getFileHash,
getBufHash,
sleep,
toUnixTime,
randomHexString,