Created
October 29, 2019 01:41
-
-
Save clarkmcc/3e46e024dfe2df8f2d76b7ce83b143fb to your computer and use it in GitHub Desktop.
This method is attached to a server-side inversify service that retrieves a list of executable's depending on the provided parameters. It returns a list of those executables (after parsing them) to the called. This snippet makes use of several string methods (split, pop, replace) as well as array methods (filter, forEach).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async getModuleVersions(modulePrefix: string): Promise<any> { | |
try { | |
let fileExtension: string; | |
let moduleLocation: string; | |
switch (modulePrefix) { | |
case 'module1': | |
fileExtension = 'exe'; | |
moduleLocation = ''; | |
break; | |
case 'module2': | |
fileExtension = 'exe'; | |
moduleLocation = ''; | |
break; | |
case 'module3': | |
fileExtension = 'exe'; | |
moduleLocation = ''; | |
break; | |
case 'module4': | |
fileExtension = 'dll'; | |
moduleLocation = ''; | |
break; | |
default: | |
fileExtension = 'file'; | |
moduleLocation = ''; | |
break; | |
} | |
const options = { prefix: `modules/${modulePrefix}/` }; | |
const [files]: Array<any> = await storage | |
.bucket(process.env.GOOGLE_STORAGE_ASSETS_BUCKET_NAME) | |
.getFiles(options); | |
const versions: Array<any> = []; | |
files | |
.filter(file => file.metadata.name.includes(fileExtension)) | |
.forEach(file => { | |
let md5Hex: string; | |
try { | |
md5Hex = Buffer.from(file.metadata.md5Hash, 'base64').toString('hex'); | |
} catch { | |
md5Hex = 'unable-to-parse'; | |
} | |
const stringVersion = file.metadata.name | |
.split('/') | |
.pop() | |
.replace(`.${fileExtension}`, ''); | |
const numberVersion = parseInt(stringVersion.replace(/\./g, ''), 10); | |
versions.push({ | |
modulePrefix: modulePrefix, | |
stringVersion: stringVersion, | |
numberVerion: numberVersion, | |
filename: file.metadata.name, | |
moduleName: `${moduleLocation}${modulePrefix}.${fileExtension}`, | |
md5: md5Hex, | |
url: `https://filestore.com/${file.metadata.name}` | |
}); | |
}); | |
return versions; | |
} catch (e) { | |
console.log(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment