import fs from "fs" ; import path from "path" ; import pify from "pify" ; import TextDatasource from "./TextDatasource" ; import { fireInstantiationHandlers , registerDatasource } from "./register" ; import Credentials from "../credentials/Credentials" ; import { getCredentials } from "../credentials/channel" ; import { ATTACHMENT_EXT } from "../tools/attachments" ; import { AttachmentDetails , BufferLike , DatasourceConfigurationFile , DatasourceLoadedData , EncryptedContent , History , VaultID } from "../types" ; augments TextDatasource memberof export default class FileDatasource extends TextDatasource { _filename : string ; mkdir : Function ; readFile : Function ; stat : Function ; unlink : Function ; writeFile : Function ; param credentials constructor ( credentials ) { super ( credentials ) ; const { data : credentialData } = getCredentials ( credentials . id ) ; const { datasource : datasourceConfig } = credentialData as { datasource : DatasourceConfigurationFile ; } ; const { path } = datasourceConfig ; this . _filename = path ; this . mkdir = pify ( fs . mkdir ) ; this . readFile = pify ( fs . readFile ) ; this . stat = pify ( fs . stat ) ; this . unlink = pify ( fs . unlink ) ; this . writeFile = pify ( fs . writeFile ) ; this . type = "file" ; fireInstantiationHandlers ( "file" , this ) ; } get baseDir ( ) : string { return path . dirname ( this . path ) ; } memberof get path ( ) { return this . _filename ; } memberof protected async _ensureAttachmentsPaths ( vaultID ) { const attachmentsDir = path . join ( this . baseDir , ".buttercup" , vaultID ) ; await this . mkdir ( attachmentsDir , { recursive : true } ) ; } param vaultID param attachmentID memberof async getAttachment ( vaultID , attachmentID ) { await this . _ensureAttachmentsPaths ( vaultID ) ; const attachmentPath = path . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; return this . readFile ( attachmentPath ) ; } memberof getConfiguration ( ) { return { type : "file" , path : this . _filename } ; } param credentials returns memberof load ( credentials ) { return this . hasContent ? super . load ( credentials ) : this . readFile ( this . path , "utf8" ) . then ( contents => { this . setContent ( contents ) ; 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 = path . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; await this . writeFile ( attachmentPath , buffer ) ; } param vaultID param attachmentID memberof async removeAttachment ( vaultID , attachmentID ) { await this . _ensureAttachmentsPaths ( vaultID ) ; const attachmentPath = path . join ( this . baseDir , ".buttercup" , vaultID , `${ attachmentID }.${ ATTACHMENT_EXT }` ) ; await this . unlink ( attachmentPath ) ; } param history param credentials returns memberof save ( history , credentials ) { return super . save ( history , credentials ) . then ( encrypted => this . writeFile ( this . path , encrypted ) ) ; } memberof supportsAttachments ( ) { return true ; } returns memberof supportsRemoteBypass ( ) { return true ; } } registerDatasource ( "file" , FileDatasource ) ;