移除无用的代码

This commit is contained in:
Wang Kejun 2023-09-13 16:55:38 +08:00
parent c15c8fdd49
commit 0cb1660f1c
31 changed files with 141 additions and 768 deletions

14
es/types/Upload.d.ts vendored Normal file
View File

@ -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;
};

1
es/types/Upload.js Normal file
View File

@ -0,0 +1 @@
export {};

View File

@ -16,11 +16,10 @@ export default interface Uploader<ED extends EntityDict & BaseEntityDict> {
* @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<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
file: string | File) => Promise<any>, file: string | File) => Promise<void>;
autoInform?: boolean) => Promise<any>, file: string | File) => Promise<void>;
/**
* upload是否成功不确定的文件OSS发起主动确认
* @param extraFile

View File

@ -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;
}

View File

@ -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, '-');
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -6,11 +6,10 @@ import { OpSchema } from '../../oak-app-domain/ExtraFile/Schema';
export default class Qiniu<ED extends EntityDict & BaseEntityDict> implements Uploader<ED> {
name: string;
formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<void>;
upload(extraFile: OpSchema, uploadFn: (name: string, // 文件的part name
upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name
uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
file: string | File) => Promise<any>, file: string | File): Promise<void>;
autoInform?: boolean) => Promise<any>, file: string | File): Promise<void>;
checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<boolean>;
removeFile(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<void>;
}

View File

@ -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;
}

14
lib/types/Upload.d.ts vendored Normal file
View File

@ -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;
};

2
lib/types/Upload.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -16,11 +16,10 @@ export default interface Uploader<ED extends EntityDict & BaseEntityDict> {
* @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<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
file: string | File) => Promise<any>, file: string | File) => Promise<void>;
autoInform?: boolean) => Promise<any>, file: string | File) => Promise<void>;
/**
* upload是否成功不确定的文件OSS发起主动确认
* @param extraFile

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -6,11 +6,10 @@ import { OpSchema } from '../../oak-app-domain/ExtraFile/Schema';
export default class Qiniu<ED extends EntityDict & BaseEntityDict> implements Uploader<ED> {
name: string;
formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<void>;
upload(extraFile: OpSchema, uploadFn: (name: string, // 文件的part name
upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name
uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
file: string | File) => Promise<any>, file: string | File): Promise<void>;
autoInform?: boolean) => Promise<any>, file: string | File): Promise<void>;
checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<boolean>;
removeFile(extraFile: OpSchema, context: BackendRuntimeContext<ED>): Promise<void>;
}

View File

@ -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) {

View File

@ -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';

View File

@ -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';
// 请求链接获取标题,发布时间,图片等信息

View File

@ -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';

15
src/types/Upload.ts Normal file
View File

@ -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;
};

View File

@ -7,41 +7,54 @@ export default interface Uploader<ED extends EntityDict & BaseEntityDict> {
/**
* extrafile生成之前
* @param extraFileextraFile数据
* @param extraFileextraFile数据
* @param context
* @returns
* @returns
*/
formUploadMeta: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext<ED>) => Promise<void>;
formUploadMeta: (
extraFile: EntityDict['extraFile']['OpSchema'],
context: BackendRuntimeContext<ED>
) => Promise<void>;
/**
* 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<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
upload: (
extraFile: EntityDict['extraFile']['OpSchema'],
uploadFn: (
file: File | string,
name: string, // 文件的part name
uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean // 上传成功是否会自动通知server若不会则需要前台显式通知
) => Promise<any>,
file: string | File
) => Promise<any>, file: string | File) => Promise<void>,
) => Promise<void>;
// 前端上传时对回调的处理
/**
* upload是否成功不确定的文件OSS发起主动确认
* @param extraFile
* @param extraFile
* @returns OSS成功
*/
checkWhetherSuccess: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext<ED>) => Promise<boolean>;
checkWhetherSuccess: (
extraFile: EntityDict['extraFile']['OpSchema'],
context: BackendRuntimeContext<ED>
) => Promise<boolean>;
/**
* OSS发起删除命令
* @param extraFile
* @param context
* @returns
* @param extraFile
* @param context
* @returns
*/
removeFile: (extraFile: EntityDict['extraFile']['OpSchema'], context: BackendRuntimeContext<ED>) => Promise<void>;
removeFile: (
extraFile: EntityDict['extraFile']['OpSchema'],
context: BackendRuntimeContext<ED>
) => Promise<void>;
}

View File

@ -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, '-');
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<ED extends EntityDict & BaseEntityDict> implements Uploader<ED> {
name = 'qiniu';
async formUploadMeta(extraFile: OpSchema, context: BackendRuntimeContext<ED>) {
const { origin, objectId, extension, entity, bucket } =
extraFile;
async formUploadMeta(
extraFile: OpSchema,
context: BackendRuntimeContext<ED>
) {
const { origin, objectId, extension, entity, bucket } = extraFile;
// 构造文件上传所需的key
const key = `${entity ? entity + '/' : ''}${objectId}${extension ? '.' + extension : ''}`;
const {
instance,
config,
} = await getConfig<ED, BackendRuntimeContext<ED>>(context, 'Cos', 'qiniu');
const key = `${entity ? entity + '/' : ''}${objectId}${
extension ? '.' + extension : ''
}`;
const { instance, config } = await getConfig<
ED,
BackendRuntimeContext<ED>
>(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<string, any>, // 上传的其它part参数
autoInform: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
file: string | File,
) => Promise<any>, file: string | File) {
async upload(
extraFile: OpSchema,
uploadFn: (
file: File | string,
name: string, // 文件的part name
uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean // 上传成功是否会自动通知server若不会则需要前台显式通知
) => Promise<any>,
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<ED extends EntityDict & BaseEntityDict> implements Up
throw new Error('图片上传失败');
}
async checkWhetherSuccess(extraFile: OpSchema, context: BackendRuntimeContext<ED>) {
async checkWhetherSuccess(
extraFile: OpSchema,
context: BackendRuntimeContext<ED>
) {
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<ED>) {
const { bucket, uploadMeta } = extraFile;
}
};