-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathNFSAdapter.ts
89 lines (77 loc) · 2.74 KB
/
NFSAdapter.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Readable } from 'node:stream';
import { IncomingHttpHeaders } from 'node:http';
import {
SingletonProto,
AccessLevel,
Inject,
} from '@eggjs/tegg';
import { Pointcut } from '@eggjs/tegg/aop';
import { EggLogger } from 'egg';
import { AsyncTimer } from '../aop/AsyncTimer.js';
import { NFSClient } from '../typing.js';
const INSTANCE_NAME = 'nfsAdapter';
@SingletonProto({
name: INSTANCE_NAME,
accessLevel: AccessLevel.PUBLIC,
})
export class NFSAdapter {
@Inject()
private readonly nfsClient: NFSClient;
@Inject()
private readonly logger: EggLogger;
@Pointcut(AsyncTimer)
async uploadBytes(storeKey: string, bytes: Uint8Array) {
this.logger.info('[%s:uploadBytes] key: %s, bytes: %d', INSTANCE_NAME, storeKey, bytes.length);
await this.nfsClient.uploadBytes(bytes, { key: storeKey });
}
// will return next store position
@Pointcut(AsyncTimer)
async appendBytes(storeKey: string, bytes: Uint8Array, position?: string, headers?: IncomingHttpHeaders) {
// make sure position is undefined by the first time
if (!position) position = undefined;
const options = {
key: storeKey,
position,
headers,
};
const result = await this.nfsClient.appendBytes(bytes, options);
if (result?.nextAppendPosition) return String(result.nextAppendPosition);
}
@Pointcut(AsyncTimer)
async uploadFile(storeKey: string, file: string) {
this.logger.info('[%s:uploadFile] key: %s, file: %s', INSTANCE_NAME, storeKey, file);
await this.nfsClient.upload(file, { key: storeKey });
}
@Pointcut(AsyncTimer)
async downloadFile(storeKey: string, file: string, timeout: number) {
this.logger.info('[%s:downloadFile] key: %s, file: %s, timeout: %s',
INSTANCE_NAME, storeKey, file, timeout);
await this.nfsClient.download(storeKey, file, { timeout });
}
@Pointcut(AsyncTimer)
async remove(storeKey: string) {
this.logger.info('[%s:remove] key: %s', INSTANCE_NAME, storeKey);
await this.nfsClient.remove(storeKey);
}
@Pointcut(AsyncTimer)
async getStream(storeKey: string): Promise<Readable | undefined> {
return await this.nfsClient.createDownloadStream(storeKey);
}
@Pointcut(AsyncTimer)
async getBytes(storeKey: string): Promise<Uint8Array | undefined> {
return await this.nfsClient.readBytes(storeKey);
}
@Pointcut(AsyncTimer)
async getDownloadUrl(storeKey: string): Promise<string | undefined> {
if (typeof this.nfsClient.url === 'function') {
return this.nfsClient.url(storeKey) as string;
}
}
async getDownloadUrlOrStream(storeKey: string): Promise<string | Readable | undefined> {
const downloadUrl = await this.getDownloadUrl(storeKey);
if (downloadUrl) {
return downloadUrl;
}
return await this.getStream(storeKey);
}
}