Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
@@ -0,0 +1,78 @@
|
||||
function newFileStorage(host, port) {
|
||||
const MODULE_NAME = 'File Storage'
|
||||
const INFO_LOG = false
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
const MAX_RETRY = 2
|
||||
let currentRetry = 0
|
||||
|
||||
const recoverableErrors = [
|
||||
'SOCKETTIMEDOUT',
|
||||
'TIMEDOUT',
|
||||
'CONNRESET',
|
||||
'CONNREFUSED',
|
||||
'NOTFOUND',
|
||||
'ENOTFOUND',
|
||||
'ECONNREFUSED',
|
||||
'CONNREFUSED',
|
||||
'NOTFOUND',
|
||||
'ESOCKETTIMEDOUT',
|
||||
'ECONNRESET',
|
||||
'ETIMEDOUT',
|
||||
'EAI_AGAIN'
|
||||
]
|
||||
|
||||
let thisObject = {
|
||||
getFileFromHost: getFileFromHost
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
async function getFileFromHost(filePath, callBackFunction, pathComplete) {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFileFromHost -> Entering function.') }
|
||||
|
||||
let folder = ''
|
||||
if (pathComplete === false || pathComplete === undefined) {
|
||||
folder = 'Storage/'
|
||||
}
|
||||
let url = folder + filePath
|
||||
if (host !== undefined && port !== undefined) {
|
||||
url = 'http://' + host + ':' + port + '/' + folder + filePath
|
||||
} else {
|
||||
url = folder + filePath
|
||||
}
|
||||
|
||||
callWebServer(undefined, url, (response, fileContent) => {
|
||||
if (response.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, fileContent)
|
||||
} else {
|
||||
callBackFunction(response)
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
if (verifyRetry(err.code) && currentRetry < MAX_RETRY) {
|
||||
currentRetry++
|
||||
if (INFO_LOG === true) { console.log('[INFO] getTextFile -> Retrying connection to the server because received error: ' + err.code + '. Retry #: ' + currentRetry) }
|
||||
getFileFromHost(filePath, callBackFunction)
|
||||
} else if (err.message === 'Request aborted') {
|
||||
let err = { code: 'The specified key does not exist.' }
|
||||
callBackFunction(err)
|
||||
} else {
|
||||
console.log('Error geting the file from the serever:', err)
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function verifyRetry(errorCode) {
|
||||
for (let i = 0; i < recoverableErrors.length; i++) {
|
||||
const error = recoverableErrors[i]
|
||||
if (error === errorCode) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user