-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathStorageService.ts
More file actions
56 lines (47 loc) · 1.71 KB
/
StorageService.ts
File metadata and controls
56 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { MultiCloudManager } from '../storage/MultiCloudManager';
import { StorageOptimizer, StorageTier } from '../storage/StorageOptimizer';
import { DataCompressor } from '../storage/DataCompressor';
export class StorageService {
private cloudManager: MultiCloudManager;
private optimizer: StorageOptimizer;
private compressor: DataCompressor;
constructor() {
this.cloudManager = new MultiCloudManager();
this.optimizer = new StorageOptimizer();
this.compressor = new DataCompressor();
}
async storeFile(fileName: string, content: Buffer, options: { redundant?: boolean } = {}): Promise<any> {
// 1. Compress data
const { buffer, compressed } = await this.compressor.compress(content);
// 2. Upload to primary cloud (with automatic failover)
const result = await this.cloudManager.upload(fileName, buffer);
// 3. Handle redundancy if requested
if (options.redundant) {
await this.cloudManager.replicate(fileName, buffer);
}
return {
fileName,
provider: result.provider,
url: result.url,
isCompressed: compressed,
tier: StorageTier.HOT
};
}
async retrieveFile(fileName: string, provider: any, isCompressed: boolean): Promise<Buffer> {
try {
const stream = await this.cloudManager.getDownloadStream(fileName, provider);
// Convert stream to Buffer
const chunks: any[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
let data = Buffer.concat(chunks);
if (isCompressed) {
return await this.compressor.decompress(data);
}
return data;
} catch (error) {
throw new Error(`Failed to retrieve file ${fileName}: ${error.message}`);
}
}
}