UNPKG

@mikemajara/notion-cms

Version:

A TypeScript library for using Notion as a headless CMS

2 lines 4.05 kB
'use strict';async function n(){try{return await import('@aws-sdk/client-s3')}catch{throw new Error("AWS SDK is required for S3-compatible storage. Install: npm install @aws-sdk/client-s3")}}var r=class{constructor(e){this.config=e;}async initializeS3Client(){if(!this.s3Client)try{let e=await n(),{S3Client:t}=e,i={region:this.config.region||"us-east-1"};this.config.endpoint&&!this.config.endpoint.includes("amazonaws.com")&&(i.endpoint=this.config.endpoint,i.forcePathStyle=!0),this.config.accessKey&&this.config.secretKey&&(i.credentials={accessKeyId:this.config.accessKey,secretAccessKey:this.config.secretKey}),this.s3Client=new t(i);}catch(e){throw console.warn("AWS SDK not found. Install @aws-sdk/client-s3 for S3-compatible storage:",e),e}}async exists(e){try{await this.initializeS3Client();let t=await n(),{HeadObjectCommand:i}=t;return await this.s3Client.send(new i({Bucket:this.config.bucket,Key:e})),!0}catch(t){if(t.name==="NotFound"||t.$metadata?.httpStatusCode===404)return false;throw t}}async store(e,t){try{await this.initializeS3Client();let i=await n(),{PutObjectCommand:s}=i;await this.s3Client.send(new s({Bucket:this.config.bucket,Key:e,Body:t,ContentType:this.getContentType(e)}));}catch(i){throw console.error(`Failed to store file ${e} in S3:`,i),i}}getPublicUrl(e){let{endpoint:t,bucket:i,region:s}=this.config;if(!i||typeof i!="string"||i.trim()==="")throw new Error("S3Storage: Bucket name is missing or invalid in config.");if(!t||typeof t!="string"||t.trim()==="")return `https://${i}.s3.${s||"us-east-1"}.amazonaws.com/${encodeURIComponent(e)}`;if(t.includes("amazonaws.com"))return `https://${i}.s3.${s||"us-east-1"}.amazonaws.com/${encodeURIComponent(e)}`;let c=t.replace(/\/+$/,""),l=i.replace(/\/+$/,""),g=e.replace(/^\/+/,"");return `${c}/${l}/${encodeURIComponent(g)}`}async delete(e){try{await this.initializeS3Client();let t=await n(),{DeleteObjectCommand:i}=t;await this.s3Client.send(new i({Bucket:this.config.bucket,Key:e}));}catch(t){throw console.error(`Failed to delete file ${e} from S3:`,t),t}}async listFiles(){try{await this.initializeS3Client();let e=await n(),{ListObjectsV2Command:t}=e;return ((await this.s3Client.send(new t({Bucket:this.config.bucket}))).Contents||[]).filter(s=>typeof s.Key=="string"&&s.Key.length>0).map(s=>s.Key)}catch(e){throw console.error("Failed to list files from S3:",e),e}}async getFileSize(e){try{await this.initializeS3Client();let t=await n(),{HeadObjectCommand:i}=t;return (await this.s3Client.send(new i({Bucket:this.config.bucket,Key:e}))).ContentLength||0}catch(t){return console.error(`Failed to get file size for ${e}:`,t),0}}getContentType(e){let t=e.split(".").pop()?.toLowerCase();return {jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",pdf:"application/pdf",mp4:"video/mp4",mov:"video/quicktime",mp3:"audio/mpeg",wav:"audio/wav",zip:"application/zip",txt:"text/plain",md:"text/markdown"}[t||""]||"application/octet-stream"}},o=class{constructor(e){this.storagePath=e;}async exists(e){let{fileExists:t}=await import('./file-utils-42FAMQFG.js'),i=`${this.storagePath}/${e}`;return t(i)}async store(e,t){let{writeFile:i}=await import('./file-utils-42FAMQFG.js'),s=`${this.storagePath}/${e}`;await i(s,t);}getPublicUrl(e){let t=this.storagePath;t.startsWith("./public")?t=t.replace(/^\.\/public\/?/,""):t.startsWith("public")&&(t=t.replace(/^public\/?/,"")),t=t.replace(/^\/+|\/+$/g,"");let i=e.replace(/^\/+/,"");return t?`/${t}/${i}`:`/${i}`}async delete(e){try{let t=await import('fs/promises'),i=`${this.storagePath}/${e}`;await t.unlink(i);}catch(t){console.warn(`Failed to delete file ${e}:`,t);}}async listFiles(){try{return (await(await import('fs/promises')).readdir(this.storagePath,{withFileTypes:!0})).filter(i=>i.isFile()).map(i=>i.name)}catch(e){return console.warn(`Failed to list files in ${this.storagePath}:`,e),[]}}async getFileSize(e){let{getFileSize:t}=await import('./file-utils-42FAMQFG.js'),i=`${this.storagePath}/${e}`;return t(i)}}; exports.LocalStorage=o;exports.S3Storage=r;