2019-02-27 02:53:48 +01:00
|
|
|
const { getFileListFileByOutpoint } = require('server/lbrynet');
|
|
|
|
const logger = require('winston');
|
2019-02-26 07:56:44 +01:00
|
|
|
|
2019-02-27 02:53:48 +01:00
|
|
|
function delay(t) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(resolve, t);
|
2019-02-26 07:56:44 +01:00
|
|
|
});
|
2019-02-27 02:53:48 +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) {
|
2019-02-27 02:53:48 +01:00
|
|
|
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;
|