Добавлено сохранение валидных uploaded-файлов в удаленном хранилище
This commit is contained in:
@@ -241,27 +241,29 @@ class FileDecompressor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async gzipFileIfNotExists(filename, outDir) {
|
async gzipFileIfNotExists(filename, outDir, isMaxCompression) {
|
||||||
const hash = await utils.getFileHash(filename, 'sha256', 'hex');
|
const hash = await utils.getFileHash(filename, 'sha256', 'hex');
|
||||||
|
|
||||||
const outFilename = `${outDir}/${hash}`;
|
const outFilename = `${outDir}/${hash}`;
|
||||||
|
|
||||||
if (!await fs.pathExists(outFilename)) {
|
if (!await fs.pathExists(outFilename)) {
|
||||||
await this.gzipFile(filename, outFilename, 1);
|
await this.gzipFile(filename, outFilename, (isMaxCompression ? 9 : 1));
|
||||||
|
|
||||||
// переупакуем через некоторое время на максималках
|
// переупакуем через некоторое время на максималках, если упаковали плохо
|
||||||
const filenameCopy = `${filename}.copy`;
|
if (!isMaxCompression) {
|
||||||
await fs.copy(filename, filenameCopy);
|
const filenameCopy = `${filename}.copy`;
|
||||||
|
await fs.copy(filename, filenameCopy);
|
||||||
|
|
||||||
(async() => {
|
(async() => {
|
||||||
await utils.sleep(5000);
|
await utils.sleep(5000);
|
||||||
const filenameGZ = `${filename}.gz`;
|
const filenameGZ = `${filename}.gz`;
|
||||||
await this.gzipFile(filenameCopy, filenameGZ, 9);
|
await this.gzipFile(filenameCopy, filenameGZ, 9);
|
||||||
|
|
||||||
await fs.move(filenameGZ, outFilename, {overwrite: true});
|
await fs.move(filenameGZ, outFilename, {overwrite: true});
|
||||||
|
|
||||||
await fs.remove(filenameCopy);
|
await fs.remove(filenameCopy);
|
||||||
})().catch((e) => { if (appLogger.inited) appLogger.log(LM_ERR, `FileDecompressor.gzipFileIfNotExists: ${e.message}`) });
|
})().catch((e) => { if (appLogger.inited) appLogger.log(LM_ERR, `FileDecompressor.gzipFileIfNotExists: ${e.message}`) });
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await utils.touchFile(outFilename);
|
await utils.touchFile(outFilename);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ class ReaderWorker {
|
|||||||
let decompDir = '';
|
let decompDir = '';
|
||||||
let downloadedFilename = '';
|
let downloadedFilename = '';
|
||||||
let isUploaded = false;
|
let isUploaded = false;
|
||||||
|
let isRestored = false;
|
||||||
let convertFilename = '';
|
let convertFilename = '';
|
||||||
|
|
||||||
const overLoadMes = 'Слишком большая очередь загрузки. Пожалуйста, попробуйте позже.';
|
const overLoadMes = 'Слишком большая очередь загрузки. Пожалуйста, попробуйте позже.';
|
||||||
@@ -88,9 +89,17 @@ class ReaderWorker {
|
|||||||
downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
|
downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
|
||||||
await fs.writeFile(downloadedFilename, downdata);
|
await fs.writeFile(downloadedFilename, downdata);
|
||||||
} else {//uploaded file
|
} else {//uploaded file
|
||||||
downloadedFilename = `${this.config.uploadDir}/${url.substr(7)}`;
|
const fileHash = url.substr(7);
|
||||||
if (!await fs.pathExists(downloadedFilename))
|
downloadedFilename = `${this.config.uploadDir}/${fileHash}`;
|
||||||
throw new Error('Файл не найден на сервере (возможно был удален как устаревший). Пожалуйста, загрузите файл с диска на сервер заново.');
|
if (!await fs.pathExists(downloadedFilename)) {
|
||||||
|
//если удалено из upload, попробуем восстановить из удаленного хранилища
|
||||||
|
try {
|
||||||
|
downloadedFilename = await this.restoreRemoteFile(fileHash);
|
||||||
|
isRestored = true;
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error('Файл не найден на сервере (возможно был удален как устаревший). Пожалуйста, загрузите файл с диска на сервер заново.');
|
||||||
|
}
|
||||||
|
}
|
||||||
await utils.touchFile(downloadedFilename);
|
await utils.touchFile(downloadedFilename);
|
||||||
isUploaded = true;
|
isUploaded = true;
|
||||||
}
|
}
|
||||||
@@ -146,6 +155,20 @@ class ReaderWorker {
|
|||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//лениво сохраним downloadedFilename в tmp и в удаленном хранилище в случае isUploaded
|
||||||
|
if (this.remoteWebDavStorage && isUploaded && !isRestored) {
|
||||||
|
(async() => {
|
||||||
|
await utils.sleep(30*1000);
|
||||||
|
try {
|
||||||
|
//сжимаем файл в tmp, если там уже нет с тем же именем-sha256
|
||||||
|
const compDownloadedFilename = await this.decomp.gzipFileIfNotExists(downloadedFilename, this.config.tempPublicDir, true);
|
||||||
|
await this.remoteWebDavStorage.putFile(compDownloadedFilename);
|
||||||
|
} catch (e) {
|
||||||
|
log(LM_ERR, e.stack);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(LM_ERR, e.stack);
|
log(LM_ERR, e.stack);
|
||||||
if (e.message == 'abort')
|
if (e.message == 'abort')
|
||||||
@@ -188,6 +211,24 @@ class ReaderWorker {
|
|||||||
return `file://${hash}`;
|
return `file://${hash}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async restoreRemoteFile(filename) {
|
||||||
|
const basename = path.basename(filename);
|
||||||
|
const targetName = `${this.config.tempPublicDir}/${basename}`;
|
||||||
|
|
||||||
|
if (!await fs.pathExists(targetName)) {
|
||||||
|
let found = false;
|
||||||
|
if (this.remoteWebDavStorage) {
|
||||||
|
found = await this.remoteWebDavStorage.getFileSuccess(targetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
throw new Error('404 Файл не найден');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return targetName;
|
||||||
|
}
|
||||||
|
|
||||||
restoreCachedFile(filename) {
|
restoreCachedFile(filename) {
|
||||||
const workerId = this.workerState.generateWorkerId();
|
const workerId = this.workerState.generateWorkerId();
|
||||||
const wState = this.workerState.getControl(workerId);
|
const wState = this.workerState.getControl(workerId);
|
||||||
@@ -197,21 +238,10 @@ class ReaderWorker {
|
|||||||
try {
|
try {
|
||||||
wState.set({state: 'download', step: 1, totalSteps: 1, path: filename, progress: 0});
|
wState.set({state: 'download', step: 1, totalSteps: 1, path: filename, progress: 0});
|
||||||
|
|
||||||
const basename = path.basename(filename);
|
const targetName = await this.restoreRemoteFile(filename);
|
||||||
const targetName = `${this.config.tempPublicDir}/${basename}`;
|
|
||||||
|
|
||||||
if (!await fs.pathExists(targetName)) {
|
|
||||||
let found = false;
|
|
||||||
if (this.remoteWebDavStorage) {
|
|
||||||
found = await this.remoteWebDavStorage.getFileSuccess(targetName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
throw new Error('404 Файл не найден');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const stat = await fs.stat(targetName);
|
const stat = await fs.stat(targetName);
|
||||||
|
|
||||||
|
const basename = path.basename(filename);
|
||||||
wState.finish({path: `/tmp/${basename}`, size: stat.size, progress: 100});
|
wState.finish({path: `/tmp/${basename}`, size: stat.size, progress: 100});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.message.indexOf('404') < 0)
|
if (e.message.indexOf('404') < 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user