@nathapp/nestjs-storage 1.0.0-rc.1
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/.eslintrc.js +24 -0
- package/LICENSE +21 -0
- package/README.md +4 -0
- package/bun.lockb +0 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -0
- package/dist/module/index.d.ts +1 -0
- package/dist/module/index.js +17 -0
- package/dist/module/storage.module.d.ts +9 -0
- package/dist/module/storage.module.js +62 -0
- package/dist/providers/base-storage.d.ts +23 -0
- package/dist/providers/base-storage.js +25 -0
- package/dist/providers/dropbox-storage.d.ts +23 -0
- package/dist/providers/dropbox-storage.js +256 -0
- package/dist/providers/gdrive-storage.d.ts +20 -0
- package/dist/providers/gdrive-storage.js +238 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +24 -0
- package/dist/providers/local-storage.d.ts +18 -0
- package/dist/providers/local-storage.js +147 -0
- package/dist/providers/nfs-storage.d.ts +18 -0
- package/dist/providers/nfs-storage.js +47 -0
- package/dist/providers/oss-storage.d.ts +19 -0
- package/dist/providers/oss-storage.js +96 -0
- package/dist/providers/provider-factory.d.ts +5 -0
- package/dist/providers/provider-factory.js +40 -0
- package/dist/providers/s3-base-storage.d.ts +26 -0
- package/dist/providers/s3-base-storage.js +162 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +17 -0
- package/dist/service/storage-service.d.ts +17 -0
- package/dist/service/storage-service.js +60 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +18 -0
- package/dist/types/options.d.ts +77 -0
- package/dist/types/options.js +13 -0
- package/dist/types/storage.d.ts +18 -0
- package/dist/types/storage.js +2 -0
- package/dist/util/getMimetypes.d.ts +1 -0
- package/dist/util/getMimetypes.js +63 -0
- package/dist/util/index.d.ts +2 -0
- package/dist/util/index.js +18 -0
- package/dist/util/makeRequest.d.ts +17 -0
- package/dist/util/makeRequest.js +70 -0
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/package.json +76 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
parserOptions: {
|
|
4
|
+
project: 'tsconfig.json',
|
|
5
|
+
sourceType: 'module',
|
|
6
|
+
},
|
|
7
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
|
8
|
+
extends: [
|
|
9
|
+
'plugin:@typescript-eslint/recommended',
|
|
10
|
+
'plugin:prettier/recommended',
|
|
11
|
+
],
|
|
12
|
+
root: true,
|
|
13
|
+
env: {
|
|
14
|
+
node: true,
|
|
15
|
+
jest: true,
|
|
16
|
+
},
|
|
17
|
+
ignorePatterns: ['.eslintrc.js'],
|
|
18
|
+
rules: {
|
|
19
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
|
20
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
21
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
22
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
23
|
+
},
|
|
24
|
+
};
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 KnowYourself
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/bun.lockb
ADDED
|
Binary file
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./module"), exports);
|
|
18
|
+
__exportStar(require("./providers"), exports);
|
|
19
|
+
__exportStar(require("./service"), exports);
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './storage.module';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./storage.module"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { StorageOption, AsyncStorageOptions } from '../types/options';
|
|
2
|
+
import { DynamicModule } from '@nestjs/common';
|
|
3
|
+
export declare class StorageModule {
|
|
4
|
+
private static providerFactory;
|
|
5
|
+
private static createService;
|
|
6
|
+
static forRoot(options: StorageOption): DynamicModule;
|
|
7
|
+
static forRootAsync(options: AsyncStorageOptions): Promise<DynamicModule>;
|
|
8
|
+
private static createAsyncProviders;
|
|
9
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var StorageModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.StorageModule = void 0;
|
|
11
|
+
const providers_1 = require("../providers");
|
|
12
|
+
const storage_service_1 = require("../service/storage-service");
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
let StorageModule = StorageModule_1 = class StorageModule {
|
|
15
|
+
static providerFactory(options) {
|
|
16
|
+
return providers_1.ProviderFactory.createProvider(options);
|
|
17
|
+
}
|
|
18
|
+
static createService(options) {
|
|
19
|
+
const provider = this.providerFactory(options);
|
|
20
|
+
const nestProvider = {
|
|
21
|
+
provide: storage_service_1.StorageService,
|
|
22
|
+
useValue: new storage_service_1.StorageService(provider),
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
providers: [nestProvider],
|
|
26
|
+
exports: [nestProvider],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
static forRoot(options) {
|
|
30
|
+
return {
|
|
31
|
+
global: true,
|
|
32
|
+
module: StorageModule_1,
|
|
33
|
+
...this.createService(options),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
static async forRootAsync(options) {
|
|
37
|
+
return {
|
|
38
|
+
global: options.isGlobal,
|
|
39
|
+
module: StorageModule_1,
|
|
40
|
+
imports: options.imports || [],
|
|
41
|
+
...this.createAsyncProviders(options),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
static createAsyncProviders(options) {
|
|
45
|
+
const provider = {
|
|
46
|
+
provide: storage_service_1.StorageService,
|
|
47
|
+
useFactory: async (...args) => {
|
|
48
|
+
const storageOption = await options.useFactory(...args);
|
|
49
|
+
return new storage_service_1.StorageService(this.providerFactory(storageOption));
|
|
50
|
+
},
|
|
51
|
+
inject: options.inject || [],
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
providers: [provider],
|
|
55
|
+
exports: [provider],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.StorageModule = StorageModule;
|
|
60
|
+
exports.StorageModule = StorageModule = StorageModule_1 = __decorate([
|
|
61
|
+
(0, common_1.Module)({})
|
|
62
|
+
], StorageModule);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Provider } from '../types/options';
|
|
2
|
+
import { Header206, IFile, PartialStream } from '../types/storage';
|
|
3
|
+
export declare abstract class BaseStorage {
|
|
4
|
+
protected readonly provider: Provider;
|
|
5
|
+
protected constructor(provider: Provider);
|
|
6
|
+
get storageProvider(): Provider;
|
|
7
|
+
abstract putFile(path: string, data: Buffer): Promise<IFile>;
|
|
8
|
+
abstract getFileOrFolder(fileId: string): Promise<IFile>;
|
|
9
|
+
abstract readFile(fileId: string): Promise<NodeJS.ReadableStream>;
|
|
10
|
+
abstract deleteFileOrFolder(fileId: string): Promise<boolean>;
|
|
11
|
+
abstract moveFileOrFolder(fileId: string, newPath: string): Promise<IFile>;
|
|
12
|
+
abstract renameFileOrFolder(fileId: string, newName: string): Promise<IFile>;
|
|
13
|
+
abstract createFolder(path: string): Promise<IFile>;
|
|
14
|
+
abstract readFolder(folderId: string): Promise<IFile[]>;
|
|
15
|
+
abstract getObjectUrl(fileId: string): Promise<string>;
|
|
16
|
+
abstract getSignedUrl(fileId: string, expires?: number): Promise<string>;
|
|
17
|
+
abstract streamFile(fileId: string, range: string): Promise<PartialStream>;
|
|
18
|
+
protected buildRange(range: string, file: IFile): {
|
|
19
|
+
start: number;
|
|
20
|
+
end: number;
|
|
21
|
+
headers: Header206;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseStorage = void 0;
|
|
4
|
+
class BaseStorage {
|
|
5
|
+
constructor(provider) {
|
|
6
|
+
this.provider = provider;
|
|
7
|
+
}
|
|
8
|
+
get storageProvider() {
|
|
9
|
+
return this.provider;
|
|
10
|
+
}
|
|
11
|
+
buildRange(range, file) {
|
|
12
|
+
const parts = range.replace(/bytes=/, '').split('-');
|
|
13
|
+
const start = parseInt(parts[0], 10);
|
|
14
|
+
const end = parseInt(parts[1]) > 0 ? parseInt(parts[1], 10) : file.size - 1;
|
|
15
|
+
const chunkSize = end - start + 1;
|
|
16
|
+
const headers = {
|
|
17
|
+
contentType: file.mimeType || '',
|
|
18
|
+
contentDisposition: 'attachment',
|
|
19
|
+
contentLength: chunkSize.toString(),
|
|
20
|
+
contentRange: `bytes ${start}-${end}/${file.size}`,
|
|
21
|
+
};
|
|
22
|
+
return { start, end, headers };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.BaseStorage = BaseStorage;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BaseStorage } from './base-storage';
|
|
2
|
+
import { IFile, PartialStream } from '../types/storage';
|
|
3
|
+
import { DropboxStorageOption } from '../types/options';
|
|
4
|
+
export declare class DropboxStorage extends BaseStorage {
|
|
5
|
+
private readonly credentials;
|
|
6
|
+
private readonly refreshToken;
|
|
7
|
+
private token;
|
|
8
|
+
constructor(options: DropboxStorageOption);
|
|
9
|
+
createFolder(path: string): Promise<IFile>;
|
|
10
|
+
deleteFileOrFolder(fileId: string): Promise<boolean>;
|
|
11
|
+
getFileOrFolder(fileId: string): Promise<IFile>;
|
|
12
|
+
moveFileOrFolder(fileId: string, newPath: string): Promise<IFile>;
|
|
13
|
+
putFile(path: string, data: Buffer): Promise<IFile>;
|
|
14
|
+
readFile(fileId: string): Promise<NodeJS.ReadableStream>;
|
|
15
|
+
readFolder(folderId: string): Promise<IFile[]>;
|
|
16
|
+
renameFileOrFolder(fileId: string, newName: string): Promise<IFile>;
|
|
17
|
+
getObjectUrl(fileId: string): Promise<string>;
|
|
18
|
+
getSignedUrl(fileId: string): Promise<string>;
|
|
19
|
+
streamFile(fileId: string, range: string): Promise<PartialStream>;
|
|
20
|
+
private parseFile;
|
|
21
|
+
private makeRequest;
|
|
22
|
+
private authenticate;
|
|
23
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DropboxStorage = void 0;
|
|
4
|
+
const base_storage_1 = require("./base-storage");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const util_1 = require("../util");
|
|
7
|
+
const dropboxFileSchema = zod_1.z.object({
|
|
8
|
+
'.tag': zod_1.z.union([zod_1.z.literal('file'), zod_1.z.literal('folder')]),
|
|
9
|
+
name: zod_1.z.string(),
|
|
10
|
+
id: zod_1.z.string(),
|
|
11
|
+
path_lower: zod_1.z.string(),
|
|
12
|
+
path_display: zod_1.z.string(),
|
|
13
|
+
client_modified: zod_1.z.string(),
|
|
14
|
+
size: zod_1.z.number().optional(),
|
|
15
|
+
});
|
|
16
|
+
const dropboxGetFilesResponseSchema = zod_1.z.object({
|
|
17
|
+
entries: zod_1.z.array(dropboxFileSchema),
|
|
18
|
+
has_more: zod_1.z.boolean(),
|
|
19
|
+
cursor: zod_1.z.string(),
|
|
20
|
+
});
|
|
21
|
+
const dropboxTokenSchema = zod_1.z.object({
|
|
22
|
+
access_token: zod_1.z.string(),
|
|
23
|
+
expires_in: zod_1.z.number(),
|
|
24
|
+
refresh_token: zod_1.z.string(),
|
|
25
|
+
});
|
|
26
|
+
class DropboxStorage extends base_storage_1.BaseStorage {
|
|
27
|
+
constructor(options) {
|
|
28
|
+
super(options.provider);
|
|
29
|
+
this.credentials = {
|
|
30
|
+
clientId: options.options.clientId,
|
|
31
|
+
clientSecret: options.options.clientSecret,
|
|
32
|
+
};
|
|
33
|
+
this.refreshToken = options.options.refreshToken;
|
|
34
|
+
}
|
|
35
|
+
async createFolder(path) {
|
|
36
|
+
const params = {
|
|
37
|
+
path: 'https://api.dropboxapi.com/2/files/create_folder_v2',
|
|
38
|
+
query: {
|
|
39
|
+
path: `${path}`,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const data = await this.makeRequest(params, dropboxFileSchema);
|
|
43
|
+
return this.parseFile(data);
|
|
44
|
+
}
|
|
45
|
+
async deleteFileOrFolder(fileId) {
|
|
46
|
+
const params = {
|
|
47
|
+
path: 'https://api.dropboxapi.com/2/files/delete_v2',
|
|
48
|
+
query: {
|
|
49
|
+
path: `${fileId}`,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
await this.makeRequest(params, dropboxFileSchema);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
async getFileOrFolder(fileId) {
|
|
56
|
+
const params = {
|
|
57
|
+
path: 'https://api.dropboxapi.com/2/files/get_metadata',
|
|
58
|
+
query: {
|
|
59
|
+
path: `${fileId}`,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const data = await this.makeRequest(params, dropboxFileSchema);
|
|
63
|
+
return this.parseFile(data);
|
|
64
|
+
}
|
|
65
|
+
async moveFileOrFolder(fileId, newPath) {
|
|
66
|
+
const params = {
|
|
67
|
+
path: 'https://api.dropboxapi.com/2/files/move_v2',
|
|
68
|
+
query: {
|
|
69
|
+
from_path: fileId,
|
|
70
|
+
to_path: newPath,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
const data = await this.makeRequest(params, dropboxFileSchema);
|
|
74
|
+
return this.parseFile(data);
|
|
75
|
+
}
|
|
76
|
+
async putFile(path, data) {
|
|
77
|
+
const token = await this.authenticate();
|
|
78
|
+
const response = await fetch('https://content.dropboxapi.com/2/files/upload', {
|
|
79
|
+
method: 'POST',
|
|
80
|
+
headers: {
|
|
81
|
+
Authorization: `Bearer ${token.access_token}`,
|
|
82
|
+
'Content-Type': 'application/octet-stream',
|
|
83
|
+
'Dropbox-API-Arg': JSON.stringify({
|
|
84
|
+
path,
|
|
85
|
+
mode: 'overwrite',
|
|
86
|
+
}),
|
|
87
|
+
},
|
|
88
|
+
body: data,
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
const json = await response.json();
|
|
92
|
+
const dropboxFile = dropboxFileSchema.parse(json);
|
|
93
|
+
return this.parseFile(dropboxFile);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
throw new Error('Failed to upload file to Dropbox');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async readFile(fileId) {
|
|
100
|
+
const token = await this.authenticate();
|
|
101
|
+
const options = {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: {
|
|
104
|
+
authorization: `Bearer ${token.access_token}`,
|
|
105
|
+
'Dropbox-API-Arg': JSON.stringify({
|
|
106
|
+
path: fileId,
|
|
107
|
+
}),
|
|
108
|
+
'Content-Type': 'application/octet-stream',
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
fetch('https://content.dropboxapi.com/2/files/download', options)
|
|
113
|
+
.then((response) => {
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
reject(response);
|
|
116
|
+
}
|
|
117
|
+
resolve(response.body);
|
|
118
|
+
})
|
|
119
|
+
.catch((error) => {
|
|
120
|
+
reject(error);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async readFolder(folderId) {
|
|
125
|
+
const query = {
|
|
126
|
+
path: folderId,
|
|
127
|
+
include_media_info: true,
|
|
128
|
+
include_deleted: false,
|
|
129
|
+
include_has_explicit_shared_members: false,
|
|
130
|
+
include_mounted_folders: true,
|
|
131
|
+
include_non_downloadable_files: true,
|
|
132
|
+
};
|
|
133
|
+
const params = {
|
|
134
|
+
path: 'https://api.dropboxapi.com/2/files/list_folder',
|
|
135
|
+
query,
|
|
136
|
+
};
|
|
137
|
+
const data = await this.makeRequest(params, dropboxGetFilesResponseSchema);
|
|
138
|
+
return data.entries.map((file) => this.parseFile(file));
|
|
139
|
+
}
|
|
140
|
+
renameFileOrFolder(fileId, newName) {
|
|
141
|
+
return this.moveFileOrFolder(fileId, newName);
|
|
142
|
+
}
|
|
143
|
+
getObjectUrl(fileId) {
|
|
144
|
+
return this.getSignedUrl(fileId);
|
|
145
|
+
}
|
|
146
|
+
getSignedUrl(fileId) {
|
|
147
|
+
const params = {
|
|
148
|
+
path: 'https://api.dropboxapi.com/2/files/get_temporary_link',
|
|
149
|
+
query: {
|
|
150
|
+
path: fileId,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
this.makeRequest(params, zod_1.z.object({
|
|
155
|
+
link: zod_1.z.string(),
|
|
156
|
+
metadata: dropboxFileSchema,
|
|
157
|
+
}))
|
|
158
|
+
.then((data) => {
|
|
159
|
+
resolve(data.link);
|
|
160
|
+
})
|
|
161
|
+
.catch((error) => {
|
|
162
|
+
reject(error);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async streamFile(fileId, range) {
|
|
167
|
+
const token = await this.authenticate();
|
|
168
|
+
const options = {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
headers: {
|
|
171
|
+
authorization: `Bearer ${token.access_token}`,
|
|
172
|
+
'Dropbox-API-Arg': JSON.stringify({
|
|
173
|
+
path: fileId,
|
|
174
|
+
}),
|
|
175
|
+
'Content-Type': 'application/octet-stream',
|
|
176
|
+
Range: range,
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
fetch('https://content.dropboxapi.com/2/files/download', options)
|
|
181
|
+
.then((response) => {
|
|
182
|
+
if (!response.ok) {
|
|
183
|
+
reject(response);
|
|
184
|
+
}
|
|
185
|
+
resolve({
|
|
186
|
+
stream: response.body,
|
|
187
|
+
headers: {
|
|
188
|
+
contentType: response.headers.get('Content-Type') || '',
|
|
189
|
+
contentLength: response.headers.get('Content-Length') || '',
|
|
190
|
+
contentRange: response.headers.get('Content-Range') || '',
|
|
191
|
+
contentDisposition: response.headers.get('Content-Disposition') || '',
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
})
|
|
195
|
+
.catch((error) => {
|
|
196
|
+
reject(error);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
parseFile(file) {
|
|
201
|
+
return {
|
|
202
|
+
name: file.name,
|
|
203
|
+
path: file.path_lower,
|
|
204
|
+
size: file.size || 0,
|
|
205
|
+
mimeType: (0, util_1.getMimeType)(file.name),
|
|
206
|
+
isFolder: file['.tag'] === 'folder',
|
|
207
|
+
modifiedAt: new Date(file.client_modified),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
makeRequest({ path, query }, schema) {
|
|
211
|
+
return new Promise((resolve, reject) => {
|
|
212
|
+
this.authenticate()
|
|
213
|
+
.then((token) => {
|
|
214
|
+
const params = {
|
|
215
|
+
address: `https://api.dropboxapi.com/2${path}`,
|
|
216
|
+
method: 'POST',
|
|
217
|
+
headers: {
|
|
218
|
+
Authorization: `Bearer ${token.access_token}`,
|
|
219
|
+
'Content-Type': 'application/json',
|
|
220
|
+
},
|
|
221
|
+
body: JSON.stringify(query),
|
|
222
|
+
};
|
|
223
|
+
return (0, util_1.makeRequest)(params, schema);
|
|
224
|
+
})
|
|
225
|
+
.then((data) => {
|
|
226
|
+
resolve(data);
|
|
227
|
+
})
|
|
228
|
+
.catch((error) => {
|
|
229
|
+
reject(error);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
async authenticate() {
|
|
234
|
+
if (this.token && this.token.expires_in > Date.now()) {
|
|
235
|
+
return this.token;
|
|
236
|
+
}
|
|
237
|
+
const query = {
|
|
238
|
+
grant_type: 'refresh_token',
|
|
239
|
+
refresh_token: this.refreshToken,
|
|
240
|
+
client_id: this.credentials.clientId,
|
|
241
|
+
client_secret: this.credentials.clientSecret,
|
|
242
|
+
};
|
|
243
|
+
const response = await (0, util_1.makeRequest)({
|
|
244
|
+
address: 'https://api.dropboxapi.com/oauth2/token',
|
|
245
|
+
method: 'GET',
|
|
246
|
+
query,
|
|
247
|
+
}, dropboxTokenSchema);
|
|
248
|
+
const token = {
|
|
249
|
+
...response,
|
|
250
|
+
expires_in: Date.now() + response.expires_in * 1000,
|
|
251
|
+
};
|
|
252
|
+
this.token = token;
|
|
253
|
+
return token;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
exports.DropboxStorage = DropboxStorage;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BaseStorage } from './base-storage';
|
|
2
|
+
import { GDriveStorageOption } from '../types/options';
|
|
3
|
+
import { IFile, PartialStream } from '../types/storage';
|
|
4
|
+
export declare class GDriveStorage extends BaseStorage {
|
|
5
|
+
private readonly drive;
|
|
6
|
+
constructor(options: GDriveStorageOption);
|
|
7
|
+
createFolder(path: string): Promise<IFile>;
|
|
8
|
+
deleteFileOrFolder(fileId: string): Promise<boolean>;
|
|
9
|
+
getFileOrFolder(fileId: string): Promise<IFile>;
|
|
10
|
+
moveFileOrFolder(fileId: string, newPath: string): Promise<IFile>;
|
|
11
|
+
putFile(path: string, data: Buffer): Promise<IFile>;
|
|
12
|
+
readFile(fileId: string): Promise<NodeJS.ReadableStream>;
|
|
13
|
+
readFolder(folderId: string): Promise<IFile[]>;
|
|
14
|
+
renameFileOrFolder(fileId: string, newName: string): Promise<IFile>;
|
|
15
|
+
getObjectUrl(fileId: string): Promise<string>;
|
|
16
|
+
getSignedUrl(fileId: string): Promise<string>;
|
|
17
|
+
streamFile(fileId: string, range: string): Promise<PartialStream>;
|
|
18
|
+
private parseFile;
|
|
19
|
+
private readFolderRecursive;
|
|
20
|
+
}
|