spee.ch/server/utils/awaitFileSize.js

33 lines
981 B
JavaScript
Raw Normal View History

const { getFileListFileByOutpoint } = require('server/lbrynet');
const logger = require('winston');
2019-02-26 07:56:44 +01:00
function delay(t) {
return new Promise(function(resolve) {
setTimeout(resolve, t);
2019-02-26 07:56:44 +01:00
});
}
const awaitFileSize = (outpoint, size, interval, timeout) => {
logger.debug('awaitFileSize');
let start = Date.now();
function checkFileList() {
logger.debug('checkFileList');
return getFileListFileByOutpoint(outpoint).then(result => {
2020-01-13 00:48:52 +01:00
const { items: fileInfos } = result;
const fileInfo = fileInfos[0];
logger.debug('File List Result', fileInfo);
if (fileInfo.completed === true || fileInfo.written_bytes > size) {
logger.debug('FILE READY');
return 'ready';
} else if (timeout !== 0 && Date.now() - start > timeout) {
throw new Error('Timeout on awaitFileSize');
} else {
return delay(interval).then(checkFileList);
}
});
}
return checkFileList();
2019-02-26 07:56:44 +01:00
};
module.exports = awaitFileSize;