Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async readFile(bucketName: string, filePath: string): Promise {
const cmd = new S3C.ReadFile(bucketName, filePath)
cmd.mode = this.mode
return await cmd.execute() // TODO: figure out why this s3cmd has no error interface
}
async listFiles(bucketName: string, filePath?: string): Promise {
const cmd = new S3C.ListFiles(bucketName, filePath)
cmd.mode = this.mode
return await cmd.execute() // TODO: figure out why this s3cmd has no error interface
}
async function loadJSONFile(key: string) {
const content = await new S3.ReadFile(config.bucket, key).execute()
if (content !== '') {
return JSON.parse(content)
} else {
throw new Error(`Item with id '${key}' does not exist`)
}
}
static async query(
this: { new (...args: any[]): T },
pattern: string
) {
const matches = []
const name = this.name
const regexp = new RegExp(pattern, 'gi')
const keys: string[] = await new S3.ListFiles(config.bucket, name).execute()
for (const key of keys) {
if (key.match(regexp)) {
matches.push(key)
}
}
return await Promise.all(
matches.map(async match => {
const data = await loadJSONFile(match)
return Object.assign(Object.create(this.prototype), data) as T
})
)
}
static async all(this: { new (...args: any[]): T }) {
const name = this.name
const keys: string[] = await new S3.ListFiles(config.bucket, name).execute()
return await Promise.all(
keys.map(async key => {
const data = await loadJSONFile(key)
return Object.assign(Object.create(this.prototype), data) as T
})
)
}
static async deleteAll(this: { new (...args: any[]): T }) {
const name = this.name
const keys = await new S3.ListFiles(config.bucket, name).execute()
for (const key of keys) {
await new S3.DeleteFile(config.bucket, key).execute()
}
}
static async deleteAll(this: { new (...args: any[]): T }) {
const name = this.name
const keys = await new S3.ListFiles(config.bucket, name).execute()
for (const key of keys) {
await new S3.DeleteFile(config.bucket, key).execute()
}
}
async deleteFile(
bucketName: string,
filePath: string
): Promise> {
const cmd = new S3C.DeleteFile(bucketName, filePath)
cmd.mode = this.mode
return await this.handle(cmd.execute())
}
static async delete(
this: { new (...args: any[]): T },
id: string
) {
const name = this.name
const key = `${name}/${id}.json`
return await new S3.DeleteFile(config.bucket, key).execute()
}
async writeFile(
bucketName: string,
filePath: string,
content: string
): Promise> {
const cmd = new S3C.WriteFile(bucketName, filePath, content)
cmd.mode = this.mode
return await this.handle(cmd.execute())
}