extrafile 增加 getUrl

This commit is contained in:
Wang Kejun 2022-12-02 16:08:55 +08:00
parent 0d13e335dd
commit 8546164412
8 changed files with 53 additions and 24 deletions

View File

@ -6,12 +6,15 @@ import { EntityDict } from '../general-app-domain';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { Cache } from 'oak-frontend-base/lib/features/cache';
import { Application } from './application';
export declare class ExtraFile<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>, FrontCxt extends FrontendRuntimeContext<ED, Cxt, AD>, AD extends AspectDict<ED, Cxt> & CommonAspectDict<ED, Cxt>> extends Feature {
private cache;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>);
private application;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, application: Application<ED, Cxt, FrontCxt, AD>);
private getUploadInfo;
upload(extraFile: DeduceCreateOperationData<EntityDict['extraFile']['OpSchema']>): Promise<{
url: string;
bucket: string;
}>;
getUrl(extraFile?: EntityDict['extraFile']['Schema'], style?: string): string;
}

View File

@ -4,11 +4,13 @@ exports.ExtraFile = void 0;
var tslib_1 = require("tslib");
var Feature_1 = require("oak-frontend-base/lib/types/Feature");
var upload_1 = require("oak-frontend-base/lib/utils/upload");
var extraFile_1 = require("../utils/extraFile");
var ExtraFile = /** @class */ (function (_super) {
tslib_1.__extends(ExtraFile, _super);
function ExtraFile(cache) {
function ExtraFile(cache, application) {
var _this = _super.call(this) || this;
_this.cache = cache;
_this.application = application;
return _this;
}
ExtraFile.prototype.getUploadInfo = function (origin, key) {
@ -54,6 +56,16 @@ var ExtraFile = /** @class */ (function (_super) {
});
});
};
ExtraFile.prototype.getUrl = function (extraFile, style) {
var _a, _b, _c;
if (!extraFile) {
return '';
}
var application = this.application.getApplication();
var config = ((_a = application === null || application === void 0 ? void 0 : application.system) === null || _a === void 0 ? void 0 : _a.config) || ((_c = (_b = application === null || application === void 0 ? void 0 : application.system) === null || _b === void 0 ? void 0 : _b.platform) === null || _c === void 0 ? void 0 : _c.config);
var url = (0, extraFile_1.composeFileUrl)(extraFile, config, style);
return url;
};
return ExtraFile;
}(Feature_1.Feature));
exports.ExtraFile = ExtraFile;

View File

@ -8,7 +8,7 @@ var config_1 = require("./config");
function initialize(basicFeatures, type, domain) {
var application = new application_1.Application(type, domain, basicFeatures.cache, basicFeatures.localStorage);
var token = new token_1.Token(basicFeatures.cache, basicFeatures.localStorage);
var extraFile = new extraFile_1.ExtraFile(basicFeatures.cache);
var extraFile = new extraFile_1.ExtraFile(basicFeatures.cache, application);
var config = new config_1.Config(basicFeatures.cache);
return {
token: token,

View File

@ -1,6 +1,6 @@
import { OpSchema as ExtraFile } from '../general-app-domain/ExtraFile/Schema';
import { Config } from '../types/Config';
export declare function composeFileUrl(extraFile: Pick<ExtraFile, 'type' | 'bucket' | 'filename' | 'origin' | 'extra1' | 'objectId' | 'extension' | 'entity'>, config?: Config, style?: string): any;
export declare function composeFileUrl(extraFile: Pick<ExtraFile, 'type' | 'bucket' | 'filename' | 'origin' | 'extra1' | 'objectId' | 'extension' | 'entity'>, config?: Config, style?: string): string;
export declare function decomposeFileUrl(url: string): Pick<ExtraFile, 'bucket' | 'filename' | 'origin' | 'type' | 'extra1'>;
export declare function getFileURL(file: File): any;
export declare function bytesToSize(sizes: any): any;

View File

@ -2,19 +2,19 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.bytesToSize = exports.getFileURL = exports.decomposeFileUrl = exports.composeFileUrl = void 0;
function composeFileUrl(extraFile, config, style) {
var type = extraFile.type, bucket = extraFile.bucket, filename = extraFile.filename, origin = extraFile.origin, extra1 = extraFile.extra1, objectId = extraFile.objectId, extension = extraFile.extension, entity = extraFile.entity;
var _a = extraFile || {}, type = _a.type, bucket = _a.bucket, filename = _a.filename, origin = _a.origin, extra1 = _a.extra1, objectId = _a.objectId, extension = _a.extension, entity = _a.entity;
if (extra1) {
// 有extra1就用extra1 可能File对象 可能外部链接
if (typeof extra1 === 'string') {
return extra1;
}
if (extra1 instanceof File) {
return getFileURL(extra1);
return getFileURL(extra1) || '';
}
return extra1;
return extra1 || '';
}
if (config && config.Cos) {
var _a = config.Cos[origin], domain = _a.domain, protocol = _a.protocol;
var _b = config.Cos[origin], domain = _b.domain, protocol = _b.protocol;
var protocol2 = protocol;
if (protocol instanceof Array) {
// protocol存在https 说明域名有证书

View File

@ -8,28 +8,31 @@ import { Origin } from '../types/Config';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { Cache } from 'oak-frontend-base/lib/features/cache';
import { Application } from './application'
import { composeFileUrl } from '../utils/extraFile'
export class ExtraFile<
ED extends EntityDict,
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>>;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>) {
private application: Application<ED, Cxt, FrontCxt, AD>;
constructor(
cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>,
application: Application<ED, Cxt, FrontCxt, AD>
) {
super();
this.cache = cache;
this.application = application;
}
private async getUploadInfo(origin: Origin, key?: string) {
const uploadInfo = await this.cache.exec(
'getUploadInfo',
{
origin,
key,
}
);
const uploadInfo = await this.cache.exec('getUploadInfo', {
origin,
key,
});
return uploadInfo;
}
@ -55,4 +58,15 @@ export class ExtraFile<
return result;
}
}
getUrl(extraFile?: EntityDict['extraFile']['Schema'], style?: string) {
if (!extraFile) {
return '';
}
const application = this.application.getApplication();
const config = application?.system?.config || application?.system?.platform?.config;
const url = composeFileUrl(extraFile, config, style);
return url;
}
}

View File

@ -31,7 +31,7 @@ export function initialize<
basicFeatures.cache,
basicFeatures.localStorage
);
const extraFile = new ExtraFile<ED, Cxt, FrontCxt, AD>(basicFeatures.cache);
const extraFile = new ExtraFile<ED, Cxt, FrontCxt, AD>(basicFeatures.cache, application);
const config = new Config<ED, Cxt, FrontCxt, AD>(basicFeatures.cache);
return {
token,

View File

@ -15,18 +15,18 @@ export function composeFileUrl(
>,
config?: Config,
style?: string, //图片样式后缀 比如 七牛云支持url带裁剪后缀
) {
): string {
const { type, bucket, filename, origin, extra1, objectId, extension, entity } =
extraFile;
extraFile || {};
if (extra1) {
// 有extra1就用extra1 可能File对象 可能外部链接
if (typeof extra1 === 'string') {
return extra1;
}
if ((extra1 as any) instanceof File) {
return getFileURL(extra1);
if ((extra1 as File) instanceof File) {
return getFileURL(extra1) || '';
}
return extra1;
return extra1 || '';
}
if (config && config.Cos) {
const { domain, protocol } =