import { WebDAVClient } from "webdav" ; import pathPosix from "path-posix" ; import TextDatasource from "./TextDatasource" ; import { fireInstantiationHandlers , registerDatasource } from "./register" ; import { getSharedAppEnv } from "../env/appEnv" ; import Credentials from "../credentials/Credentials" ; import { getCredentials } from "../credentials/channel" ; import { ATTACHMENT_EXT } from "../tools/attachments" ; import { AttachmentDetails , BufferLike , DatasourceConfigurationWebDAV , DatasourceLoadedData , History , VaultID } from "../types" ; const MAX_DATA_SIZE = 200 * 1024 * 1024 ; augments TextDatasource memberof export default class WebDAVDatasource extends TextDatasource { protected _client : WebDAVClient ; private _config : DatasourceConfigurationWebDAV ; protected _path : string ; param credentials constructor ( credentials ) { super ( credentials ) ; const { data : credentialData } = getCredentials ( credentials . id ) ; const { datasource : datasourceConfig } = credentialData as { datasource : DatasourceConfigurationWebDAV ; } ; const { endpoint , password , path , username } = ( this . _config = datasourceConfig ) ; this . _path = path ; const createClient = getSharedAppEnv ( ) . getProperty ( "net/webdav/v1/newClient" ) ; if ( typeof username === "string" && typeof password === "string" ) { this . _client = createClient ( endpoint , { username , password , maxBodyLength : MAX_DATA_SIZE , maxContentLength : MAX_DATA_SIZE } ) ; } else { this . _client = createClient ( endpoint , { maxBodyLength : MAX_DATA_SIZE , maxContentLength : MAX_DATA_SIZE } ) ; } this . type = "webdav" ; fireInstantiationHandlers ( "webdav" , this ) ; } memberof get baseDir ( ) { return pathPosix . dirname ( this . path ) ; } memberof get client ( ) { return this . _client ; } memberof get path ( ) { return this . _path ; } memberof protected async _ensureAttachmentsPaths ( vaultID ) { const attachmentsDir = pathPosix . join ( this . baseDir , ".buttercup" , vaultID ) ; await this . client . createDirectory ( attachmentsDir , { recursive : true } ) ; } param vaultID param attachmentID memberof async getAttachment ( vaultID , attachmentID ) { await this . _ensureAttachmentsPaths ( vaultID ) ; const attachmentPath = pathPosix . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; return this . client . getFileContents ( attachmentPath ) as Promise < BufferLike > ; } memberof getConfiguration ( ) { return this . _config ; } param credentials returns memberof load ( credentials ) { return this . hasContent ? super . load ( credentials ) : this . client . getFileContents ( this . path , { format : "text" } ) . then ( content => { this . setContent ( content as string ) ; return super . load ( credentials ) ; } ) ; } param vaultID param attachmentID param buffer param details memberof async putAttachment ( vaultID , attachmentID , buffer , details ) { await this . _ensureAttachmentsPaths ( vaultID ) ; const attachmentPath = pathPosix . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; await this . client . putFileContents ( attachmentPath , buffer ) ; } param vaultID param attachmentID memberof async removeAttachment ( vaultID , attachmentID ) { await this . _ensureAttachmentsPaths ( vaultID ) ; const attachmentPath = pathPosix . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; await this . client . deleteFile ( attachmentPath ) ; } param history param credentials returns memberof async save ( history , credentials ) { const content = await super . save ( history , credentials ) ; await this . client . putFileContents ( this . path , content ) ; } memberof supportsAttachments ( ) { return true ; } returns memberof supportsRemoteBypass ( ) { return true ; } } registerDatasource ( "webdav" , WebDAVDatasource ) ;