UNPKG

@nephele/adapter-s3

Version:

S3 (or compatible) object storage adapter for the Nephele WebDAV server.

109 lines 3.7 kB
import path from 'node:path'; import { S3 } from '@aws-sdk/client-s3'; import { BadGatewayError, MethodNotImplementedError, MethodNotSupportedError, ResourceNotFoundError, } from 'nephele'; import Resource from './Resource.js'; export default class Adapter { constructor({ s3Config, bucket, uploadQueueSize, root }) { this.uploadQueueSize = 4; this.root = ''; this.s3 = new S3(s3Config); this.bucket = bucket; if (uploadQueueSize != null) { this.uploadQueueSize = uploadQueueSize; } if (root != null) { this.root = root.split('/').map(encodeURIComponent).join('/'); } } urlToRelativePath(url, baseUrl) { if (!decodeURIComponent(url.pathname) .replace(/\/?$/, () => '/') .startsWith(decodeURIComponent(baseUrl.pathname))) { return null; } return path.join(path.sep, ...decodeURIComponent(url.pathname) .substring(decodeURIComponent(baseUrl.pathname).length) .replace(/\/?$/, '') .split('/')); } relativePathToUrl(pathname, baseUrl) { const urlPath = pathname .split(path.sep) .filter((part) => part !== '') .map(encodeURIComponent) .join('/'); return new URL(urlPath, baseUrl); } relativePathToKey(pathname) { return [this.root, ...pathname.split(path.sep).map(encodeURIComponent)] .filter((part) => part != '') .join('/'); } keyToRelativePath(key) { return (path.sep + [...key.substring(this.root.length).split('/')] .filter((part) => part != '') .map(decodeURIComponent) .join(path.sep)); } async getComplianceClasses(_url, _request, _response) { return ['2']; } async getAllowedMethods(_url, _request, _response) { return []; } async getOptionsResponseCacheControl(_url, _request, _response) { return 'max-age=604800'; } async isAuthorized(_url, _method, _baseUrl, _user) { return true; } async getResource(url, baseUrl) { const path = this.urlToRelativePath(url, baseUrl); if (path == null) { throw new BadGatewayError('The given path is not managed by this server.'); } const resource = new Resource({ adapter: this, baseUrl, path, }); if (!(await resource.exists())) { throw new ResourceNotFoundError('Resource not found.'); } return resource; } async newResource(url, baseUrl) { const path = this.urlToRelativePath(url, baseUrl); if (path == null) { throw new BadGatewayError('The given path is not managed by this server.'); } return new Resource({ adapter: this, baseUrl, path, exists: false, collection: false, }); } async newCollection(url, baseUrl) { const path = this.urlToRelativePath(url, baseUrl); if (path == null) { throw new BadGatewayError('The given path is not managed by this server.'); } return new Resource({ adapter: this, baseUrl, path, exists: false, collection: true, }); } getMethod(method) { if (method === 'POST' || method === 'PATCH') { throw new MethodNotSupportedError('Method not supported.'); } throw new MethodNotImplementedError('Method not implemented.'); } } //# sourceMappingURL=Adapter.js.map