11import path from 'path'
22import { throttle } from 'lodash-es'
33
4- import { AwsS3Storage } from './AwsS3Storage .js'
4+ import { S3Storage } from './S3Storage .js'
55import { FileStorage } from './FileStorage.js'
66
77export class StorageManager {
8+ static STORAGE_TYPE = {
9+ LOCAL : 'local' ,
10+ S3 : 's3' ,
11+ }
12+
813 constructor ( ) {
914 this . storage = null
1015 this . isS3 = false
1116 this . storageData = { }
1217 this . storageLoaded = false
13-
18+
1419 // Throttle saves to avoid too many writes
1520 this . saveStorageData = throttle ( ( ) => this . persistStorageData ( ) , 1000 , { leading : true , trailing : true } )
1621 }
@@ -30,26 +35,26 @@ export class StorageManager {
3035 storagePrefix : process . env . S3_STORAGE_PREFIX || 'storage/' ,
3136 cloudfrontUrl : process . env . CLOUDFRONT_URL ,
3237 }
33-
34- if ( process . env . AWS_ACCESS_KEY_ID && process . env . AWS_SECRET_ACCESS_KEY ) {
38+
39+ if ( process . env . S3_ACCESS_KEY_ID && process . env . S3_SECRET_ACCESS_KEY ) {
3540 s3Config . credentials = {
36- accessKeyId : process . env . AWS_ACCESS_KEY_ID ,
37- secretAccessKey : process . env . AWS_SECRET_ACCESS_KEY ,
41+ accessKeyId : process . env . S3_ACCESS_KEY_ID ,
42+ secretAccessKey : process . env . S3_SECRET_ACCESS_KEY ,
3843 }
3944 }
40-
41- this . storage = new AwsS3Storage ( s3Config )
42-
45+
46+ this . storage = new S3Storage ( s3Config )
47+
4348 console . log ( 'Initializing S3 storage...' )
4449 await this . storage . initialize ( )
45-
50+
4651 } else {
4752 // Initialize local file storage
4853 this . isS3 = false
4954 this . storage = new FileStorage ( {
5055 assetsUrl : '/assets/' ,
5156 } )
52-
57+
5358 console . log ( 'Initializing local file storage...' )
5459 await this . storage . initialize ( )
5560 }
@@ -96,7 +101,7 @@ export class StorageManager {
96101 console . warn ( 'Storage not yet loaded, cannot set value' )
97102 return
98103 }
99-
104+
100105 try {
101106 // Ensure value is serializable
102107 value = JSON . parse ( JSON . stringify ( value ) )
@@ -115,7 +120,7 @@ export class StorageManager {
115120 console . warn ( 'Storage not yet loaded, cannot persist' )
116121 return
117122 }
118-
123+
119124 try {
120125 await this . storage . saveStorageData ( this . storageData )
121126 // console.log('Storage data persisted successfully')
@@ -180,7 +185,7 @@ export class StorageManager {
180185 if ( this . isS3 ) {
181186 // If CloudFront URL is configured, use it with assets prefix
182187 if ( process . env . CLOUDFRONT_URL ) {
183- const baseUrl = process . env . CLOUDFRONT_URL . endsWith ( '/' )
188+ const baseUrl = process . env . CLOUDFRONT_URL . endsWith ( '/' )
184189 ? process . env . CLOUDFRONT_URL . slice ( 0 , - 1 ) // Remove trailing slash
185190 : process . env . CLOUDFRONT_URL
186191 const assetsPrefix = ( process . env . S3_ASSETS_PREFIX || 'assets/' ) . replace ( / \/ $ / , '' ) // Remove trailing slash
0 commit comments