@clairejs/server 3.16.19 → 3.17.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/README.md +4 -0
- package/dist/services/AbstractFileService.d.ts +2 -0
- package/dist/services/implementations/LocalFileService.d.ts +2 -0
- package/dist/services/implementations/LocalFileService.js +9 -0
- package/dist/services/implementations/S3FileService.d.ts +3 -1
- package/dist/services/implementations/S3FileService.js +22 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { FileOperation } from "../common/FileOperation";
|
|
2
3
|
import { GetUploadUrlResponseBody } from "../controllers/dto/upload";
|
|
3
4
|
export declare abstract class AbstractFileService {
|
|
@@ -13,5 +14,6 @@ export declare abstract class AbstractFileService {
|
|
|
13
14
|
toURI: string;
|
|
14
15
|
}[]): Promise<void>;
|
|
15
16
|
abstract removeObject(uris: string[]): Promise<void>;
|
|
17
|
+
abstract putFile(uri: string, data: Buffer, headers?: Record<string, string>): Promise<void>;
|
|
16
18
|
getUploadUrlForTempUri(temporaryUri: string): Promise<GetUploadUrlResponseBody>;
|
|
17
19
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { FileOperation } from "../../common/FileOperation";
|
|
2
3
|
import { AbstractFileService } from "../AbstractFileService";
|
|
3
4
|
export declare class LocalFileService extends AbstractFileService {
|
|
@@ -6,6 +7,7 @@ export declare class LocalFileService extends AbstractFileService {
|
|
|
6
7
|
getFileSize(objectKeys: string[]): Promise<number[]>;
|
|
7
8
|
getAccessUrls(uris: (string | undefined)[], _isPublic: boolean): Promise<string[]>;
|
|
8
9
|
getPresignedUrl(operation: FileOperation, uris: string[]): Promise<string[]>;
|
|
10
|
+
putFile(uri: string, data: Buffer, headers?: Record<string, string>): Promise<void>;
|
|
9
11
|
moveObject(uris: {
|
|
10
12
|
fromURI: string;
|
|
11
13
|
toURI: string;
|
|
@@ -23,6 +23,15 @@ export class LocalFileService extends AbstractFileService {
|
|
|
23
23
|
}
|
|
24
24
|
return uris.map((uri) => `${this.fileServerUrl}/?objectKey="/${uri}"`);
|
|
25
25
|
}
|
|
26
|
+
async putFile(uri, data, headers) {
|
|
27
|
+
const [url] = await this.getPresignedUrl(FileOperation.PUT, [uri]);
|
|
28
|
+
await axios({
|
|
29
|
+
method: 'PUT',
|
|
30
|
+
url,
|
|
31
|
+
data,
|
|
32
|
+
headers,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
26
35
|
async moveObject(uris) {
|
|
27
36
|
await Promise.all(uris.map((uri) => axios.post(`${this.fileServerUrl}/`, {
|
|
28
37
|
from: uri.fromURI,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { AbstractLogger } from "@clairejs/core";
|
|
2
3
|
import { FileOperation } from "../../common/FileOperation";
|
|
3
4
|
import { AbstractFileService } from "../AbstractFileService";
|
|
@@ -13,8 +14,9 @@ export declare class S3FileService extends AbstractFileService {
|
|
|
13
14
|
protected readonly config: S3FileServiceConfig;
|
|
14
15
|
protected readonly logger: AbstractLogger;
|
|
15
16
|
private readonly s3Client;
|
|
16
|
-
private readonly cloudfrontSigner
|
|
17
|
+
private readonly cloudfrontSigner?;
|
|
17
18
|
constructor(config: S3FileServiceConfig, logger: AbstractLogger);
|
|
19
|
+
putFile(uri: string, data: Buffer, headers?: Record<string, string>): Promise<void>;
|
|
18
20
|
getFileSize(objectKeys: string[]): Promise<number[]>;
|
|
19
21
|
getAccessUrls(uris: (string | undefined)[], isPublic: boolean): Promise<string[]>;
|
|
20
22
|
getPresignedUrl(operation: FileOperation, uris: string[]): Promise<string[]>;
|
|
@@ -24,7 +24,17 @@ let S3FileService = class S3FileService extends AbstractFileService {
|
|
|
24
24
|
signatureVersion: "v4",
|
|
25
25
|
region: this.config.S3_BUCKET_REGION,
|
|
26
26
|
});
|
|
27
|
-
this.
|
|
27
|
+
if (this.config.CLOUD_FRONT_PUBLIC_DOMAIN && this.config.CLOUD_FRONT_PRIVATE_KEY && this.config.CLOUD_FRONT_PUBLIC_KEY_ID) {
|
|
28
|
+
this.cloudfrontSigner = new aws.CloudFront.Signer(this.config.CLOUD_FRONT_PUBLIC_KEY_ID, this.config.CLOUD_FRONT_PRIVATE_KEY);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async putFile(uri, data, headers) {
|
|
32
|
+
await this.s3Client.putObject({
|
|
33
|
+
Key: uri,
|
|
34
|
+
Bucket: this.config.S3_BUCKET_NAME,
|
|
35
|
+
Body: data,
|
|
36
|
+
Metadata: headers,
|
|
37
|
+
}).promise();
|
|
28
38
|
}
|
|
29
39
|
async getFileSize(objectKeys) {
|
|
30
40
|
return await Promise.all(objectKeys.map((uri) => this.getSingleFileSize(uri)));
|
|
@@ -51,10 +61,17 @@ let S3FileService = class S3FileService extends AbstractFileService {
|
|
|
51
61
|
.then((res) => res.ContentLength);
|
|
52
62
|
return size || 0;
|
|
53
63
|
}
|
|
54
|
-
getSinglePresignedUrl(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
getSinglePresignedUrl(operation, objectKey) {
|
|
65
|
+
if (this.cloudfrontSigner) {
|
|
66
|
+
return this.cloudfrontSigner.getSignedUrl({
|
|
67
|
+
url: `${this.config.CLOUD_FRONT_PUBLIC_DOMAIN}/${objectKey}`,
|
|
68
|
+
expires: Date.now() + this.config.PRESIGNED_URL_EXPIRATION_S * 1000,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return this.s3Client.getSignedUrl(operation, {
|
|
72
|
+
Bucket: this.config.S3_BUCKET_NAME,
|
|
73
|
+
Key: objectKey,
|
|
74
|
+
Expires: this.config.PRESIGNED_URL_EXPIRATION_S
|
|
58
75
|
});
|
|
59
76
|
}
|
|
60
77
|
async copySingleObject(fromObjectKey, toObjectKey) {
|