From 2fd4b31f681fa8dfe5db4fd248f98ce7cb440419 Mon Sep 17 00:00:00 2001 From: wenjiarui Date: Fri, 13 Oct 2023 19:11:02 +0800 Subject: [PATCH] autoUplaodd --- es/components/extraFile/upload/index.js | 11 +--- es/features/extraFile2.d.ts | 1 + es/features/extraFile2.js | 53 +++++++++++++++++++ es/utils/cos/qiniu.js | 15 ++++-- lib/components/extraFile/upload/index.js | 11 +--- lib/features/extraFile2.d.ts | 1 + lib/features/extraFile2.js | 53 +++++++++++++++++++ lib/utils/cos/qiniu.js | 15 ++++-- src/components/extraFile/upload/index.ts | 11 +--- src/features/extraFile2.ts | 65 ++++++++++++++++++++++-- src/utils/cos/qiniu.ts | 25 +++++---- 11 files changed, 208 insertions(+), 53 deletions(-) diff --git a/es/components/extraFile/upload/index.js b/es/components/extraFile/upload/index.js index 855b4f273..da0db8847 100644 --- a/es/components/extraFile/upload/index.js +++ b/es/components/extraFile/upload/index.js @@ -1,4 +1,3 @@ -import { getConfig } from '../../../utils/getContextConfig'; import { generateNewId } from 'oak-domain'; ; export default OakComponent({ @@ -137,18 +136,10 @@ export default OakComponent({ const extension = name.substring(name.lastIndexOf('.') + 1); const filename = name.substring(0, name.lastIndexOf('.')); const { files } = this.state; - let bucket2 = bucket; - if (origin === 'qiniu' && !bucket2) { - const context = this.features.cache.begin(); - const { config } = getConfig(context, 'Cos', origin); - this.features.cache.commit(); - const { defaultBucket } = config; - bucket2 = defaultBucket; - } const applicationId = this.features.application.getApplicationId(); const id = this.addItem({ applicationId, - bucket: bucket2, + bucket, origin, type, tag1, diff --git a/es/features/extraFile2.d.ts b/es/features/extraFile2.d.ts index 25c6652e7..a14d91ea8 100644 --- a/es/features/extraFile2.d.ts +++ b/es/features/extraFile2.d.ts @@ -24,4 +24,5 @@ export declare class ExtraFile2; } diff --git a/es/features/extraFile2.js b/es/features/extraFile2.js index 8954c4681..f96b41b9b 100644 --- a/es/features/extraFile2.js +++ b/es/features/extraFile2.js @@ -4,6 +4,7 @@ import { bytesToSize, getFileURL } from '../utils/extraFile'; import { assert } from 'oak-domain/lib/utils/assert'; import { getCos } from '../utils/cos'; import { unset } from 'oak-domain/lib/utils/lodash'; +import { generateNewId, generateNewIdAsync } from 'oak-domain'; export class ExtraFile2 extends Feature { cache; application; @@ -123,4 +124,56 @@ export class ExtraFile2 extends Feature { formatBytes(size) { return bytesToSize(size); } + async autoUpload(extraFile, file) { + const extraFileId = extraFile.id || generateNewId(); + await this.cache.operate('extraFile', { + action: 'create', + data: Object.assign(extraFile, { id: extraFileId, applicationId: this.application.getApplicationId() }), + id: await generateNewIdAsync(), + }); + const [newExtraFile] = this.cache.get('extraFile', { + data: { + id: 1, + origin: 1, + type: 1, + bucket: 1, + objectId: 1, + tag1: 1, + tag2: 1, + filename: 1, + md5: 1, + entity: 1, + entityId: 1, + extra1: 1, + extension: 1, + size: 1, + sort: 1, + fileType: 1, + isBridge: 1, + uploadState: 1, + uploadMeta: 1, + }, + filter: { + id: extraFileId + } + }); + console.log(newExtraFile); + const up = new Upload(); + try { + const cos = getCos(newExtraFile.origin); + await cos.upload(newExtraFile, up.uploadFile, file); + return this.getUrl(newExtraFile); + } + catch (err) { + await this.cache.operate('extraFile', { + action: 'remove', + data: {}, + filter: { + id: extraFileId + }, + id: await generateNewIdAsync(), + }); + throw err; + } + } } diff --git a/es/utils/cos/qiniu.js b/es/utils/cos/qiniu.js index 65fb1162a..ebb736f45 100644 --- a/es/utils/cos/qiniu.js +++ b/es/utils/cos/qiniu.js @@ -19,12 +19,17 @@ export default class Qiniu { const key = this.formKey(extraFile); const { instance, config } = getConfig(context, 'Cos', 'qiniu'); const { buckets } = config; - assert(bucket); - const b = buckets.find(ele => ele.name === bucket); - assert(b, `${bucket}不是一个有效的桶配置`); + let bucket2 = bucket; + if (!bucket2) { + const { defaultBucket } = config; + bucket2 = defaultBucket; + } + assert(bucket2); + const b = buckets.find(ele => ele.name === bucket2); + assert(b, `${bucket2}不是一个有效的桶配置`); Object.assign(extraFile, { - bucket, - uploadMeta: instance.getKodoUploadInfo(bucket, b.zone, key), + bucket: bucket2, + uploadMeta: instance.getKodoUploadInfo(bucket2, b.zone, key), }); } async upload(extraFile, uploadFn, file) { diff --git a/lib/components/extraFile/upload/index.js b/lib/components/extraFile/upload/index.js index a3caea37f..89f5172e8 100644 --- a/lib/components/extraFile/upload/index.js +++ b/lib/components/extraFile/upload/index.js @@ -1,6 +1,5 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const getContextConfig_1 = require("../../../utils/getContextConfig"); const oak_domain_1 = require("oak-domain"); ; exports.default = OakComponent({ @@ -139,18 +138,10 @@ exports.default = OakComponent({ const extension = name.substring(name.lastIndexOf('.') + 1); const filename = name.substring(0, name.lastIndexOf('.')); const { files } = this.state; - let bucket2 = bucket; - if (origin === 'qiniu' && !bucket2) { - const context = this.features.cache.begin(); - const { config } = (0, getContextConfig_1.getConfig)(context, 'Cos', origin); - this.features.cache.commit(); - const { defaultBucket } = config; - bucket2 = defaultBucket; - } const applicationId = this.features.application.getApplicationId(); const id = this.addItem({ applicationId, - bucket: bucket2, + bucket, origin, type, tag1, diff --git a/lib/features/extraFile2.d.ts b/lib/features/extraFile2.d.ts index 25c6652e7..a14d91ea8 100644 --- a/lib/features/extraFile2.d.ts +++ b/lib/features/extraFile2.d.ts @@ -24,4 +24,5 @@ export declare class ExtraFile2; } diff --git a/lib/features/extraFile2.js b/lib/features/extraFile2.js index 4ff8a3f24..1bc21e186 100644 --- a/lib/features/extraFile2.js +++ b/lib/features/extraFile2.js @@ -7,6 +7,7 @@ const extraFile_1 = require("../utils/extraFile"); const assert_1 = require("oak-domain/lib/utils/assert"); const cos_1 = require("../utils/cos"); const lodash_1 = require("oak-domain/lib/utils/lodash"); +const oak_domain_1 = require("oak-domain"); class ExtraFile2 extends oak_frontend_base_1.Feature { cache; application; @@ -126,5 +127,57 @@ class ExtraFile2 extends oak_frontend_base_1.Feature { formatBytes(size) { return (0, extraFile_1.bytesToSize)(size); } + async autoUpload(extraFile, file) { + const extraFileId = extraFile.id || (0, oak_domain_1.generateNewId)(); + await this.cache.operate('extraFile', { + action: 'create', + data: Object.assign(extraFile, { id: extraFileId, applicationId: this.application.getApplicationId() }), + id: await (0, oak_domain_1.generateNewIdAsync)(), + }); + const [newExtraFile] = this.cache.get('extraFile', { + data: { + id: 1, + origin: 1, + type: 1, + bucket: 1, + objectId: 1, + tag1: 1, + tag2: 1, + filename: 1, + md5: 1, + entity: 1, + entityId: 1, + extra1: 1, + extension: 1, + size: 1, + sort: 1, + fileType: 1, + isBridge: 1, + uploadState: 1, + uploadMeta: 1, + }, + filter: { + id: extraFileId + } + }); + console.log(newExtraFile); + const up = new upload_1.Upload(); + try { + const cos = (0, cos_1.getCos)(newExtraFile.origin); + await cos.upload(newExtraFile, up.uploadFile, file); + return this.getUrl(newExtraFile); + } + catch (err) { + await this.cache.operate('extraFile', { + action: 'remove', + data: {}, + filter: { + id: extraFileId + }, + id: await (0, oak_domain_1.generateNewIdAsync)(), + }); + throw err; + } + } } exports.ExtraFile2 = ExtraFile2; diff --git a/lib/utils/cos/qiniu.js b/lib/utils/cos/qiniu.js index ac2eb251f..243a66098 100644 --- a/lib/utils/cos/qiniu.js +++ b/lib/utils/cos/qiniu.js @@ -21,12 +21,17 @@ class Qiniu { const key = this.formKey(extraFile); const { instance, config } = (0, getContextConfig_1.getConfig)(context, 'Cos', 'qiniu'); const { buckets } = config; - (0, assert_1.assert)(bucket); - const b = buckets.find(ele => ele.name === bucket); - (0, assert_1.assert)(b, `${bucket}不是一个有效的桶配置`); + let bucket2 = bucket; + if (!bucket2) { + const { defaultBucket } = config; + bucket2 = defaultBucket; + } + (0, assert_1.assert)(bucket2); + const b = buckets.find(ele => ele.name === bucket2); + (0, assert_1.assert)(b, `${bucket2}不是一个有效的桶配置`); Object.assign(extraFile, { - bucket, - uploadMeta: instance.getKodoUploadInfo(bucket, b.zone, key), + bucket: bucket2, + uploadMeta: instance.getKodoUploadInfo(bucket2, b.zone, key), }); } async upload(extraFile, uploadFn, file) { diff --git a/src/components/extraFile/upload/index.ts b/src/components/extraFile/upload/index.ts index 06daf5caa..5b8350a52 100644 --- a/src/components/extraFile/upload/index.ts +++ b/src/components/extraFile/upload/index.ts @@ -195,20 +195,11 @@ export default OakComponent({ const extension = name.substring(name.lastIndexOf('.') + 1); const filename = name.substring(0, name.lastIndexOf('.')); const { files } = this.state; - let bucket2 = bucket; - if (origin === 'qiniu' && !bucket2) { - const context = this.features.cache.begin(); - const { config } = getConfig(context, 'Cos', origin); - this.features.cache.commit(); - - const { defaultBucket } = config as QiniuCosConfig; - bucket2 = defaultBucket!; - } const applicationId = this.features.application.getApplicationId(); const id = this.addItem({ applicationId, - bucket: bucket2, + bucket, origin, type, tag1, diff --git a/src/features/extraFile2.ts b/src/features/extraFile2.ts index f528c2496..eed823c80 100644 --- a/src/features/extraFile2.ts +++ b/src/features/extraFile2.ts @@ -13,7 +13,7 @@ import { assert } from 'oak-domain/lib/utils/assert'; import { getCos } from '../utils/cos'; import { OpSchema } from '../oak-app-domain/ExtraFile/Schema'; import { unset } from 'oak-domain/lib/utils/lodash'; -import { generateNewIdAsync } from 'oak-domain'; +import { generateNewId, generateNewIdAsync } from 'oak-domain'; export type FileState = 'local' | 'uploading' | 'uploaded' | 'failed'; @@ -22,7 +22,7 @@ export class ExtraFile2< Cxt extends BackendRuntimeContext, FrontCxt extends FrontendRuntimeContext, AD extends AspectDict & CommonAspectDict -> extends Feature { +> extends Feature { private cache: Cache>; private application: Application; private locales: Locales; @@ -93,7 +93,7 @@ export class ExtraFile2< assert(['local', 'failed'].includes(state)); item.state = 'uploading'; item.percentage = 0; - + const up = new Upload(); try { const cos = getCos(extraFile.origin!); @@ -175,4 +175,63 @@ export class ExtraFile2< formatBytes(size: number) { return bytesToSize(size); } + + async autoUpload(extraFile: EntityDict['extraFile']['OpSchema'], file: File | string) { + const extraFileId = extraFile.id || generateNewId(); + await this.cache.operate('extraFile', { + action: 'create', + data: Object.assign(extraFile, { id: extraFileId, applicationId: this.application.getApplicationId() }), + id: await generateNewIdAsync(), + } as EntityDict['extraFile']['Operation']); + const [newExtraFile] = this.cache.get( + 'extraFile', + { + data: { + id: 1, + origin: 1, + type: 1, + bucket: 1, + objectId: 1, + tag1: 1, + tag2: 1, + filename: 1, + md5: 1, + entity: 1, + entityId: 1, + extra1: 1, + extension: 1, + size: 1, + sort: 1, + fileType: 1, + isBridge: 1, + uploadState: 1, + uploadMeta: 1, + }, + filter: { + id: extraFileId + } + } + ); + console.log(newExtraFile); + const up = new Upload(); + try { + const cos = getCos(newExtraFile.origin!); + await cos.upload( + newExtraFile as OpSchema, + up.uploadFile, + file + ); + return this.getUrl(newExtraFile as EntityDict['extraFile']['Schema']); + } catch (err) { + await this.cache.operate('extraFile', { + action: 'remove', + data: {}, + filter: { + id: extraFileId + }, + id: await generateNewIdAsync(), + } as EntityDict['extraFile']['Operation']); + throw err; + } + } } \ No newline at end of file diff --git a/src/utils/cos/qiniu.ts b/src/utils/cos/qiniu.ts index 3d1b18d30..c4957aa0f 100644 --- a/src/utils/cos/qiniu.ts +++ b/src/utils/cos/qiniu.ts @@ -15,14 +15,14 @@ const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; export default class Qiniu implements Cos { name = 'qiniu'; - + autoInform(): boolean { return false; } private formKey(extraFile: OpSchema) { const { id, extension, objectId } = extraFile; - + assert(objectId); return `extraFile/${objectId}${extension ? '.' + extension : ''}`; } @@ -37,13 +37,18 @@ export default class Qiniu implements Cos { const { instance, config } = getConfig(context, 'Cos', 'qiniu'); const { buckets } = config as QiniuCosConfig; - assert(bucket); - const b = buckets.find(ele => ele.name === bucket); - assert(b, `${bucket}不是一个有效的桶配置`); + let bucket2 = bucket; + if (!bucket2) { + const { defaultBucket } = config as QiniuCosConfig; + bucket2 = defaultBucket!; + } + assert(bucket2); + const b = buckets.find(ele => ele.name === bucket2); + assert(b, `${bucket2}不是一个有效的桶配置`); Object.assign(extraFile, { - bucket, + bucket: bucket2, uploadMeta: (instance as QiniuCloudInstance).getKodoUploadInfo( - bucket, + bucket2, b.zone, key ), @@ -116,7 +121,7 @@ export default class Qiniu implements Cos { ) { const key = this.formKey(extraFile); const { instance, config } = getConfig(context, 'Cos', 'qiniu'); - + // web环境下访问不了七牛接口,用mockData过 const mockData = process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined; @@ -125,7 +130,7 @@ export default class Qiniu implements Cos { try { const result = await (instance as QiniuCloudInstance).getKodoFileStat(extraFile.bucket!, b.zone, key, mockData); - + const { fsize } = result; return fsize > 0; } @@ -145,7 +150,7 @@ export default class Qiniu implements Cos { async removeFile(extraFile: OpSchema, context: BRC) { const key = this.formKey(extraFile); const { instance, config } = getConfig(context, 'Cos', 'qiniu'); - + // web环境下访问不了七牛接口,用mockData过 const mockData = process.env.OAK_PLATFORM === 'web' ? true : undefined; const b = (config as QiniuCosConfig).buckets.find(ele => ele.name === extraFile.bucket);