Skip to content

Instantly share code, notes, and snippets.

@clarkmcc
Created October 29, 2019 01:41
Show Gist options
  • Save clarkmcc/3e46e024dfe2df8f2d76b7ce83b143fb to your computer and use it in GitHub Desktop.
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).
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