omnivore/packages/api/test/mock_storage.ts

77 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

import { Writable } from 'stream'
2024-05-15 07:53:42 +00:00
export class MockStorage {
buckets: { [name: string]: MockBucket }
constructor() {
this.buckets = {}
}
bucket(name: string) {
return this.buckets[name] || (this.buckets[name] = new MockBucket(name))
}
}
2024-05-15 07:53:42 +00:00
class MockBucket {
name: string
files: { [path: string]: MockFile }
constructor(name: string) {
this.name = name
this.files = {}
}
file(path: string) {
return this.files[path] || (this.files[path] = new MockFile(path))
}
}
class MockFile {
path: string
contents: Buffer
constructor(path: string) {
this.path = path
this.contents = Buffer.alloc(0)
}
createWriteStream() {
return new MockWriteStream(this)
}
getSignedUrl() {
2025-09-24 10:37:39 +00:00
return ['https://signed-url.upload.omnivore.work']
}
getMetadata() {
return [{ md5Hash: 'md5Hash' }]
}
publicUrl() {
2025-09-24 10:37:39 +00:00
return 'https://public-url.upload.omnivore.work'
}
makePublic() {
return
}
2024-05-14 14:47:10 +00:00
save() {
2024-05-15 03:02:18 +00:00
console.log('Saved file to:', this.path)
2024-05-14 14:47:10 +00:00
return
}
}
class MockWriteStream extends Writable {
file: MockFile
constructor(file: MockFile) {
super()
this.file = file
}
_write(chunk: Buffer, encoding: string, callback: (error?: Error) => void) {
this.file.contents = Buffer.concat([this.file.contents, chunk])
callback()
}
}