autoUplaodd
This commit is contained in:
parent
5fec957857
commit
2fd4b31f68
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -24,4 +24,5 @@ export declare class ExtraFile2<ED extends EntityDict, Cxt extends BackendRuntim
|
|||
} | undefined;
|
||||
getFileName(extraFile: EntityDict['extraFile']['OpSchema']): string;
|
||||
formatBytes(size: number): string;
|
||||
autoUpload(extraFile: EntityDict['extraFile']['OpSchema'], file: File | string): Promise<string>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -24,4 +24,5 @@ export declare class ExtraFile2<ED extends EntityDict, Cxt extends BackendRuntim
|
|||
} | undefined;
|
||||
getFileName(extraFile: EntityDict['extraFile']['OpSchema']): string;
|
||||
formatBytes(size: number): string;
|
||||
autoUpload(extraFile: EntityDict['extraFile']['OpSchema'], file: File | string): Promise<string>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<ED>,
|
||||
FrontCxt extends FrontendRuntimeContext<ED, Cxt, AD>,
|
||||
AD extends AspectDict<ED, Cxt> & CommonAspectDict<ED, Cxt>
|
||||
> extends Feature {
|
||||
> extends Feature {
|
||||
private cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>;
|
||||
private application: Application<ED, Cxt, FrontCxt, AD>;
|
||||
private locales: Locales<ED, Cxt, FrontCxt, AD>;
|
||||
|
|
@ -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<ED, Cxt, FrontCxt>(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<ED, Cxt, FrontCxt>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,14 +15,14 @@ const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI';
|
|||
|
||||
export default class Qiniu implements Cos<ED, BRC, FRC> {
|
||||
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<ED, BRC, FRC> {
|
|||
const { instance, config } = getConfig<ED, BRC, FRC>(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<ED, BRC, FRC> {
|
|||
) {
|
||||
const key = this.formKey(extraFile);
|
||||
const { instance, config } = getConfig<ED, BRC, FRC>(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<ED, BRC, FRC> {
|
|||
|
||||
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<ED, BRC, FRC> {
|
|||
async removeFile(extraFile: OpSchema, context: BRC) {
|
||||
const key = this.formKey(extraFile);
|
||||
const { instance, config } = getConfig<ED, BRC, FRC>(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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue