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

View File

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

View File

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

View File

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