fix upload

This commit is contained in:
Wang Kejun 2023-09-13 15:42:16 +08:00
parent f7c71d9398
commit fb1dbc8129
4 changed files with 73 additions and 79 deletions

View File

@ -24,7 +24,7 @@ import {
OakUserException, OakUserException,
OakUserUnpermittedException, OakUserUnpermittedException,
} from 'oak-domain/lib/types'; } from 'oak-domain/lib/types';
import { composeFileUrl, decomposeFileUrl } from '../utils/extraFile'; import { composeFileUrl } from '../utils/extraFile';
import { import {
OakChangeLoginWayException, OakChangeLoginWayException,
OakDistinguishUserException, OakDistinguishUserException,
@ -740,16 +740,18 @@ async function setUserInfoFromWechat<
{ {
id: await generateNewIdAsync(), id: await generateNewIdAsync(),
action: 'create', action: 'create',
data: Object.assign( data: Object.assign({
{
id: await generateNewIdAsync(), id: await generateNewIdAsync(),
tag1: 'avatar', tag1: 'avatar',
entity: 'user', entity: 'user',
entityId: user.id, entityId: user.id,
objectId: await generateNewIdAsync(), objectId: await generateNewIdAsync(),
}, origin: 'unknown',
decomposeFileUrl(avatar) extra1: avatar,
), type: 'image',
filename: '',
bucket: '',
}),
}, },
]; ];
if (extraFile$entity!.length > 0) { if (extraFile$entity!.length > 0) {

View File

@ -10,7 +10,7 @@ import { Config, Origin } from '../types/Config';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext'; import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { Application } from './application' import { Application } from './application'
import { composeFileUrl, bytesToSize } from '../utils/extraFile' import { composeFileUrl, bytesToSize } from '../utils/extraFile';
import { assert } from 'oak-domain/lib/utils/assert'; import { assert } from 'oak-domain/lib/utils/assert';
import UploaderDict from '../utils/uploader'; import UploaderDict from '../utils/uploader';
import { OpSchema } from '../oak-app-domain/ExtraFile/Schema'; import { OpSchema } from '../oak-app-domain/ExtraFile/Schema';
@ -28,7 +28,7 @@ export class ExtraFile<
constructor( constructor(
cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>,
application: Application<ED, Cxt, FrontCxt, AD>, application: Application<ED, Cxt, FrontCxt, AD>,
locales: Locales<ED, Cxt, FrontCxt, AD>, locales: Locales<ED, Cxt, FrontCxt, AD>
) { ) {
super(); super();
this.cache = cache; this.cache = cache;
@ -36,38 +36,37 @@ export class ExtraFile<
this.locales = locales; this.locales = locales;
} }
// async getUploadInfo(extraFile: EntityDict['extraFile']['CreateSingle']['data']) { async createAndUpload(
// // const { origin, extra1, filename, objectId, extension, entity } = extraFile: EntityDict['extraFile']['CreateSingle']['data']
// // extraFile; ) {
// // 构造文件上传所需的key await this.cache.operate('extraFile', {
// // const key = `${entity ? entity + '/' : ''}${objectId}${extension ? '.' + extension : ''}`;
// assert(origin && origin !== 'unknown');
// const uploadInfo = await this.cache.exec('getUploadInfo', {
// extraFile
// });
// return uploadInfo;
// }
async createAndUpload(extraFile: EntityDict['extraFile']['CreateSingle']['data']) {
await this.cache.operate(
'extraFile',
{
action: 'create', action: 'create',
data: Object.assign({}, extraFile, { extra1: null }), data: Object.assign({}, extraFile, { extra1: null }),
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation'] } as EntityDict['extraFile']['Operation']);
) const result = await this.upload(
const result = await this.upload(Object.assign({}, extraFile, { extra1: null }), extraFile.extra1!); Object.assign({}, extraFile, { extra1: null }),
extraFile.extra1!
);
const application = this.application.getApplication(); const application = this.application.getApplication();
const config = const config =
application?.system?.config || application?.system?.config ||
application?.system?.platform?.config; application?.system?.platform?.config;
const { bucket } = result; const { bucket } = result;
return { return {
url: this.getUrl(Object.assign({}, extraFile, { extra1: null }) as EntityDict['extraFile']['OpSchema']), url: this.getUrl(
Object.assign({}, extraFile, {
extra1: null,
}) as EntityDict['extraFile']['OpSchema']
),
bucket, bucket,
};
} }
}
async upload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File) { async upload(
extraFile: EntityDict['extraFile']['CreateSingle']['data'],
file: string | File
) {
const { id, origin } = extraFile; const { id, origin } = extraFile;
assert(origin, '未设置上传方式'); assert(origin, '未设置上传方式');
const [extraFileData] = this.cache.get('extraFile', { const [extraFileData] = this.cache.get('extraFile', {
@ -97,10 +96,12 @@ export class ExtraFile<
}); });
const up = new Upload(); const up = new Upload();
try { try {
const uploadInfo = UploaderDict[origin!].upload(extraFileData as OpSchema, up.uploadFile, file); await UploaderDict[origin!].upload(
await this.cache.operate( extraFileData as OpSchema,
'extraFile', up.uploadFile,
{ file
);
await this.cache.operate('extraFile', {
action: 'update', action: 'update',
data: { data: {
uploadState: 'success', uploadState: 'success',
@ -109,13 +110,10 @@ export class ExtraFile<
id, id,
}, },
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation'] } as EntityDict['extraFile']['Operation']);
)
return Object.assign(extraFileData, { uploadState: 'success' }); return Object.assign(extraFileData, { uploadState: 'success' });
} catch (err) { } catch (err) {
await this.cache.operate( await this.cache.operate('extraFile', {
'extraFile',
{
action: 'update', action: 'update',
data: { data: {
uploadState: 'failed', uploadState: 'failed',
@ -124,14 +122,16 @@ export class ExtraFile<
id, id,
}, },
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation'] } as EntityDict['extraFile']['Operation']);
);
throw err; throw err;
} }
} }
getUrl( getUrl(
extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, extraFile?:
| EntityDict['extraFile']['OpSchema']
| EntityDict['extraFile']['Schema']
| null,
style?: string style?: string
) { ) {
if (!extraFile) { if (!extraFile) {
@ -161,8 +161,10 @@ export class ExtraFile<
async getBridgeUrl(url: string) { async getBridgeUrl(url: string) {
const { result } = await this.cache.exec('crossBridge', { const { result } = await this.cache.exec('crossBridge', {
url, url,
}) });
const blob = new Blob([result as unknown as BlobPart], { type: 'image/png' }); const blob = new Blob([result as unknown as BlobPart], {
type: 'image/png',
});
return URL.createObjectURL(blob); return URL.createObjectURL(blob);
} }

View File

@ -14,7 +14,7 @@ export * from './types/Page';
export * from './types/Message'; export * from './types/Message';
export * from './types/RuntimeCxt'; export * from './types/RuntimeCxt';
export { composeFileUrl, decomposeFileUrl } from './utils/extraFile'; export { composeFileUrl } from './utils/extraFile';
export { getLivestream, getPlayBackUrl, getStreamObj } from './utils/livestream'; export { getLivestream, getPlayBackUrl, getStreamObj } from './utils/livestream';
export { BackendRuntimeContext } from './context/BackendRuntimeContext'; export { BackendRuntimeContext } from './context/BackendRuntimeContext';
export { export {

View File

@ -42,16 +42,6 @@ export function composeFileUrl(
return ''; return '';
} }
export function decomposeFileUrl(url: string): Pick<ExtraFile, 'bucket' | 'filename' | 'origin' | 'type' | 'extra1'> {
return {
origin: 'unknown',
extra1: url,
type: 'file',
filename: '',
bucket: '',
};
}
//获取file文件url //获取file文件url
export function getFileURL(file: File) { export function getFileURL(file: File) {
let getUrl = null; let getUrl = null;