@groundbrick/storage-service 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/StorageService.d.ts +13 -0
- package/dist/StorageService.d.ts.map +1 -0
- package/dist/StorageService.js +34 -0
- package/dist/StorageService.js.map +1 -0
- package/dist/StorageServiceFactory.d.ts +5 -0
- package/dist/StorageServiceFactory.d.ts.map +1 -0
- package/dist/StorageServiceFactory.js +20 -0
- package/dist/StorageServiceFactory.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/IStorageProvider.d.ts +50 -0
- package/dist/interfaces/IStorageProvider.d.ts.map +1 -0
- package/dist/interfaces/IStorageProvider.js +2 -0
- package/dist/interfaces/IStorageProvider.js.map +1 -0
- package/dist/providers/LocalStorageProvider.d.ts +12 -0
- package/dist/providers/LocalStorageProvider.d.ts.map +1 -0
- package/dist/providers/LocalStorageProvider.js +53 -0
- package/dist/providers/LocalStorageProvider.js.map +1 -0
- package/dist/providers/S3StorageProvider.d.ts +12 -0
- package/dist/providers/S3StorageProvider.d.ts.map +1 -0
- package/dist/providers/S3StorageProvider.js +98 -0
- package/dist/providers/S3StorageProvider.js.map +1 -0
- package/dist/providers/index.d.ts +3 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +3 -0
- package/dist/providers/index.js.map +1 -0
- package/package.json +6 -7
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StorageConfig } from './interfaces/IStorageProvider.js';
|
|
2
|
+
export declare class StorageService {
|
|
3
|
+
private static instance;
|
|
4
|
+
private provider;
|
|
5
|
+
private constructor();
|
|
6
|
+
static getInstance(config?: StorageConfig): StorageService;
|
|
7
|
+
upload(file: Buffer, path: string, options?: any): Promise<import("./index.js").StorageResult>;
|
|
8
|
+
download(path: string): Promise<import("./index.js").StorageFile>;
|
|
9
|
+
delete(path: string): Promise<void>;
|
|
10
|
+
getUrl(path: string): Promise<string>;
|
|
11
|
+
exists(path: string): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=StorageService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StorageService.d.ts","sourceRoot":"","sources":["../src/StorageService.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAGnF,qBAAa,cAAc;IACzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAAmB;IAEnC,OAAO;WAIO,WAAW,CAAC,MAAM,CAAC,EAAE,aAAa,GAAG,cAAc;IAW3D,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG;IAIhD,QAAQ,CAAC,IAAI,EAAE,MAAM;IAIrB,MAAM,CAAC,IAAI,EAAE,MAAM;IAInB,MAAM,CAAC,IAAI,EAAE,MAAM;IAInB,MAAM,CAAC,IAAI,EAAE,MAAM;CAG1B"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { StorageServiceFactory } from './StorageServiceFactory.js';
|
|
2
|
+
export class StorageService {
|
|
3
|
+
static instance;
|
|
4
|
+
provider;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.provider = StorageServiceFactory.create(config);
|
|
7
|
+
}
|
|
8
|
+
static getInstance(config) {
|
|
9
|
+
if (!StorageService.instance) {
|
|
10
|
+
if (!config) {
|
|
11
|
+
throw new Error('Config é necessária na primeira inicialização');
|
|
12
|
+
}
|
|
13
|
+
StorageService.instance = new StorageService(config);
|
|
14
|
+
}
|
|
15
|
+
return StorageService.instance;
|
|
16
|
+
}
|
|
17
|
+
// Métodos proxy para o provider
|
|
18
|
+
async upload(file, path, options) {
|
|
19
|
+
return this.provider.upload(file, path, options);
|
|
20
|
+
}
|
|
21
|
+
async download(path) {
|
|
22
|
+
return this.provider.download(path);
|
|
23
|
+
}
|
|
24
|
+
async delete(path) {
|
|
25
|
+
return this.provider.delete(path);
|
|
26
|
+
}
|
|
27
|
+
async getUrl(path) {
|
|
28
|
+
return this.provider.getUrl(path);
|
|
29
|
+
}
|
|
30
|
+
async exists(path) {
|
|
31
|
+
return this.provider.exists(path);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=StorageService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StorageService.js","sourceRoot":"","sources":["../src/StorageService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAEnE,MAAM,OAAO,cAAc;IACjB,MAAM,CAAC,QAAQ,CAAiB;IAChC,QAAQ,CAAmB;IAEnC,YAAoB,MAAqB;QACvC,IAAI,CAAC,QAAQ,GAAG,qBAAqB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvD,CAAC;IAEM,MAAM,CAAC,WAAW,CAAC,MAAsB;QAC9C,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC;YAC7B,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;YACnE,CAAC;YACD,cAAc,CAAC,QAAQ,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;QACvD,CAAC;QACD,OAAO,cAAc,CAAC,QAAQ,CAAC;IACjC,CAAC;IAED,gCAAgC;IAChC,KAAK,CAAC,MAAM,CAAC,IAAY,EAAE,IAAY,EAAE,OAAa;QACpD,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StorageServiceFactory.d.ts","sourceRoot":"","sources":["../src/StorageServiceFactory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAInF,qBAAa,qBAAqB;IAChC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB;CAkBvD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { LocalStorageProvider } from './providers/LocalStorageProvider.js';
|
|
2
|
+
import { S3StorageProvider } from './providers/S3StorageProvider.js';
|
|
3
|
+
export class StorageServiceFactory {
|
|
4
|
+
static create(config) {
|
|
5
|
+
switch (config.provider) {
|
|
6
|
+
case 'local':
|
|
7
|
+
return new LocalStorageProvider(config.config);
|
|
8
|
+
case 's3':
|
|
9
|
+
return new S3StorageProvider(config.config);
|
|
10
|
+
// Futuramente você pode adicionar:
|
|
11
|
+
// case 'gcs':
|
|
12
|
+
// return new GCSStorageProvider(config.config as any);
|
|
13
|
+
// case 'azure':
|
|
14
|
+
// return new AzureStorageProvider(config.config as any);
|
|
15
|
+
default:
|
|
16
|
+
throw new Error(`Provider ${config.provider} não suportado`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=StorageServiceFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StorageServiceFactory.js","sourceRoot":"","sources":["../src/StorageServiceFactory.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAErE,MAAM,OAAO,qBAAqB;IAChC,MAAM,CAAC,MAAM,CAAC,MAAqB;QACjC,QAAQ,MAAM,CAAC,QAAQ,EAAE,CAAC;YACxB,KAAK,OAAO;gBACV,OAAO,IAAI,oBAAoB,CAAC,MAAM,CAAC,MAAa,CAAC,CAAC;YAExD,KAAK,IAAI;gBACP,OAAO,IAAI,iBAAiB,CAAC,MAAM,CAAC,MAAa,CAAC,CAAC;YAErD,mCAAmC;YACnC,cAAc;YACd,yDAAyD;YACzD,gBAAgB;YAChB,2DAA2D;YAE3D;gBACE,MAAM,IAAI,KAAK,CAAC,YAAY,MAAM,CAAC,QAAQ,gBAAgB,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,cAAc,kCAAkC,CAAC;AACjD,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,cAAc,kCAAkC,CAAC;AACjD,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface IStorageProvider {
|
|
2
|
+
upload(file: Buffer, path: string, options?: UploadOptions): Promise<StorageResult>;
|
|
3
|
+
download(path: string): Promise<StorageFile>;
|
|
4
|
+
delete(path: string): Promise<void>;
|
|
5
|
+
getUrl(path: string): Promise<string>;
|
|
6
|
+
exists(path: string): Promise<boolean>;
|
|
7
|
+
}
|
|
8
|
+
export interface UploadOptions {
|
|
9
|
+
contentType?: string;
|
|
10
|
+
metadata?: Record<string, string>;
|
|
11
|
+
isPublic?: boolean;
|
|
12
|
+
cacheControl?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface StorageResult {
|
|
15
|
+
url: string;
|
|
16
|
+
path: string;
|
|
17
|
+
size: number;
|
|
18
|
+
contentType?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface StorageFile {
|
|
21
|
+
buffer: Buffer;
|
|
22
|
+
contentType?: string;
|
|
23
|
+
metadata?: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
export type StorageProviderType = 'local' | 's3' | 'gcs' | 'azure';
|
|
26
|
+
export interface StorageConfig {
|
|
27
|
+
provider: StorageProviderType;
|
|
28
|
+
config: LocalStorageConfig | S3StorageConfig | GCSStorageConfig | AzureStorageConfig;
|
|
29
|
+
}
|
|
30
|
+
export interface LocalStorageConfig {
|
|
31
|
+
basePath: string;
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
}
|
|
34
|
+
export interface S3StorageConfig {
|
|
35
|
+
bucket: string;
|
|
36
|
+
region: string;
|
|
37
|
+
accessKeyId: string;
|
|
38
|
+
secretAccessKey: string;
|
|
39
|
+
endpoint?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface GCSStorageConfig {
|
|
42
|
+
bucket: string;
|
|
43
|
+
projectId: string;
|
|
44
|
+
keyFilename: string;
|
|
45
|
+
}
|
|
46
|
+
export interface AzureStorageConfig {
|
|
47
|
+
connectionString: string;
|
|
48
|
+
containerName: string;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=IStorageProv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AmB,CAAC;IAC9B,MAAM,EAAE,kBAAkB,GAAG,eAAe,GAAG,gBAAgB,GAAG,kBAAkB,CAAC;CACtF;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,kBAAkB;IACjC,gBAAgB,EAAE,MAAM,CAAC;IACzB,aAAa,EAAE,MAAM,CAAC;CACvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IStorageProvider.js","sourceRoot":"","sources":["../../src/interfaces/IStorageProvider.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IStorageProvider, StorageResult, UploadOptions, LocalStorageConfig, StorageFile } from '../interfaces/IStorageProvider.js';
|
|
2
|
+
export declare class LocalStorageProvider implements IStorageProvider {
|
|
3
|
+
private basePath;
|
|
4
|
+
private baseUrl;
|
|
5
|
+
constructor(config: LocalStorageConfig);
|
|
6
|
+
upload(file: Buffer, filePath: string, options?: UploadOptions): Promise<StorageResult>;
|
|
7
|
+
download(filePath: string): Promise<StorageFile>;
|
|
8
|
+
delete(filePath: string): Promise<void>;
|
|
9
|
+
getUrl(filePath: string): Promise<string>;
|
|
10
|
+
exists(filePath: string): Promise<boolean>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=LocalStorageProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalStorageProvider.d.ts","sourceRoot":"","sources":["../../src/providers/LocalStorageProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,aAAa,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAKpI,qBAAa,oBAAqB,YAAW,gBAAgB;IAC3D,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;gBAEZ,MAAM,EAAE,kBAAkB;IAKhC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAoBvF,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAWhD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKvC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIzC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CASjD"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as fs from 'fs/promises';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { lookup as lookupMime } from 'mime-types';
|
|
4
|
+
export class LocalStorageProvider {
|
|
5
|
+
basePath;
|
|
6
|
+
baseUrl;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.basePath = config.basePath;
|
|
9
|
+
this.baseUrl = config.baseUrl;
|
|
10
|
+
}
|
|
11
|
+
async upload(file, filePath, options) {
|
|
12
|
+
const fullPath = path.join(this.basePath, filePath);
|
|
13
|
+
const dir = path.dirname(fullPath);
|
|
14
|
+
// Garante que o diretório existe
|
|
15
|
+
await fs.mkdir(dir, { recursive: true });
|
|
16
|
+
// Salva o arquivo
|
|
17
|
+
await fs.writeFile(fullPath, file);
|
|
18
|
+
const stats = await fs.stat(fullPath);
|
|
19
|
+
return {
|
|
20
|
+
url: `${this.baseUrl}/${filePath}`,
|
|
21
|
+
path: filePath,
|
|
22
|
+
size: stats.size,
|
|
23
|
+
contentType: options?.contentType
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
async download(filePath) {
|
|
27
|
+
const fullPath = path.join(this.basePath, filePath);
|
|
28
|
+
const buffer = await fs.readFile(fullPath);
|
|
29
|
+
const contentType = lookupMime(filePath) || undefined;
|
|
30
|
+
return {
|
|
31
|
+
buffer,
|
|
32
|
+
contentType: typeof contentType === 'string' ? contentType : undefined
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
async delete(filePath) {
|
|
36
|
+
const fullPath = path.join(this.basePath, filePath);
|
|
37
|
+
await fs.unlink(fullPath);
|
|
38
|
+
}
|
|
39
|
+
async getUrl(filePath) {
|
|
40
|
+
return `${this.baseUrl}/${filePath}`;
|
|
41
|
+
}
|
|
42
|
+
async exists(filePath) {
|
|
43
|
+
const fullPath = path.join(this.basePath, filePath);
|
|
44
|
+
try {
|
|
45
|
+
await fs.access(fullPath);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=LocalStorageProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalStorageProvider.js","sourceRoot":"","sources":["../../src/providers/LocalStorageProvider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,OAAO,oBAAoB;IACvB,QAAQ,CAAS;IACjB,OAAO,CAAS;IAExB,YAAY,MAA0B;QACpC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;Q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,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QACpD,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,QAAgB;QAC3B,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,QAAQ,EAAE,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,QAAgB;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QACpD,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC1B,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IStorageProvider, StorageResult, UploadOptions, S3StorageConfig, StorageFile } from '../interfaces/IStorageProvider.js';
|
|
2
|
+
export declare class S3StorageProvider implements IStorageProvider {
|
|
3
|
+
private client;
|
|
4
|
+
private bucket;
|
|
5
|
+
constructor(config: S3StorageConfig);
|
|
6
|
+
upload(file: Buffer, filePath: string, options?: UploadOptions): Promise<StorageResult>;
|
|
7
|
+
download(filePath: string): Promise<StorageFile>;
|
|
8
|
+
delete(filePath: string): Promise<void>;
|
|
9
|
+
getUrl(filePath: string): Promise<string>;
|
|
10
|
+
exists(filePath: string): Promise<boolean>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=S3StorageProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"S3StorageProvider.d.ts","sourceRoot":"","sources":["../../src/providers/S3StorageProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAYjI,qBAAa,iBAAkB,YAAW,gBAAgB;IACxD,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,EAAE,eAAe;IAY7B,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IA4CvF,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBhD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAUzC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAYjD"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, HeadObjectCommand, S3ServiceException } from '@aws-sdk/client-s3';
|
|
2
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
3
|
+
export class S3StorageProvider {
|
|
4
|
+
client;
|
|
5
|
+
bucket;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.client = new S3Client({
|
|
8
|
+
region: config.region,
|
|
9
|
+
credentials: {
|
|
10
|
+
accessKeyId: config.accessKeyId,
|
|
11
|
+
secretAccessKey: config.secretAccessKey
|
|
12
|
+
},
|
|
13
|
+
...(config.endpoint && { endpoint: config.endpoint })
|
|
14
|
+
});
|
|
15
|
+
this.bucket = config.bucket;
|
|
16
|
+
}
|
|
17
|
+
async upload(file, filePath, options) {
|
|
18
|
+
const commandInput = {
|
|
19
|
+
Bucket: this.bucket,
|
|
20
|
+
Key: filePath,
|
|
21
|
+
Body: file,
|
|
22
|
+
ContentType: options?.contentType,
|
|
23
|
+
Metadata: options?.metadata,
|
|
24
|
+
CacheControl: options?.cacheControl,
|
|
25
|
+
...(options?.isPublic ? { ACL: 'public-read' } : {})
|
|
26
|
+
};
|
|
27
|
+
try {
|
|
28
|
+
await this.client.send(new PutObjectCommand(commandInput));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const s3Error = error;
|
|
32
|
+
const message = typeof s3Error.message === 'string' ? s3Error.message : '';
|
|
33
|
+
const aclUnsupported = s3Error instanceof S3ServiceException &&
|
|
34
|
+
'ACL' in commandInput &&
|
|
35
|
+
(s3Error.name === 'AccessControlListNotSupported' ||
|
|
36
|
+
s3Error.code === 'AccessControlListNotSupported' ||
|
|
37
|
+
message.includes('does not allow ACL') ||
|
|
38
|
+
message.includes('AccessControlListNotSupported'));
|
|
39
|
+
if (aclUnsupported) {
|
|
40
|
+
const { ACL, ...fallbackInput } = commandInput;
|
|
41
|
+
await this.client.send(new PutObjectCommand(fallbackInput));
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const url = await this.getUrl(filePath);
|
|
48
|
+
return {
|
|
49
|
+
url,
|
|
50
|
+
path: filePath,
|
|
51
|
+
size: file.length,
|
|
52
|
+
contentType: options?.contentType
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async download(filePath) {
|
|
56
|
+
const command = new GetObjectCommand({
|
|
57
|
+
Bucket: this.bucket,
|
|
58
|
+
Key: filePath
|
|
59
|
+
});
|
|
60
|
+
const response = await this.client.send(command);
|
|
61
|
+
const stream = response.Body;
|
|
62
|
+
const buffer = Buffer.from(await stream.transformToByteArray());
|
|
63
|
+
return {
|
|
64
|
+
buffer,
|
|
65
|
+
contentType: response.ContentType,
|
|
66
|
+
metadata: response.Metadata
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async delete(filePath) {
|
|
70
|
+
const command = new DeleteObjectCommand({
|
|
71
|
+
Bucket: this.bucket,
|
|
72
|
+
Key: filePath
|
|
73
|
+
});
|
|
74
|
+
await this.client.send(command);
|
|
75
|
+
}
|
|
76
|
+
async getUrl(filePath) {
|
|
77
|
+
const command = new GetObjectCommand({
|
|
78
|
+
Bucket: this.bucket,
|
|
79
|
+
Key: filePath
|
|
80
|
+
});
|
|
81
|
+
// URL assinada válida por 1 hora
|
|
82
|
+
return await getSignedUrl(this.client, command, { expiresIn: 3600 });
|
|
83
|
+
}
|
|
84
|
+
async exists(filePath) {
|
|
85
|
+
try {
|
|
86
|
+
const command = new HeadObjectCommand({
|
|
87
|
+
Bucket: this.bucket,
|
|
88
|
+
Key: filePath
|
|
89
|
+
});
|
|
90
|
+
await this.client.send(command);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=S3StorageProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"S3StorageProvider.js","sourceRoot":"","sources":["../../src/providers/S3StorageProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,QAAQ,EACR,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EAEjB,kBAAkB,EACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAE7D,MAAM,OAAO,iBAAiB;IACpB,MAAM,CAAW;IACjB,MAAM,CAAS;IAEvB,YAAY,MAAuB;QACjC,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC;YACzB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,WAAW,EAAE;gBACX,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,eAAe,EAAE,MAAM,CAAC,eAAe;aACxC;YACD,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC;SACtD,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,GA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IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACvE,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,QAAgB;QAC3B,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,IAAI,iBAAiB,CAAC;gBACpC,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,GAAG,EAAE,QAAQ;aACd,CAAC,CAAC;YACH,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groundbrick/storage-service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Serviço de storage plugável com suporte a múltiplos provedores (Local, S3, GCS, Azure)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,13 +18,12 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
|
-
"build": "tsc
|
|
22
|
-
"
|
|
23
|
-
"clean": "rm -rf dist",
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"dev": "tsc --watch",
|
|
23
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
24
24
|
"prepublishOnly": "npm run clean && npm run build",
|
|
25
|
-
"lint": "eslint src
|
|
26
|
-
"
|
|
27
|
-
"prepare": "npm run build"
|
|
25
|
+
"lint": "eslint src/**/*.ts",
|
|
26
|
+
"lint:fix": "eslint src/**/*.ts --fix"
|
|
28
27
|
},
|
|
29
28
|
"keywords": [
|
|
30
29
|
"storage",
|