dl-commons 0.0.2-testing.19 → 0.0.2-testing.20
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/index.d.ts +2 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/models/interfaces/dl-storage.interface.d.ts +35 -0
- package/dist/models/interfaces/dl-storage.interface.js +7 -0
- package/dist/models/interfaces/dl-storage.interface.js.map +1 -0
- package/dist/models/interfaces/s3.interface.d.ts +1 -1
- package/dist/services/storage.service.d.ts +18 -0
- package/dist/services/storage.service.js +81 -0
- package/dist/services/storage.service.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/models/interfaces/dl-storage.interface.ts +41 -0
- package/src/models/interfaces/s3.interface.ts +1 -1
- package/src/services/storage.service.ts +84 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -111,6 +111,7 @@ import { IPicture } from './models/interfaces/picture.interface';
|
|
|
111
111
|
import { IAuthor } from './models/interfaces/author.interface';
|
|
112
112
|
import { Author, AuthorDocument, AuthorSchema } from './models/schemas/embedded/author.schema';
|
|
113
113
|
import { CropAreaDTO } from './models/dtos/commons/crop-area.dto';
|
|
114
|
+
import { StorageService } from './services/storage.service';
|
|
114
115
|
|
|
115
116
|
export {
|
|
116
117
|
/**
|
|
@@ -119,6 +120,7 @@ export {
|
|
|
119
120
|
SessionsService,
|
|
120
121
|
DictionariesService,
|
|
121
122
|
SMTPService,
|
|
123
|
+
StorageService,
|
|
122
124
|
|
|
123
125
|
/**
|
|
124
126
|
* Rest Wrapper
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
|
|
3
|
+
export interface IRecord {
|
|
4
|
+
createdDate: any;
|
|
5
|
+
modifiedDate: any;
|
|
6
|
+
state: string;
|
|
7
|
+
deleted: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IMeta {
|
|
11
|
+
name: string;
|
|
12
|
+
size: number;
|
|
13
|
+
mimeType: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IS3 {
|
|
17
|
+
bucket: string;
|
|
18
|
+
etag: string;
|
|
19
|
+
key: string;
|
|
20
|
+
secret: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ICommit {
|
|
24
|
+
value: boolean;
|
|
25
|
+
date: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class FileInfo {
|
|
29
|
+
id: string;
|
|
30
|
+
record: IRecord;
|
|
31
|
+
meta: IMeta;
|
|
32
|
+
s3: IS3;
|
|
33
|
+
commit: ICommit;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface IFileRequest {
|
|
37
|
+
file: Readable;
|
|
38
|
+
mimeType: string;
|
|
39
|
+
fileName: string;
|
|
40
|
+
size?: number;
|
|
41
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import * as FormData from 'form-data';
|
|
3
|
+
import { ConfigService } from '@nestjs/config';
|
|
4
|
+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
5
|
+
import internal from 'stream';
|
|
6
|
+
import { RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
|
|
7
|
+
import { IFileRequest } from '../models/interfaces/dl-storage.interface';
|
|
8
|
+
import { IS3File } from '../models/interfaces/s3.interface';
|
|
9
|
+
|
|
10
|
+
@Injectable()
|
|
11
|
+
export class StorageService {
|
|
12
|
+
//#region Private Members
|
|
13
|
+
private readonly _storageApiUrl: string;
|
|
14
|
+
private readonly _accessToken: string;
|
|
15
|
+
private readonly _bucketName: string;
|
|
16
|
+
//#endregion
|
|
17
|
+
|
|
18
|
+
//#region Constructor
|
|
19
|
+
constructor(private readonly _configsService: ConfigService) {
|
|
20
|
+
this._storageApiUrl = this._configsService.getOrThrow('DL_STORAGE__REST__BASE_URL');
|
|
21
|
+
this._accessToken = this._configsService.getOrThrow('DL_STORAGE__REST__ACCESS_TOKEN');
|
|
22
|
+
this._bucketName = this._configsService.getOrThrow('DL_STORAGE__REST__BUCKET_NAME');
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
|
|
26
|
+
//#region Public API
|
|
27
|
+
public async upload(body: IFileRequest, autoCommit: boolean) {
|
|
28
|
+
const form = new FormData();
|
|
29
|
+
form.append('file', body.file, {
|
|
30
|
+
filename: body.fileName,
|
|
31
|
+
contentType: body.mimeType,
|
|
32
|
+
knownLength: body.size,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
let url = `${this._storageApiUrl}/bucket/${this._bucketName}/upload`;
|
|
36
|
+
if (autoCommit) {
|
|
37
|
+
url += '?autoCommit=true';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const response = await axios.postForm(url, form, this._axiosStandardConfig);
|
|
41
|
+
return this._handleAxiosResponse(response, 'upload_failed');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public async download(fileId: string): Promise<AxiosResponse<internal.PassThrough>> {
|
|
45
|
+
try {
|
|
46
|
+
return axios.get<internal.PassThrough>(`${this._storageApiUrl}/bucket/${this._bucketName}/file/${fileId}/download`, this._axiosStreamConfig);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new RESTError('storage_error', error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async getMeta(fileId: string, bucketName: string): Promise<IS3File> {
|
|
53
|
+
const response = await axios.get(`${this._storageApiUrl}/bucket/${bucketName}/file/${fileId}/meta`, this._axiosStandardConfig);
|
|
54
|
+
return this._handleAxiosResponse(response, 'get_meta_failed');
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
|
|
58
|
+
//#region Private API
|
|
59
|
+
private _handleAxiosResponse(response: AxiosResponse, errorKeyword: string): IS3File {
|
|
60
|
+
if (response?.data && response.data?.result && response.data.result?.data) {
|
|
61
|
+
return response.data.result.data;
|
|
62
|
+
} else {
|
|
63
|
+
throw { error: { keyword: errorKeyword, debug: response.data } };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private get _axiosStandardConfig(): AxiosRequestConfig {
|
|
68
|
+
return {
|
|
69
|
+
headers: {
|
|
70
|
+
'X-Access-Token': this._accessToken,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private get _axiosStreamConfig(): AxiosRequestConfig {
|
|
76
|
+
return {
|
|
77
|
+
responseType: 'stream',
|
|
78
|
+
headers: {
|
|
79
|
+
'X-Access-Token': this._accessToken,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
}
|