dfs-adapter 0.0.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/README.md ADDED
@@ -0,0 +1 @@
1
+ ###
@@ -0,0 +1,76 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export declare interface OssConfig {
5
+ accessKeyId: string;
6
+ accessKeySecret: string;
7
+ stsToken?: string | undefined;
8
+ bucket?: string | undefined;
9
+ endpoint?: string | undefined;
10
+ region?: string | undefined;
11
+ internal?: boolean | undefined;
12
+ secure?: boolean | undefined;
13
+ timeout?: string | number | undefined;
14
+ cname?: boolean | undefined;
15
+ [key: string]: any;
16
+ access: string;
17
+ }
18
+ export declare interface MinioConfig {
19
+ endPoint: string;
20
+ port: number;
21
+ accessKey: string;
22
+ secretKey: string;
23
+ bucketName: string;
24
+ [key: string]: any;
25
+ access: string;
26
+ }
27
+ export declare interface HttpConfig {
28
+ url: string;
29
+ headers: {
30
+ Authorization: string;
31
+ [key: string]: any;
32
+ };
33
+ body: {
34
+ [key: string]: any;
35
+ };
36
+ access: string;
37
+ }
38
+ export declare interface NativeConfig {
39
+ storage: string;
40
+ access: string;
41
+ }
42
+ export declare interface FastdfsConfig {
43
+ domain: string;
44
+ group: string;
45
+ token: string;
46
+ access: string;
47
+ }
48
+ export declare type ConfigOptionsMap = {
49
+ oss: OssConfig;
50
+ minio: MinioConfig;
51
+ http: HttpConfig;
52
+ native: NativeConfig;
53
+ fastdfs: FastdfsConfig;
54
+ };
55
+ export declare type ConfigOptionsKey = keyof ConfigOptionsMap;
56
+ type ConfigOptionsOptions<T> = T extends ConfigOptionsKey ? {
57
+ type: T;
58
+ } & ConfigOptionsMap[T] : never;
59
+ export type DfsConfig = ConfigOptionsOptions<ConfigOptionsKey>;
60
+ export declare const defineDfsConfig: (config: DfsConfig) => DfsConfig;
61
+ export declare class Dfs<T extends ConfigOptionsKey> {
62
+ protected readonly context: VWebApplicationContext;
63
+ protected readonly options: ConfigOptionsOptions<T>;
64
+ protected readonly logger: Logger;
65
+ private readonly timeMap;
66
+ constructor(options: ConfigOptionsOptions<T>, context: VWebApplicationContext);
67
+ protected getTmpFile(file: string): string;
68
+ protected timeBegin(label: string): void;
69
+ protected timeEnd(label: string): void;
70
+ protected stream2buffer(stream: NodeJS.ReadableStream): Promise<Buffer>;
71
+ startup(): Promise<void>;
72
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
73
+ download(path: string, ...args: any[]): Promise<Buffer>;
74
+ getAccess(path: string): Promise<string>;
75
+ }
76
+ export {};
package/lib/declare.js ADDED
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Dfs = exports.defineDfsConfig = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const defineDfsConfig = (config) => config;
19
+ exports.defineDfsConfig = defineDfsConfig;
20
+ class Dfs {
21
+ constructor(options, context) {
22
+ this.options = options;
23
+ this.context = context;
24
+ this.logger = context.log4j.getLogger('Dfs');
25
+ this.timeMap = new Map();
26
+ }
27
+ getTmpFile(file) {
28
+ let base = path_1.default.resolve('.');
29
+ let tmpDir = base + '/tmp';
30
+ if (!fs_1.default.existsSync(tmpDir)) {
31
+ fs_1.default.mkdirSync(tmpDir);
32
+ }
33
+ return tmpDir + `/${file}`;
34
+ }
35
+ timeBegin(label) {
36
+ this.timeMap.set(label, Date.now());
37
+ }
38
+ timeEnd(label) {
39
+ const costTime = Date.now() - this.timeMap.get(label);
40
+ this.logger.info(`${label} cost [${costTime}ms]`);
41
+ }
42
+ stream2buffer(stream) {
43
+ return new Promise((resolve, reject) => {
44
+ const buffers = [];
45
+ stream.on('data', chunk => {
46
+ buffers.push(chunk);
47
+ });
48
+ stream.on('end', () => {
49
+ return resolve(Buffer.concat(buffers));
50
+ });
51
+ stream.on('error', reject);
52
+ stream.read();
53
+ });
54
+ }
55
+ startup() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ if (this.logger.isDebugEnabled()) {
58
+ this.logger.info(`${this.constructor.name} is startup`);
59
+ }
60
+ });
61
+ }
62
+ upload(path, buffer, ...args) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ if (this.logger.isDebugEnabled()) {
65
+ this.logger.info(`${this.constructor.name} upload ${path}`);
66
+ }
67
+ return path;
68
+ });
69
+ }
70
+ download(path, ...args) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ if (this.logger.isDebugEnabled()) {
73
+ this.logger.info(`${this.constructor.name} download ${path}`);
74
+ }
75
+ return null;
76
+ });
77
+ }
78
+ getAccess(path) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ if (this.logger.isDebugEnabled()) {
81
+ this.logger.info(`${this.constructor.name} access ${path}`);
82
+ }
83
+ return '';
84
+ });
85
+ }
86
+ }
87
+ exports.Dfs = Dfs;
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { Dfs, FastdfsConfig } from "./declare";
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export default class HttpDfs extends Dfs<'fastdfs'> {
5
+ private client;
6
+ constructor(options: FastdfsConfig & {
7
+ type: 'fastdfs';
8
+ }, context: VWebApplicationContext);
9
+ startup(): Promise<void>;
10
+ upload(path: string, buffer: any): Promise<string>;
11
+ download(path: string, ...args: any[]): Promise<Buffer>;
12
+ getAccess(path: string): Promise<string>;
13
+ }
package/lib/fastdfs.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const declare_1 = require("./declare");
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const vweb_core_1 = require("vweb-core");
18
+ const form_data_1 = __importDefault(require("form-data"));
19
+ const fs_1 = __importDefault(require("fs"));
20
+ const path_1 = __importDefault(require("path"));
21
+ class HttpDfs extends declare_1.Dfs {
22
+ constructor(options, context) {
23
+ super(Object.assign({ group: 'group1' }, options), context);
24
+ this.client = axios_1.default.create();
25
+ }
26
+ startup() {
27
+ const _super = Object.create(null, {
28
+ startup: { get: () => super.startup }
29
+ });
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ return _super.startup.call(this);
32
+ });
33
+ }
34
+ upload(path, buffer) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ try {
37
+ this.timeBegin(`upload ${path}`);
38
+ let { name, dir, ext } = path_1.default.parse(path);
39
+ let tmpFile = this.getTmpFile(`${vweb_core_1.util.snowflake.nextId()}${ext}`);
40
+ fs_1.default.writeFileSync(tmpFile, buffer);
41
+ const { domain, group, token } = this.options;
42
+ let form = new form_data_1.default();
43
+ form.append('file', fs_1.default.createReadStream(tmpFile));
44
+ form.append('filename', `${name}${ext}`);
45
+ if (token) {
46
+ form.append('auth_token', token);
47
+ }
48
+ form.append('path', dir);
49
+ let headers = form.getHeaders();
50
+ return new Promise((resolve, reject) => {
51
+ form.getLength((err, length) => {
52
+ if (err) {
53
+ reject(err);
54
+ }
55
+ headers['contentLength'] = length;
56
+ axios_1.default.post(`${domain}/${group}/upload`, form, {
57
+ maxContentLength: Infinity,
58
+ maxBodyLength: Infinity,
59
+ headers
60
+ }).then(({ data }) => {
61
+ resolve(path);
62
+ }).catch(err => {
63
+ reject(err);
64
+ }).finally(() => {
65
+ if (fs_1.default.existsSync(tmpFile)) {
66
+ fs_1.default.rmSync(tmpFile);
67
+ }
68
+ });
69
+ });
70
+ });
71
+ }
72
+ finally {
73
+ this.timeEnd(`upload ${path}`);
74
+ }
75
+ });
76
+ }
77
+ download(path, ...args) {
78
+ const _super = Object.create(null, {
79
+ download: { get: () => super.download }
80
+ });
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ try {
83
+ const { domain, group } = this.options;
84
+ this.timeBegin(`download ${path}`);
85
+ const { status, data } = yield axios_1.default.get(`${domain}/${group}${path}`, { responseType: 'arraybuffer' });
86
+ yield _super.download.call(this, path, ...args);
87
+ return data;
88
+ }
89
+ catch (e) {
90
+ this.logger.error(`download ${path} error >>> ${e.message}`);
91
+ }
92
+ finally {
93
+ this.timeEnd(`download ${path}`);
94
+ }
95
+ });
96
+ }
97
+ getAccess(path) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (this.options.access) {
100
+ return `${this.options.access}/dfs/get?filename=${path}`;
101
+ }
102
+ const { domain, group } = this.options;
103
+ return `${domain}/${group}${path}`;
104
+ });
105
+ }
106
+ }
107
+ exports.default = HttpDfs;
package/lib/http.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { Dfs, HttpConfig } from "./declare";
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export default class HttpDfs extends Dfs<'http'> {
5
+ private client;
6
+ constructor(options: HttpConfig & {
7
+ type: 'http';
8
+ }, context: VWebApplicationContext);
9
+ startup(): Promise<void>;
10
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
11
+ download(path: string, ...args: any[]): Promise<Buffer>;
12
+ getAccess(path: string): Promise<string>;
13
+ }
package/lib/http.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const declare_1 = require("./declare");
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const vweb_core_1 = require("vweb-core");
18
+ const form_data_1 = __importDefault(require("form-data"));
19
+ const path_1 = __importDefault(require("path"));
20
+ const fs_1 = __importDefault(require("fs"));
21
+ class HttpDfs extends declare_1.Dfs {
22
+ constructor(options, context) {
23
+ super(options, context);
24
+ this.client = axios_1.default.create({
25
+ headers: options.headers
26
+ });
27
+ }
28
+ startup() {
29
+ const _super = Object.create(null, {
30
+ startup: { get: () => super.startup }
31
+ });
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ return _super.startup.call(this);
34
+ });
35
+ }
36
+ upload(path, buffer, ...args) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ try {
39
+ this.timeBegin(`upload ${path}`);
40
+ let { name, dir, ext } = path_1.default.parse(path);
41
+ let tmpFile = this.getTmpFile(`${vweb_core_1.util.snowflake.nextId()}${ext}`);
42
+ fs_1.default.writeFileSync(tmpFile, buffer);
43
+ let form = new form_data_1.default();
44
+ form.append('file', fs_1.default.createReadStream(tmpFile));
45
+ form.append('filename', `${name}${ext}`);
46
+ form.append('path', path);
47
+ let headers = form.getHeaders();
48
+ return new Promise((resolve, reject) => {
49
+ form.getLength((err, length) => {
50
+ if (err) {
51
+ reject(err);
52
+ }
53
+ headers['contentLength'] = length;
54
+ axios_1.default.post(this.options.url, form, {
55
+ maxContentLength: Infinity,
56
+ maxBodyLength: Infinity,
57
+ headers,
58
+ params: this.options.body
59
+ }).then(({ data }) => {
60
+ resolve(path);
61
+ }).catch(err => {
62
+ reject(err);
63
+ }).finally(() => {
64
+ if (fs_1.default.existsSync(tmpFile)) {
65
+ fs_1.default.rmSync(tmpFile);
66
+ }
67
+ });
68
+ });
69
+ });
70
+ }
71
+ finally {
72
+ this.timeEnd(`upload ${path}`);
73
+ }
74
+ });
75
+ }
76
+ download(path, ...args) {
77
+ const _super = Object.create(null, {
78
+ download: { get: () => super.download }
79
+ });
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ try {
82
+ this.timeBegin(`download ${path}`);
83
+ const { data } = yield axios_1.default.get(this.options.url, {
84
+ params: Object.assign({ filename: path }, this.options.body),
85
+ responseType: 'arraybuffer'
86
+ });
87
+ yield _super.download.call(this, path, ...args);
88
+ return data;
89
+ }
90
+ finally {
91
+ this.timeEnd(`download ${path}`);
92
+ }
93
+ });
94
+ }
95
+ getAccess(path) {
96
+ return __awaiter(this, void 0, void 0, function* () {
97
+ if (this.options.access) {
98
+ return `${this.options.access}/dfs/get?filename=${path}`;
99
+ }
100
+ return `${this.options.url}?filename=${path}`;
101
+ });
102
+ }
103
+ }
104
+ exports.default = HttpDfs;
package/lib/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { DfsConfig } from "./declare";
2
+ import Mdfs from "./mdfs";
3
+ export * from './declare';
4
+ declare global {
5
+ const dfs: Mdfs;
6
+ interface Object {
7
+ readonly dfs: Mdfs;
8
+ }
9
+ }
10
+ declare const _default: {
11
+ name: string;
12
+ launch: (config: DfsConfig) => Mdfs;
13
+ };
14
+ export default _default;
package/lib/index.js ADDED
@@ -0,0 +1,27 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ const mdfs_1 = __importDefault(require("./mdfs"));
21
+ __exportStar(require("./declare"), exports);
22
+ exports.default = {
23
+ name: 'dfs',
24
+ launch: function (config) {
25
+ return new mdfs_1.default(config, this);
26
+ }
27
+ };
package/lib/mdfs.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import { VWebApplicationContext } from "vweb-core";
3
+ import { ConfigOptionsKey, Dfs, DfsConfig } from "./declare";
4
+ export default class Mdfs extends Dfs<ConfigOptionsKey> {
5
+ private dfs;
6
+ private readonly config;
7
+ constructor(config: DfsConfig, context: VWebApplicationContext);
8
+ startup(): Promise<void>;
9
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
10
+ download(path: string, ...args: any[]): Promise<Buffer>;
11
+ getAccess(path: string): Promise<string>;
12
+ }
package/lib/mdfs.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ const vweb_core_1 = require("vweb-core");
24
+ const declare_1 = require("./declare");
25
+ class MDFSHttpListenerWrapper {
26
+ constructor() {
27
+ this.annotation = {
28
+ type: "controller",
29
+ component: true,
30
+ name: `MDFSHttpListener${vweb_core_1.util.snowflake.nextId()}Controller`,
31
+ mapping: { value: "dfs", method: "all" },
32
+ mappings: {
33
+ get: [{ value: 'get', method: "get" }]
34
+ }
35
+ };
36
+ }
37
+ get(filename, res) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ let { name, ext } = require('path').parse(filename);
40
+ res.attachment(`${name}${ext}`);
41
+ return this.dfs.download(filename);
42
+ });
43
+ }
44
+ }
45
+ class Mdfs extends declare_1.Dfs {
46
+ constructor(config, context) {
47
+ super(config, context);
48
+ this.config = config;
49
+ context.trusteeship(new MDFSHttpListenerWrapper());
50
+ const _a = this.config, { type } = _a, options = __rest(_a, ["type"]);
51
+ let Type = require(`./${type}`).default;
52
+ this.dfs = new Type(options, context);
53
+ }
54
+ startup() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ return this.dfs.startup();
57
+ });
58
+ }
59
+ upload(path, buffer, ...args) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ return this.dfs.upload(path, buffer, ...args);
62
+ });
63
+ }
64
+ download(path, ...args) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ return this.dfs.download(path, ...args);
67
+ });
68
+ }
69
+ getAccess(path) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return this.dfs.getAccess(path);
72
+ });
73
+ }
74
+ }
75
+ exports.default = Mdfs;
package/lib/minio.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { Dfs, MinioConfig } from "./declare";
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export default class MinioDfs extends Dfs<'minio'> {
5
+ private client;
6
+ constructor(options: MinioConfig & {
7
+ type: 'minio';
8
+ }, context: VWebApplicationContext);
9
+ startup(): Promise<void>;
10
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
11
+ download(path: string, ...args: any[]): Promise<Buffer>;
12
+ getAccess(path: string): Promise<string>;
13
+ }
package/lib/minio.js ADDED
@@ -0,0 +1,90 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const declare_1 = require("./declare");
36
+ const minio = __importStar(require("minio"));
37
+ class MinioDfs extends declare_1.Dfs {
38
+ constructor(options, context) {
39
+ super(Object.assign({ accessKey: '', secretKey: '' }, options), context);
40
+ this.client = new minio.Client(this.options);
41
+ }
42
+ startup() {
43
+ const _super = Object.create(null, {
44
+ startup: { get: () => super.startup }
45
+ });
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return _super.startup.call(this);
48
+ });
49
+ }
50
+ upload(path, buffer, ...args) {
51
+ const _super = Object.create(null, {
52
+ upload: { get: () => super.upload }
53
+ });
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ try {
56
+ this.timeBegin(`upload ${path}`);
57
+ this.client.putObject(this.options.bucketName, path, buffer, ...args);
58
+ return _super.upload.call(this, path, buffer, ...args);
59
+ }
60
+ finally {
61
+ this.timeEnd(`upload ${path}`);
62
+ }
63
+ });
64
+ }
65
+ download(path, ...args) {
66
+ const _super = Object.create(null, {
67
+ download: { get: () => super.download }
68
+ });
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ try {
71
+ this.timeBegin(`download ${path}`);
72
+ yield _super.download.call(this, path, ...args);
73
+ let stream = yield this.client.getObject(this.options.bucketName, path);
74
+ return yield this.stream2buffer(stream);
75
+ }
76
+ finally {
77
+ this.timeEnd(`download ${path}`);
78
+ }
79
+ });
80
+ }
81
+ getAccess(path) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ if (this.options.access) {
84
+ return `${this.options.access}/dfs/get?filename=${path}`;
85
+ }
86
+ return `http://${this.options.endPoint}:${this.options.port}/${this.options.bucketName}${path}`;
87
+ });
88
+ }
89
+ }
90
+ exports.default = MinioDfs;
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import { Dfs, NativeConfig } from "./declare";
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export default class NativeDfs extends Dfs<'native'> {
5
+ constructor(options: NativeConfig & {
6
+ type: 'native';
7
+ }, context: VWebApplicationContext);
8
+ startup(): Promise<void>;
9
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
10
+ download(path: string, ...args: any[]): Promise<Buffer>;
11
+ getAccess(path: string): Promise<string>;
12
+ }
package/lib/native.js ADDED
@@ -0,0 +1,85 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const declare_1 = require("./declare");
36
+ const fs = __importStar(require("fs"));
37
+ class NativeDfs extends declare_1.Dfs {
38
+ constructor(options, context) {
39
+ super(options, context);
40
+ }
41
+ startup() {
42
+ const _super = Object.create(null, {
43
+ startup: { get: () => super.startup }
44
+ });
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ return _super.startup.call(this);
47
+ });
48
+ }
49
+ upload(path, buffer, ...args) {
50
+ const _super = Object.create(null, {
51
+ upload: { get: () => super.upload }
52
+ });
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ try {
55
+ this.timeBegin(`upload ${path}`);
56
+ fs.writeFileSync(`${this.options.storage}${path}`, buffer);
57
+ return _super.upload.call(this, path, buffer, ...args);
58
+ }
59
+ finally {
60
+ this.timeEnd(`upload ${path}`);
61
+ }
62
+ });
63
+ }
64
+ download(path, ...args) {
65
+ const _super = Object.create(null, {
66
+ download: { get: () => super.download }
67
+ });
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ try {
70
+ this.timeBegin(`download ${path}`);
71
+ yield _super.download.call(this, path, ...args);
72
+ return fs.readFileSync(`${this.options.storage}${path}`);
73
+ }
74
+ finally {
75
+ this.timeEnd(`download ${path}`);
76
+ }
77
+ });
78
+ }
79
+ getAccess(path) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ return `${this.options.access}/dfs/get?filename=${path}`;
82
+ });
83
+ }
84
+ }
85
+ exports.default = NativeDfs;
package/lib/oss.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { Dfs, OssConfig } from "./declare";
3
+ import { VWebApplicationContext } from "vweb-core";
4
+ export default class OssDfs extends Dfs<'oss'> {
5
+ private readonly client;
6
+ constructor(options: OssConfig & {
7
+ type: 'oss';
8
+ }, context: VWebApplicationContext);
9
+ startup(): Promise<void>;
10
+ upload(path: string, buffer: any, ...args: any[]): Promise<string>;
11
+ download(path: string, ...args: any[]): Promise<Buffer>;
12
+ getAccess(path: string): Promise<string>;
13
+ }
package/lib/oss.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const declare_1 = require("./declare");
16
+ const ali_oss_1 = __importDefault(require("ali-oss"));
17
+ const fs_1 = __importDefault(require("fs"));
18
+ class OssDfs extends declare_1.Dfs {
19
+ constructor(options, context) {
20
+ super(options, context);
21
+ this.client = new ali_oss_1.default(options);
22
+ }
23
+ startup() {
24
+ const _super = Object.create(null, {
25
+ startup: { get: () => super.startup }
26
+ });
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ return _super.startup.call(this);
29
+ });
30
+ }
31
+ upload(path, buffer, ...args) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ try {
34
+ this.timeBegin(`upload ${path}`);
35
+ let result;
36
+ if (buffer.constructor.name === 'Buffer') {
37
+ result = yield this.client.put(path, buffer, ...args);
38
+ }
39
+ else if (typeof buffer === 'string') {
40
+ if (fs_1.default.existsSync(buffer)) {
41
+ result = yield this.client.put(path, buffer, ...args);
42
+ }
43
+ else {
44
+ result = yield this.client.put(path, Buffer.from(buffer), ...args);
45
+ }
46
+ }
47
+ else {
48
+ result = yield this.client.putStream(path, buffer, ...args);
49
+ }
50
+ return path;
51
+ }
52
+ finally {
53
+ this.timeEnd(`upload ${path}`);
54
+ }
55
+ });
56
+ }
57
+ download(path, ...args) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ try {
60
+ this.timeBegin(`download ${path}`);
61
+ const result = yield this.client.getStream(path, ...args);
62
+ return this.stream2buffer(result.stream);
63
+ }
64
+ finally {
65
+ this.timeEnd(`download ${path}`);
66
+ }
67
+ });
68
+ }
69
+ getAccess(path) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ if (this.options.access) {
72
+ return `${this.options.access}/dfs/get?filename=${path}`;
73
+ }
74
+ return this.client.getObjectUrl(path, this.options.access);
75
+ });
76
+ }
77
+ }
78
+ exports.default = OssDfs;
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "dfs-adapter",
3
+ "version": "0.0.1",
4
+ "description": "oss minio dfs native",
5
+ "main": "./lib/index",
6
+ "files": [
7
+ "lib/"
8
+ ],
9
+ "scripts": {
10
+ "test": "node bin/app.js",
11
+ "build": "tsc --build tsconfig.json",
12
+ "run": "npm run build && node bin/app.js"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git@codeup.aliyun.com:60b0836844816b8ed2335ed6/vweb/modules/mq-adapter.git"
17
+ },
18
+ "devDependencies": {
19
+ "@babel/cli": "^7.16.0",
20
+ "@babel/core": "^7.16.0",
21
+ "@babel/plugin-proposal-decorators": "^7.16.4",
22
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
23
+ "@babel/plugin-transform-runtime": "^7.16.4",
24
+ "@babel/preset-env": "^7.16.4",
25
+ "@babel/register": "^7.16.0",
26
+ "@babel/runtime": "^7.16.3",
27
+ "@types/ali-oss": "^6.16.3",
28
+ "@types/node": "^16.11.12",
29
+ "ali-oss": "^6.18.1",
30
+ "axios": "^1.6.1",
31
+ "babel-plugin-transform-decorators-legacy": "^1.3.5",
32
+ "form-data": "^4.0.0",
33
+ "minio": "^7.1.3",
34
+ "vweb-core": "^3.0.14",
35
+ "vweb-mvc": "^1.2.18"
36
+ },
37
+ "author": "",
38
+ "license": "ISC"
39
+ }