@versatiles/google-cloud
Version:
A server for VersaTiles in Google Cloud Run
55 lines (54 loc) • 1.53 kB
JavaScript
import { AbstractBucket, AbstractBucketFile } from './abstract.js';
import { access, constants, stat } from 'fs/promises';
import { createReadStream } from 'fs';
import { resolve } from 'path';
import { BucketFileMetadata } from './metadata.js';
export class BucketFileLocal extends AbstractBucketFile {
#filename;
constructor(filename) {
super();
this.#filename = filename;
}
get name() {
return this.#filename;
}
// Check if the file exists
async exists() {
try {
await access(this.#filename, constants.R_OK);
return true;
}
catch (_) {
return false;
}
}
// Get metadata for the file
async getMetadata() {
const statResult = await stat(this.#filename);
return new BucketFileMetadata({
cacheControl: undefined,
contentType: undefined,
etag: undefined,
filename: this.#filename,
mtime: statResult.mtime,
size: statResult.size,
});
}
// Create a readable stream for the file
createReadStream(opt) {
return createReadStream(this.#filename, opt);
}
}
export class BucketLocal extends AbstractBucket {
#basePath;
constructor(basePath) {
super();
this.#basePath = basePath;
}
async check() {
await access(this.#basePath);
}
getFile(relativePath) {
return new BucketFileLocal(resolve(this.#basePath, relativePath));
}
}