From 0cb1660f1c530ebfb931b77eb3977fc6aa524e5e Mon Sep 17 00:00:00 2001 From: wkj <278599135@qq.com> Date: Wed, 13 Sep 2023 16:55:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/types/Upload.d.ts | 14 +++++ es/types/Upload.js | 1 + es/types/Uploader.d.ts | 5 +- es/utils/externalUpload/aliyun.d.ts | 28 --------- es/utils/externalUpload/aliyun.js | 66 -------------------- es/utils/externalUpload/qiniu.d.ts | 20 ------ es/utils/externalUpload/qiniu.js | 59 ------------------ es/utils/externalUpload/qiniu_live.d.ts | 23 ------- es/utils/externalUpload/qiniu_live.js | 45 -------------- es/utils/uploader/qiniu.d.ts | 5 +- es/utils/uploader/qiniu.js | 13 ++-- lib/types/Upload.d.ts | 14 +++++ lib/types/Upload.js | 2 + lib/types/Uploader.d.ts | 5 +- lib/utils/externalUpload/aliyun.d.ts | 28 --------- lib/utils/externalUpload/aliyun.js | 67 -------------------- lib/utils/externalUpload/qiniu.d.ts | 20 ------ lib/utils/externalUpload/qiniu.js | 59 ------------------ lib/utils/externalUpload/qiniu_live.d.ts | 23 ------- lib/utils/externalUpload/qiniu_live.js | 40 ------------ lib/utils/uploader/qiniu.d.ts | 5 +- lib/utils/uploader/qiniu.js | 11 ++-- src/aspects/AspectDict.ts | 1 - src/aspects/extraFile.ts | 6 -- src/features/extraFile.ts | 1 - src/types/Upload.ts | 15 +++++ src/types/Uploader.ts | 47 +++++++++----- src/utils/externalUpload/aliyun.ts | 79 ------------------------ src/utils/externalUpload/qiniu.ts | 73 ---------------------- src/utils/externalUpload/qiniu_live.ts | 57 ----------------- src/utils/uploader/qiniu.ts | 77 +++++++++++++---------- 31 files changed, 141 insertions(+), 768 deletions(-) create mode 100644 es/types/Upload.d.ts create mode 100644 es/types/Upload.js delete mode 100644 es/utils/externalUpload/aliyun.d.ts delete mode 100644 es/utils/externalUpload/aliyun.js delete mode 100644 es/utils/externalUpload/qiniu.d.ts delete mode 100644 es/utils/externalUpload/qiniu.js delete mode 100644 es/utils/externalUpload/qiniu_live.d.ts delete mode 100644 es/utils/externalUpload/qiniu_live.js create mode 100644 lib/types/Upload.d.ts create mode 100644 lib/types/Upload.js delete mode 100644 lib/utils/externalUpload/aliyun.d.ts delete mode 100644 lib/utils/externalUpload/aliyun.js delete mode 100644 lib/utils/externalUpload/qiniu.d.ts delete mode 100644 lib/utils/externalUpload/qiniu.js delete mode 100644 lib/utils/externalUpload/qiniu_live.d.ts delete mode 100644 lib/utils/externalUpload/qiniu_live.js create mode 100644 src/types/Upload.ts delete mode 100644 src/utils/externalUpload/aliyun.ts delete mode 100644 src/utils/externalUpload/qiniu.ts delete mode 100644 src/utils/externalUpload/qiniu_live.ts diff --git a/es/types/Upload.d.ts b/es/types/Upload.d.ts new file mode 100644 index 000000000..dde4c4c7c --- /dev/null +++ b/es/types/Upload.d.ts @@ -0,0 +1,14 @@ +export declare type QiniuUploadInfo = { + key?: string; + uploadToken: string; + uploadHost: string; + bucket: string; +}; +export declare type AliyunUploadInfo = { + key?: string; + signature: string; + policy: string; + uploadHost: string; + bucket: string; + accessKey: string; +}; diff --git a/es/types/Upload.js b/es/types/Upload.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/es/types/Upload.js @@ -0,0 +1 @@ +export {}; diff --git a/es/types/Uploader.d.ts b/es/types/Uploader.d.ts index 210b97b80..fa4c65515 100644 --- a/es/types/Uploader.d.ts +++ b/es/types/Uploader.d.ts @@ -16,11 +16,10 @@ export default interface Uploader { * @param extraFile * @returns */ - upload: (extraFile: EntityDict['extraFile']['OpSchema'], uploadFn: (name: string, // 文件的part name + upload: (extraFile: EntityDict['extraFile']['OpSchema'], uploadFn: (file: File | string, name: string, // 文件的part name uploadUrl: string, // 上传的url formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) - file: string | File) => Promise, file: string | File) => Promise; + autoInform?: boolean) => Promise, file: string | File) => Promise; /** * 后台对upload是否成功不确定的文件,向OSS发起主动确认 * @param extraFile diff --git a/es/utils/externalUpload/aliyun.d.ts b/es/utils/externalUpload/aliyun.d.ts deleted file mode 100644 index 0363ea9ff..000000000 --- a/es/utils/externalUpload/aliyun.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -export default class aliyunInstance { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }); - getUploadInfo(key?: string): { - key: string | undefined; - signature: string; - policy: string; - uploadHost: string; - bucket: string; - domain: string; - accessKey: string; - }; - getPolicyBase64(timeout?: number): string; - getSignature(policyBase64: string): string; - hmacSha1(encodedFlags: any, secretKey: string): string; - urlSafeBase64Encode(jsonFlags: string): string; - base64ToUrlSafe(v: string): string; -} diff --git a/es/utils/externalUpload/aliyun.js b/es/utils/externalUpload/aliyun.js deleted file mode 100644 index ee8b89521..000000000 --- a/es/utils/externalUpload/aliyun.js +++ /dev/null @@ -1,66 +0,0 @@ -import crypto from 'crypto'; -import { Buffer } from 'buffer'; -export default class aliyunInstance { - accessKey; - secretKey; - uploadHost; //阿里云上传url - bucket; - domain; - constructor(config) { - const { accessKey, secretKey, uploadHost, bucket, domain } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - getUploadInfo(key) { - try { - const { uploadHost, domain, bucket, accessKey } = this; - const policy = this.getPolicyBase64(); - const signature = this.getSignature(policy); - return { - key, - signature, - policy, - uploadHost, - bucket, - domain, - accessKey, - }; - } - catch (err) { - throw err; - } - } - getPolicyBase64(timeout = 8000) { - const date = new Date(); - date.setHours(date.getHours() + timeout); - const policyText = { - expiration: date.toISOString(), - conditions: [ - ['content-length-range', 0, 5 * 1024 * 1024], // 设置上传文件的大小限制,5mb - ], - }; - const policyBase64 = this.urlSafeBase64Encode(JSON.stringify(policyText)); - return policyBase64; - } - getSignature(policyBase64) { - const encoded = this.hmacSha1(policyBase64, this.secretKey); - ; - const signature = this.base64ToUrlSafe(encoded); - return signature; - } - hmacSha1(encodedFlags, secretKey) { - const hmac = crypto.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - } - urlSafeBase64Encode(jsonFlags) { - const encoded = Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - } - base64ToUrlSafe(v) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - } -} diff --git a/es/utils/externalUpload/qiniu.d.ts b/es/utils/externalUpload/qiniu.d.ts deleted file mode 100644 index e3eea171a..000000000 --- a/es/utils/externalUpload/qiniu.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { QiniuUploadInfo } from 'oak-frontend-base'; -export default class qiniuInstance { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }); - getUploadInfo(key?: string): QiniuUploadInfo; - getToken(scope: string): string; - base64ToUrlSafe(v: string): string; - hmacSha1(encodedFlags: any, secretKey: string): string; - urlSafeBase64Encode(jsonFlags: string): string; -} diff --git a/es/utils/externalUpload/qiniu.js b/es/utils/externalUpload/qiniu.js deleted file mode 100644 index 2b14db253..000000000 --- a/es/utils/externalUpload/qiniu.js +++ /dev/null @@ -1,59 +0,0 @@ -import crypto from 'crypto'; -import { Buffer } from 'buffer'; -export default class qiniuInstance { - accessKey; - secretKey; - uploadHost; //七牛上传url - bucket; - domain; - constructor(config) { - const { accessKey, secretKey, uploadHost, bucket, domain } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - getUploadInfo(key) { - try { - const { uploadHost, domain, bucket } = this; - const scope = key ? `${bucket}:${key}` : bucket; - const uploadToken = this.getToken(scope); - return { - key, - uploadToken, - uploadHost, - bucket, - domain, - }; - } - catch (err) { - throw err; - } - } - getToken(scope) { - // 构造策略 - const putPolicy = { - scope: scope, - deadline: 3600 + Math.floor(Date.now() / 1000), - }; - // 构造凭证 - const encodedFlags = this.urlSafeBase64Encode(JSON.stringify(putPolicy)); - const encoded = this.hmacSha1(encodedFlags, this.secretKey); - const encodedSign = this.base64ToUrlSafe(encoded); - const uploadToken = this.accessKey + ':' + encodedSign + ':' + encodedFlags; - return uploadToken; - } - base64ToUrlSafe(v) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - } - hmacSha1(encodedFlags, secretKey) { - const hmac = crypto.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - } - urlSafeBase64Encode(jsonFlags) { - const encoded = Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - } -} diff --git a/es/utils/externalUpload/qiniu_live.d.ts b/es/utils/externalUpload/qiniu_live.d.ts deleted file mode 100644 index c1644e1d4..000000000 --- a/es/utils/externalUpload/qiniu_live.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -export default class QiniuLiveInstance { - accessKey: string; - secretKey: string; - host: string; - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; - rawQuery?: string; - contentType?: string; - contentLength?: string; - bodyStr?: string; - constructor(config: { - accessKey: string; - secretKey: string; - host: string; - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; - rawQuery?: string; - contentType?: string; - bodyStr?: string; - contentLength?: string; - }); - getToken(): string; -} diff --git a/es/utils/externalUpload/qiniu_live.js b/es/utils/externalUpload/qiniu_live.js deleted file mode 100644 index b719299d7..000000000 --- a/es/utils/externalUpload/qiniu_live.js +++ /dev/null @@ -1,45 +0,0 @@ -import { base64ToUrlSafe, hmacSha1 } from '../sign'; -export default class QiniuLiveInstance { - accessKey; - secretKey; - host; // 请求域名, - method; - path; // 请求路径 实际的请求路径详见各七牛直播云接口说明的请求包 - rawQuery; - contentType; - contentLength; - bodyStr; - constructor(config) { - const { accessKey, secretKey, host, method, path, rawQuery, contentType, bodyStr, contentLength } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.host = host; - this.method = method; - this.path = path; - this.rawQuery = rawQuery; - this.contentType = contentType; - this.bodyStr = bodyStr; - this.contentLength = contentLength; - } - getToken() { - const { method, path, rawQuery, host, contentType, contentLength, bodyStr, accessKey, secretKey } = this; - // 1. 添加 Path - let data = `${method} ${path}`; - if (rawQuery) { - data += `?${rawQuery}`; - } - data += `\nHost: ${host}`; - if (contentType) { - data += `\nContent-Type: ${contentType}`; - } - data += "\n\n"; - if (bodyStr && contentType && contentType !== "application/octet-stream") { - data += bodyStr; - } - console.log('data', data); - const sign = hmacSha1(data, secretKey); - const encodedSign = base64ToUrlSafe(sign); - const toke = "Qiniu " + accessKey + ":" + encodedSign; - return toke; - } -} diff --git a/es/utils/uploader/qiniu.d.ts b/es/utils/uploader/qiniu.d.ts index 0bed2f8cc..b77f00196 100644 --- a/es/utils/uploader/qiniu.d.ts +++ b/es/utils/uploader/qiniu.d.ts @@ -6,11 +6,10 @@ import { OpSchema } from '../../oak-app-domain/ExtraFile/Schema'; export default class Qiniu implements Uploader { name: string; formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext): Promise; - upload(extraFile: OpSchema, uploadFn: (name: string, // 文件的part name + upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name uploadUrl: string, // 上传的url formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) - file: string | File) => Promise, file: string | File): Promise; + autoInform?: boolean) => Promise, file: string | File): Promise; checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext): Promise; removeFile(extraFile: OpSchema, context: BackendRuntimeContext): Promise; } diff --git a/es/utils/uploader/qiniu.js b/es/utils/uploader/qiniu.js index eb394350b..b3dddcbfa 100644 --- a/es/utils/uploader/qiniu.js +++ b/es/utils/uploader/qiniu.js @@ -1,5 +1,4 @@ import { getConfig } from '../../utils/getContextConfig'; -import { get } from 'oak-domain/lib/utils/lodash'; const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; export default class Qiniu { name = 'qiniu'; @@ -7,19 +6,19 @@ export default class Qiniu { const { origin, objectId, extension, entity, bucket } = extraFile; // 构造文件上传所需的key const key = `${entity ? entity + '/' : ''}${objectId}${extension ? '.' + extension : ''}`; - const { instance, config, } = await getConfig(context, 'Cos', 'qiniu'); + const { instance, config } = await getConfig(context, 'Cos', 'qiniu'); const { uploadHost, bucket: bucket2 } = config; Object.assign(extraFile, { bucket: bucket || bucket2, - uploadMeta: instance.getUploadInfo(uploadHost, bucket || bucket2, key) + uploadMeta: instance.getUploadInfo(uploadHost, bucket || bucket2, key), }); } async upload(extraFile, uploadFn, file) { const uploadMeta = extraFile.uploadMeta; - const result = await uploadFn('', get(extraFile, 'uploadMeta.uploadHost', ''), { - key: uploadMeta?.key, - token: uploadMeta?.uploadToken, - }, true, file); + const result = await uploadFn(file, 'file', uploadMeta.uploadHost, { + key: uploadMeta.key, + token: uploadMeta.uploadToken, + }, true); if (result.success === true || result.key) { return; } diff --git a/lib/types/Upload.d.ts b/lib/types/Upload.d.ts new file mode 100644 index 000000000..dde4c4c7c --- /dev/null +++ b/lib/types/Upload.d.ts @@ -0,0 +1,14 @@ +export declare type QiniuUploadInfo = { + key?: string; + uploadToken: string; + uploadHost: string; + bucket: string; +}; +export declare type AliyunUploadInfo = { + key?: string; + signature: string; + policy: string; + uploadHost: string; + bucket: string; + accessKey: string; +}; diff --git a/lib/types/Upload.js b/lib/types/Upload.js new file mode 100644 index 000000000..c8ad2e549 --- /dev/null +++ b/lib/types/Upload.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/lib/types/Uploader.d.ts b/lib/types/Uploader.d.ts index 210b97b80..fa4c65515 100644 --- a/lib/types/Uploader.d.ts +++ b/lib/types/Uploader.d.ts @@ -16,11 +16,10 @@ export default interface Uploader { * @param extraFile * @returns */ - upload: (extraFile: EntityDict['extraFile']['OpSchema'], uploadFn: (name: string, // 文件的part name + upload: (extraFile: EntityDict['extraFile']['OpSchema'], uploadFn: (file: File | string, name: string, // 文件的part name uploadUrl: string, // 上传的url formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) - file: string | File) => Promise, file: string | File) => Promise; + autoInform?: boolean) => Promise, file: string | File) => Promise; /** * 后台对upload是否成功不确定的文件,向OSS发起主动确认 * @param extraFile diff --git a/lib/utils/externalUpload/aliyun.d.ts b/lib/utils/externalUpload/aliyun.d.ts deleted file mode 100644 index 0363ea9ff..000000000 --- a/lib/utils/externalUpload/aliyun.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -export default class aliyunInstance { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }); - getUploadInfo(key?: string): { - key: string | undefined; - signature: string; - policy: string; - uploadHost: string; - bucket: string; - domain: string; - accessKey: string; - }; - getPolicyBase64(timeout?: number): string; - getSignature(policyBase64: string): string; - hmacSha1(encodedFlags: any, secretKey: string): string; - urlSafeBase64Encode(jsonFlags: string): string; - base64ToUrlSafe(v: string): string; -} diff --git a/lib/utils/externalUpload/aliyun.js b/lib/utils/externalUpload/aliyun.js deleted file mode 100644 index 7bdafdfbf..000000000 --- a/lib/utils/externalUpload/aliyun.js +++ /dev/null @@ -1,67 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var tslib_1 = require("tslib"); -var crypto_1 = tslib_1.__importDefault(require("crypto")); -var buffer_1 = require("buffer"); -var aliyunInstance = /** @class */ (function () { - function aliyunInstance(config) { - var accessKey = config.accessKey, secretKey = config.secretKey, uploadHost = config.uploadHost, bucket = config.bucket, domain = config.domain; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - aliyunInstance.prototype.getUploadInfo = function (key) { - try { - var _a = this, uploadHost = _a.uploadHost, domain = _a.domain, bucket = _a.bucket, accessKey = _a.accessKey; - var policy = this.getPolicyBase64(); - var signature = this.getSignature(policy); - return { - key: key, - signature: signature, - policy: policy, - uploadHost: uploadHost, - bucket: bucket, - domain: domain, - accessKey: accessKey, - }; - } - catch (err) { - throw err; - } - }; - aliyunInstance.prototype.getPolicyBase64 = function (timeout) { - if (timeout === void 0) { timeout = 8000; } - var date = new Date(); - date.setHours(date.getHours() + timeout); - var policyText = { - expiration: date.toISOString(), - conditions: [ - ['content-length-range', 0, 5 * 1024 * 1024], // 设置上传文件的大小限制,5mb - ], - }; - var policyBase64 = this.urlSafeBase64Encode(JSON.stringify(policyText)); - return policyBase64; - }; - aliyunInstance.prototype.getSignature = function (policyBase64) { - var encoded = this.hmacSha1(policyBase64, this.secretKey); - ; - var signature = this.base64ToUrlSafe(encoded); - return signature; - }; - aliyunInstance.prototype.hmacSha1 = function (encodedFlags, secretKey) { - var hmac = crypto_1.default.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - }; - aliyunInstance.prototype.urlSafeBase64Encode = function (jsonFlags) { - var encoded = buffer_1.Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - }; - aliyunInstance.prototype.base64ToUrlSafe = function (v) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - }; - return aliyunInstance; -}()); -exports.default = aliyunInstance; diff --git a/lib/utils/externalUpload/qiniu.d.ts b/lib/utils/externalUpload/qiniu.d.ts deleted file mode 100644 index e3eea171a..000000000 --- a/lib/utils/externalUpload/qiniu.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { QiniuUploadInfo } from 'oak-frontend-base'; -export default class qiniuInstance { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }); - getUploadInfo(key?: string): QiniuUploadInfo; - getToken(scope: string): string; - base64ToUrlSafe(v: string): string; - hmacSha1(encodedFlags: any, secretKey: string): string; - urlSafeBase64Encode(jsonFlags: string): string; -} diff --git a/lib/utils/externalUpload/qiniu.js b/lib/utils/externalUpload/qiniu.js deleted file mode 100644 index 2a76384ef..000000000 --- a/lib/utils/externalUpload/qiniu.js +++ /dev/null @@ -1,59 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var tslib_1 = require("tslib"); -var crypto_1 = tslib_1.__importDefault(require("crypto")); -var buffer_1 = require("buffer"); -var qiniuInstance = /** @class */ (function () { - function qiniuInstance(config) { - var accessKey = config.accessKey, secretKey = config.secretKey, uploadHost = config.uploadHost, bucket = config.bucket, domain = config.domain; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - qiniuInstance.prototype.getUploadInfo = function (key) { - try { - var _a = this, uploadHost = _a.uploadHost, domain = _a.domain, bucket = _a.bucket; - var scope = key ? "".concat(bucket, ":").concat(key) : bucket; - var uploadToken = this.getToken(scope); - return { - key: key, - uploadToken: uploadToken, - uploadHost: uploadHost, - bucket: bucket, - domain: domain, - }; - } - catch (err) { - throw err; - } - }; - qiniuInstance.prototype.getToken = function (scope) { - // 构造策略 - var putPolicy = { - scope: scope, - deadline: 3600 + Math.floor(Date.now() / 1000), - }; - // 构造凭证 - var encodedFlags = this.urlSafeBase64Encode(JSON.stringify(putPolicy)); - var encoded = this.hmacSha1(encodedFlags, this.secretKey); - var encodedSign = this.base64ToUrlSafe(encoded); - var uploadToken = this.accessKey + ':' + encodedSign + ':' + encodedFlags; - return uploadToken; - }; - qiniuInstance.prototype.base64ToUrlSafe = function (v) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - }; - qiniuInstance.prototype.hmacSha1 = function (encodedFlags, secretKey) { - var hmac = crypto_1.default.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - }; - qiniuInstance.prototype.urlSafeBase64Encode = function (jsonFlags) { - var encoded = buffer_1.Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - }; - return qiniuInstance; -}()); -exports.default = qiniuInstance; diff --git a/lib/utils/externalUpload/qiniu_live.d.ts b/lib/utils/externalUpload/qiniu_live.d.ts deleted file mode 100644 index c1644e1d4..000000000 --- a/lib/utils/externalUpload/qiniu_live.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -export default class QiniuLiveInstance { - accessKey: string; - secretKey: string; - host: string; - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; - rawQuery?: string; - contentType?: string; - contentLength?: string; - bodyStr?: string; - constructor(config: { - accessKey: string; - secretKey: string; - host: string; - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; - rawQuery?: string; - contentType?: string; - bodyStr?: string; - contentLength?: string; - }); - getToken(): string; -} diff --git a/lib/utils/externalUpload/qiniu_live.js b/lib/utils/externalUpload/qiniu_live.js deleted file mode 100644 index 260dc613f..000000000 --- a/lib/utils/externalUpload/qiniu_live.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var sign_1 = require("../sign"); -var QiniuLiveInstance = /** @class */ (function () { - function QiniuLiveInstance(config) { - var accessKey = config.accessKey, secretKey = config.secretKey, host = config.host, method = config.method, path = config.path, rawQuery = config.rawQuery, contentType = config.contentType, bodyStr = config.bodyStr, contentLength = config.contentLength; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.host = host; - this.method = method; - this.path = path; - this.rawQuery = rawQuery; - this.contentType = contentType; - this.bodyStr = bodyStr; - this.contentLength = contentLength; - } - QiniuLiveInstance.prototype.getToken = function () { - var _a = this, method = _a.method, path = _a.path, rawQuery = _a.rawQuery, host = _a.host, contentType = _a.contentType, contentLength = _a.contentLength, bodyStr = _a.bodyStr, accessKey = _a.accessKey, secretKey = _a.secretKey; - // 1. 添加 Path - var data = "".concat(method, " ").concat(path); - if (rawQuery) { - data += "?".concat(rawQuery); - } - data += "\nHost: ".concat(host); - if (contentType) { - data += "\nContent-Type: ".concat(contentType); - } - data += "\n\n"; - if (bodyStr && contentType && contentType !== "application/octet-stream") { - data += bodyStr; - } - console.log('data', data); - var sign = (0, sign_1.hmacSha1)(data, secretKey); - var encodedSign = (0, sign_1.base64ToUrlSafe)(sign); - var toke = "Qiniu " + accessKey + ":" + encodedSign; - return toke; - }; - return QiniuLiveInstance; -}()); -exports.default = QiniuLiveInstance; diff --git a/lib/utils/uploader/qiniu.d.ts b/lib/utils/uploader/qiniu.d.ts index 0bed2f8cc..b77f00196 100644 --- a/lib/utils/uploader/qiniu.d.ts +++ b/lib/utils/uploader/qiniu.d.ts @@ -6,11 +6,10 @@ import { OpSchema } from '../../oak-app-domain/ExtraFile/Schema'; export default class Qiniu implements Uploader { name: string; formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext): Promise; - upload(extraFile: OpSchema, uploadFn: (name: string, // 文件的part name + upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name uploadUrl: string, // 上传的url formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) - file: string | File) => Promise, file: string | File): Promise; + autoInform?: boolean) => Promise, file: string | File): Promise; checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext): Promise; removeFile(extraFile: OpSchema, context: BackendRuntimeContext): Promise; } diff --git a/lib/utils/uploader/qiniu.js b/lib/utils/uploader/qiniu.js index ca6ba5ea0..d0e66bbe3 100644 --- a/lib/utils/uploader/qiniu.js +++ b/lib/utils/uploader/qiniu.js @@ -2,7 +2,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var getContextConfig_1 = require("../../utils/getContextConfig"); -var lodash_1 = require("oak-domain/lib/utils/lodash"); var QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; var Qiniu = /** @class */ (function () { function Qiniu() { @@ -22,7 +21,7 @@ var Qiniu = /** @class */ (function () { _b = config, uploadHost = _b.uploadHost, bucket2 = _b.bucket; Object.assign(extraFile, { bucket: bucket || bucket2, - uploadMeta: instance.getUploadInfo(uploadHost, bucket || bucket2, key) + uploadMeta: instance.getUploadInfo(uploadHost, bucket || bucket2, key), }); return [2 /*return*/]; } @@ -36,10 +35,10 @@ var Qiniu = /** @class */ (function () { switch (_a.label) { case 0: uploadMeta = extraFile.uploadMeta; - return [4 /*yield*/, uploadFn('', (0, lodash_1.get)(extraFile, 'uploadMeta.uploadHost', ''), { - key: uploadMeta === null || uploadMeta === void 0 ? void 0 : uploadMeta.key, - token: uploadMeta === null || uploadMeta === void 0 ? void 0 : uploadMeta.uploadToken, - }, true, file)]; + return [4 /*yield*/, uploadFn(file, 'file', uploadMeta.uploadHost, { + key: uploadMeta.key, + token: uploadMeta.uploadToken, + }, true)]; case 1: result = _a.sent(); if (result.success === true || result.key) { diff --git a/src/aspects/AspectDict.ts b/src/aspects/AspectDict.ts index d98f0b961..51d0f041d 100644 --- a/src/aspects/AspectDict.ts +++ b/src/aspects/AspectDict.ts @@ -1,7 +1,6 @@ import { WebEnv, WechatMpEnv } from 'oak-domain/lib/types/Environment'; import { AppType } from '../oak-app-domain/Application/Schema'; import { EntityDict } from '../oak-app-domain'; -import { QiniuUploadInfo } from 'oak-frontend-base'; import { Config, Origin } from '../types/Config'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; diff --git a/src/aspects/extraFile.ts b/src/aspects/extraFile.ts index 680f35c65..5f98df124 100644 --- a/src/aspects/extraFile.ts +++ b/src/aspects/extraFile.ts @@ -1,10 +1,4 @@ import { EntityDict } from '../oak-app-domain'; -import { Origin, QiniuCosConfig } from '../types/Config'; -import { QiniuUploadInfo } from 'oak-frontend-base'; -import { getConfig } from '../utils/getContextConfig'; -import { assert } from 'oak-domain/lib/utils/assert'; -import { QiniuCloudInstance } from 'oak-external-sdk'; -import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { WechatSDK } from 'oak-external-sdk'; // 请求链接获取标题,发布时间,图片等信息 diff --git a/src/features/extraFile.ts b/src/features/extraFile.ts index a21de0207..0062a52e5 100644 --- a/src/features/extraFile.ts +++ b/src/features/extraFile.ts @@ -5,7 +5,6 @@ import { Locales } from 'oak-frontend-base/es/features/locales'; import { CommonAspectDict } from 'oak-common-aspect'; import AspectDict from '../aspects/AspectDict'; import { EntityDict } from '../oak-app-domain'; -import { QiniuUploadInfo } from 'oak-frontend-base'; import { Config, Origin } from '../types/Config'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext'; diff --git a/src/types/Upload.ts b/src/types/Upload.ts new file mode 100644 index 000000000..2e604cd3f --- /dev/null +++ b/src/types/Upload.ts @@ -0,0 +1,15 @@ +export type QiniuUploadInfo = { + key?: string; + uploadToken: string; + uploadHost: string; + bucket: string; +}; + +export type AliyunUploadInfo = { + key?: string; + signature: string; + policy: string; + uploadHost: string; + bucket: string; + accessKey: string; +}; diff --git a/src/types/Uploader.ts b/src/types/Uploader.ts index 64b7a9e7e..d3990eb5a 100644 --- a/src/types/Uploader.ts +++ b/src/types/Uploader.ts @@ -7,41 +7,54 @@ export default interface Uploader { /** * 注入在后台extrafile生成之前 - * @param extraFile,要生成的extraFile数据 + * @param extraFile,要生成的extraFile数据 * @param context 后台上下文 - * @returns + * @returns */ - formUploadMeta: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext) => Promise; + formUploadMeta: ( + extraFile: EntityDict['extraFile']['OpSchema'], + context: BackendRuntimeContext + ) => Promise; /** * 前台在生成extraFile返回之后调用此函数,获得上传OSS的相应参数 * 上传成功后,OSS服务器尽量使用通过后台回调服务器的方式去确认上传,若OSS不提供此能力则将autoInform置为false,前台主动在上传成功之后去更新 - * @param extraFile - * @returns + * @param extraFile + * @returns */ - upload: (extraFile: EntityDict['extraFile']['OpSchema'], uploadFn: ( - name: string, // 文件的part name - uploadUrl: string, // 上传的url - formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) + upload: ( + extraFile: EntityDict['extraFile']['OpSchema'], + uploadFn: ( + file: File | string, + name: string, // 文件的part name + uploadUrl: string, // 上传的url + formData: Record, // 上传的其它part参数 + autoInform?: boolean // 上传成功是否会自动通知server(若不会则需要前台显式通知) + ) => Promise, file: string | File - ) => Promise, file: string | File) => Promise, + ) => Promise; // 前端上传时对回调的处理 /** * 后台对upload是否成功不确定的文件,向OSS发起主动确认 - * @param extraFile + * @param extraFile * @returns 是否已经上传OSS成功 */ - checkWhetherSuccess: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext) => Promise; + checkWhetherSuccess: ( + extraFile: EntityDict['extraFile']['OpSchema'], + context: BackendRuntimeContext + ) => Promise; /** * 后台向OSS发起删除命令 - * @param extraFile - * @param context - * @returns + * @param extraFile + * @param context + * @returns */ - removeFile: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext) => Promise; + removeFile: ( + extraFile: EntityDict['extraFile']['OpSchema'], + context: BackendRuntimeContext + ) => Promise; } \ No newline at end of file diff --git a/src/utils/externalUpload/aliyun.ts b/src/utils/externalUpload/aliyun.ts deleted file mode 100644 index 7995aca20..000000000 --- a/src/utils/externalUpload/aliyun.ts +++ /dev/null @@ -1,79 +0,0 @@ -import crypto from 'crypto'; -import { Buffer } from 'buffer'; - -export default class aliyunInstance { - accessKey: string; - secretKey: string; - uploadHost: string; //阿里云上传url - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }) { - const { accessKey, secretKey, uploadHost, bucket, domain } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - - getUploadInfo(key?: string) { - try { - const { uploadHost, domain, bucket, accessKey } = this; - const policy = this.getPolicyBase64(); - const signature = this.getSignature(policy); - return { - key, - signature, - policy, - uploadHost, - bucket, - domain, - accessKey, - }; - } catch (err) { - throw err; - } - } - - getPolicyBase64(timeout: number = 8000) { - const date = new Date(); - date.setHours(date.getHours() + timeout); - const policyText = { - expiration: date.toISOString(), //设置该Policy的失效时间 - conditions: [ - ['content-length-range', 0, 5 * 1024 * 1024], // 设置上传文件的大小限制,5mb - ], - }; - const policyBase64 = this.urlSafeBase64Encode( - JSON.stringify(policyText) - ); - return policyBase64; - } - - getSignature(policyBase64: string) { - const encoded = this.hmacSha1(policyBase64, this.secretKey);; - const signature = this.base64ToUrlSafe(encoded); - return signature; - } - - hmacSha1(encodedFlags: any, secretKey: string) { - const hmac = crypto.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - } - - urlSafeBase64Encode(jsonFlags: string) { - const encoded = Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - } - - base64ToUrlSafe(v: string) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - } -} diff --git a/src/utils/externalUpload/qiniu.ts b/src/utils/externalUpload/qiniu.ts deleted file mode 100644 index 3a92ba001..000000000 --- a/src/utils/externalUpload/qiniu.ts +++ /dev/null @@ -1,73 +0,0 @@ -import crypto from 'crypto'; -import { Buffer } from 'buffer'; -import { QiniuUploadInfo } from 'oak-frontend-base'; - -export default class qiniuInstance { - accessKey: string; - secretKey: string; - uploadHost: string; //七牛上传url - bucket: string; - domain: string; - constructor(config: { - accessKey: string; - secretKey: string; - uploadHost: string; - bucket: string; - domain: string; - }) { - const { accessKey, secretKey, uploadHost, bucket, domain } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.uploadHost = uploadHost; - this.bucket = bucket; - this.domain = domain; - } - - getUploadInfo(key?: string): QiniuUploadInfo { - try { - const { uploadHost, domain, bucket } = this; - const scope = key ? `${bucket}:${key}` : bucket; - const uploadToken = this.getToken(scope); - return { - key, - uploadToken, - uploadHost, - bucket, - domain, - }; - } catch (err) { - throw err; - } - } - - getToken(scope: string) { - // 构造策略 - const putPolicy = { - scope: scope, - deadline: 3600 + Math.floor(Date.now() / 1000), - }; - // 构造凭证 - const encodedFlags = this.urlSafeBase64Encode( - JSON.stringify(putPolicy) - ); - const encoded = this.hmacSha1(encodedFlags, this.secretKey); - const encodedSign = this.base64ToUrlSafe(encoded); - const uploadToken = - this.accessKey + ':' + encodedSign + ':' + encodedFlags; - return uploadToken; - } - - base64ToUrlSafe(v: string) { - return v.replace(/\//g, '_').replace(/\+/g, '-'); - } - - hmacSha1(encodedFlags: any, secretKey: string) { - const hmac = crypto.createHmac('sha1', secretKey); - hmac.update(encodedFlags); - return hmac.digest('base64'); - } - urlSafeBase64Encode(jsonFlags: string) { - const encoded = Buffer.from(jsonFlags).toString('base64'); - return this.base64ToUrlSafe(encoded); - } -} diff --git a/src/utils/externalUpload/qiniu_live.ts b/src/utils/externalUpload/qiniu_live.ts deleted file mode 100644 index 44000aff5..000000000 --- a/src/utils/externalUpload/qiniu_live.ts +++ /dev/null @@ -1,57 +0,0 @@ -import {base64ToUrlSafe, hmacSha1, urlSafeBase64Encode} from '../sign'; - -export default class QiniuLiveInstance { - accessKey: string; - secretKey: string; - host: string; // 请求域名, - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; // 请求路径 实际的请求路径详见各七牛直播云接口说明的请求包 - rawQuery?: string; - contentType?: string; - contentLength?: string; - bodyStr?: string; - constructor(config: { - accessKey: string; - secretKey: string; - host: string; - method: 'GET' | 'POST' | 'PUT' | 'DELETE'; - path: string; - rawQuery?: string; - contentType?: string; - bodyStr?: string; - contentLength?: string; - }) { - const { accessKey, secretKey, host, method, path, rawQuery, contentType, bodyStr, contentLength } = config; - this.accessKey = accessKey; - this.secretKey = secretKey; - this.host = host; - this.method = method; - this.path = path; - this.rawQuery = rawQuery; - this.contentType = contentType; - this.bodyStr = bodyStr; - this.contentLength = contentLength; - } - - getToken() { - const {method, path, rawQuery, host, contentType, contentLength, bodyStr, accessKey, secretKey} = this; - // 1. 添加 Path - let data = `${method} ${path}` - if (rawQuery) { - data += `?${rawQuery}` - } - data += `\nHost: ${host}` - if (contentType) { - data += `\nContent-Type: ${contentType}` - } - data += "\n\n" - if(bodyStr && contentType && contentType !== "application/octet-stream") { - data+=bodyStr; - } - console.log('data', data); - const sign = hmacSha1(data, secretKey); - const encodedSign = base64ToUrlSafe(sign); - const toke = "Qiniu " + accessKey + ":" + encodedSign; - return toke; - } -} diff --git a/src/utils/uploader/qiniu.ts b/src/utils/uploader/qiniu.ts index 7df2c1668..64ae4cfc4 100644 --- a/src/utils/uploader/qiniu.ts +++ b/src/utils/uploader/qiniu.ts @@ -5,7 +5,7 @@ import { BackendRuntimeContext } from '../../context/BackendRuntimeContext'; import Uploader from "../../types/Uploader"; import { OpSchema } from '../../oak-app-domain/ExtraFile/Schema'; -import { QiniuUploadInfo } from 'oak-frontend-base'; +import { QiniuUploadInfo } from '../../types/Upload'; import { getConfig } from '../../utils/getContextConfig'; import { QiniuCosConfig } from '../../types/Config'; import { QiniuCloudInstance } from 'oak-external-sdk'; @@ -16,42 +16,52 @@ const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; export default class Qiniu implements Uploader { name = 'qiniu'; - async formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext) { - const { origin, objectId, extension, entity, bucket } = - extraFile; + async formUploadMeta( + extraFile: OpSchema, + context: BackendRuntimeContext + ) { + const { origin, objectId, extension, entity, bucket } = extraFile; // 构造文件上传所需的key - const key = `${entity ? entity + '/' : ''}${objectId}${extension ? '.' + extension : ''}`; - const { - instance, - config, - } = await getConfig>(context, 'Cos', 'qiniu'); + const key = `${entity ? entity + '/' : ''}${objectId}${ + extension ? '.' + extension : '' + }`; + const { instance, config } = await getConfig< + ED, + BackendRuntimeContext + >(context, 'Cos', 'qiniu'); const { uploadHost, bucket: bucket2 } = config as QiniuCosConfig; - Object.assign( - extraFile, { + Object.assign(extraFile, { bucket: bucket || bucket2, - uploadMeta: (instance as QiniuCloudInstance).getUploadInfo(uploadHost, bucket || bucket2, key) - } - ) + uploadMeta: (instance as QiniuCloudInstance).getUploadInfo( + uploadHost, + bucket || bucket2, + key + ), + }); } - async upload(extraFile: OpSchema, uploadFn: ( - name: string, // 文件的part name - uploadUrl: string, // 上传的url - formData: Record, // 上传的其它part参数 - autoInform: boolean, // 上传成功是否会自动通知server(若不会则需要前台显式通知) - file: string | File, - ) => Promise, file: string | File) { + async upload( + extraFile: OpSchema, + uploadFn: ( + file: File | string, + name: string, // 文件的part name + uploadUrl: string, // 上传的url + formData: Record, // 上传的其它part参数 + autoInform?: boolean // 上传成功是否会自动通知server(若不会则需要前台显式通知) + ) => Promise, + file: string | File + ) { const uploadMeta = extraFile.uploadMeta! as QiniuUploadInfo; const result = await uploadFn( - '', - get(extraFile, 'uploadMeta.uploadHost', ''), - { - key: uploadMeta?.key, - token: uploadMeta?.uploadToken, - }, - true, file, + 'file', + uploadMeta.uploadHost, + { + key: uploadMeta.key, + token: uploadMeta.uploadToken, + }, + true ); if (result.success === true || result.key) { return; @@ -59,15 +69,20 @@ export default class Qiniu implements Up throw new Error('图片上传失败'); } - async checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext) { + async checkWhetherSuccess( + extraFile: OpSchema, + context: BackendRuntimeContext + ) { const { uploadMeta, bucket } = extraFile; const { key } = uploadMeta as { key: string }; - const qiniuSearchUrl = QiniuSearchUrl.replace('EncodedEntryURI', `${bucket}:${key}`); + const qiniuSearchUrl = QiniuSearchUrl.replace( + 'EncodedEntryURI', + `${bucket}:${key}` + ); return false; } async removeFile(extraFile: OpSchema, context: BackendRuntimeContext) { const { bucket, uploadMeta } = extraFile; - } };