Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: add maximum allowed size config of uploaded images (#6865)

wpxp12345646e13092c0a9
diff --git a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
index 0568173342..9171adf92d 100644
--- a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
+++ b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
@@ -25,7 +25,7 @@ import { MessageType } from '@univerjs/design';
import { DocSelectionManagerService, DocSkeletonManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { IDocDrawingService } from '@univerjs/docs-drawing';
import { docDrawingPositionToTransform, DocSelectionRenderService } from '@univerjs/docs-ui';
-import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, getDrawingShapeKeyByDrawingSearch, getImageSize, IDrawingManagerService, IImageIoService, ImageUploadStatusType } from '@univerjs/drawing';
+import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, getDrawingImageAllowSize, getDrawingShapeKeyByDrawingSearch, getImageSize, IDrawingManagerService, IImageIoService, ImageUploadStatusType } from '@univerjs/drawing';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
import { ILocalFileService, IMessageService } from '@univerjs/ui';
@@ -100,7 +100,7 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRen
switch (type) {
case ImageUploadStatusType.ERROR_EXCEED_SIZE:
- content = this._localeService.t('update-status.exceedMaxSize', String(DRAWING_IMAGE_ALLOW_SIZE / (1024 * 1024)));
+ content = this._localeService.t('update-status.exceedMaxSize', String(getDrawingImageAllowSize() / (1024 * 1024)));
break;
case ImageUploadStatusType.ERROR_IMAGE_TYPE:
content = this._localeService.t('update-status.invalidImageType');
diff --git a/packages/drawing/src/basics/config.ts b/packages/drawing/src/basics/config.ts
index 0902635b96..a812a196fc 100644
--- a/packages/drawing/src/basics/config.ts
+++ b/packages/drawing/src/basics/config.ts
@@ -20,6 +20,14 @@ export const DRAWING_IMAGE_HEIGHT_LIMIT = 500;
export const DRAWING_IMAGE_COUNT_LIMIT = 10;
-export const DRAWING_IMAGE_ALLOW_SIZE = 5 * 1024 * 1024;
+let DRAWING_IMAGE_ALLOW_SIZE = 5 * 1024 * 1024;
+
+export function setDrawingImageAllowSize(size: number): void {
+ DRAWING_IMAGE_ALLOW_SIZE = size;
+}
+
+export function getDrawingImageAllowSize(): number {
+ return DRAWING_IMAGE_ALLOW_SIZE;
+}
export const DRAWING_IMAGE_ALLOW_IMAGE_LIST = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/bmp'];
diff --git a/packages/drawing/src/config/config.ts b/packages/drawing/src/config/config.ts
index dddef50874..1c909b1f21 100644
--- a/packages/drawing/src/config/config.ts
+++ b/packages/drawing/src/config/config.ts
@@ -22,6 +22,11 @@ export const configSymbol = Symbol(DRAWING_PLUGIN_CONFIG_KEY);
export interface IUniverDrawingConfig {
override?: DependencyOverride;
+ /**
+ * The maximum allowed size of uploaded images in bytes.
+ * @default 5 * 1024 * 1024 (5MB)
+ */
+ allowImageSize?: number;
}
export const defaultPluginConfig: IUniverDrawingConfig = {};
diff --git a/packages/drawing/src/index.ts b/packages/drawing/src/index.ts
index 11c61689df..20aa5b1932 100644
--- a/packages/drawing/src/index.ts
+++ b/packages/drawing/src/index.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-export { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT } from './basics/config';
+export { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, getDrawingImageAllowSize } from './basics/config';
export { SetDrawingSelectedOperation } from './commands/operations/set-drawing-selected.operation';
export type { IUniverDrawingConfig } from './config/config';
export type { IDocFloatDomData, IDocFloatDomDataBase, IImageData } from './models/image-model-interface';
diff --git a/packages/drawing/src/plugin.ts b/packages/drawing/src/plugin.ts
index 0d7abbef97..e41660dc26 100644
--- a/packages/drawing/src/plugin.ts
+++ b/packages/drawing/src/plugin.ts
@@ -18,6 +18,7 @@ import type { Dependency } from '@univerjs/core';
import type { IUniverDrawingConfig } from './config/config';
import { ICommandService, IConfigService, Inject, Injector, IURLImageService, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
import pkg from '../package.json';
+import { setDrawingImageAllowSize } from './basics/config';
import { SetDrawingSelectedOperation } from './commands/operations/set-drawing-selected.operation';
import { defaultPluginConfig, DRAWING_PLUGIN_CONFIG_KEY } from './config/config';
import { DrawingManagerService } from './services/drawing-manager-impl.service';
@@ -46,6 +47,10 @@ export class UniverDrawingPlugin extends Plugin {
this._config
);
this._configService.setConfig(DRAWING_PLUGIN_CONFIG_KEY, rest);
+
+ if (rest.allowImageSize !== undefined) {
+ setDrawingImageAllowSize(rest.allowImageSize);
+ }
}
override onStarting(): void {
diff --git a/packages/drawing/src/services/__tests__/image-io-impl.service.spec.ts b/packages/drawing/src/services/__tests__/image-io-impl.service.spec.ts
index 9ae3217caa..736fb241c3 100644
--- a/packages/drawing/src/services/__tests__/image-io-impl.service.spec.ts
+++ b/packages/drawing/src/services/__tests__/image-io-impl.service.spec.ts
@@ -16,7 +16,7 @@
import { ImageSourceType, ImageUploadStatusType } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { DRAWING_IMAGE_ALLOW_SIZE } from '../../basics/config';
+import { getDrawingImageAllowSize } from '../../basics/config';
import { ImageIoService } from '../image-io-impl.service';
type MockLoadEvent = ProgressEvent<FileReader> & {
@@ -107,7 +107,7 @@ describe('ImageIoService', () => {
service.setWaitCount(1);
await expect(
- service.saveImage(new File([new Uint8Array(DRAWING_IMAGE_ALLOW_SIZE + 1)], 'big.png', { type: 'image/png' }))
+ service.saveImage(new File([new Uint8Array(getDrawingImageAllowSize() + 1)], 'big.png', { type: 'image/png' }))
).rejects.toThrow(ImageUploadStatusType.ERROR_EXCEED_SIZE);
expect(counts).toContain(0);
diff --git a/packages/drawing/src/services/image-io-impl.service.ts b/packages/drawing/src/services/image-io-impl.service.ts
index cb4a1eda18..8f5b76d039 100644
--- a/packages/drawing/src/services/image-io-impl.service.ts
+++ b/packages/drawing/src/services/image-io-impl.service.ts
@@ -19,7 +19,7 @@ import type { Observable } from 'rxjs';
import type { IImageIoService, IImageIoServiceParam } from './image-io.service';
import { generateRandomId } from '@univerjs/core';
import { Subject } from 'rxjs';
-import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE } from '../basics/config';
+import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, getDrawingImageAllowSize } from '../basics/config';
import { ImageSourceType, ImageUploadStatusType } from './image-io.service';
export class ImageIoService implements IImageIoService {
@@ -61,7 +61,7 @@ export class ImageIoService implements IImageIoService {
this._decreaseWaiting();
return;
}
- if (imageFile.size > DRAWING_IMAGE_ALLOW_SIZE) {
+ if (imageFile.size > getDrawingImageAllowSize()) {
reject(new Error(ImageUploadStatusType.ERROR_EXCEED_SIZE));
this._decreaseWaiting();
return;
diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
index 2e14eddd24..dfaa019cf0 100644
--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
@@ -43,10 +43,10 @@ import { MessageType } from '@univerjs/design';
import { docDrawingPositionToTransform } from '@univerjs/docs-ui';
import {
DRAWING_IMAGE_ALLOW_IMAGE_LIST,
- DRAWING_IMAGE_ALLOW_SIZE,
DRAWING_IMAGE_COUNT_LIMIT,
DRAWING_IMAGE_HEIGHT_LIMIT,
DRAWING_IMAGE_WIDTH_LIMIT,
+ getDrawingImageAllowSize,
getImageSize,
IDrawingManagerService,
IImageIoService,
@@ -202,7 +202,7 @@ export class SheetDrawingUpdateController extends Disposable implements IRenderM
if (type === ImageUploadStatusType.ERROR_EXCEED_SIZE) {
this._messageService.show({
type: MessageType.Error,
- content: this._localeService.t('update-status.exceedMaxSize', String(DRAWING_IMAGE_ALLOW_SIZE / (1024 * 1024))),
+ content: this._localeService.t('update-status.exceedMaxSize', String(getDrawingImageAllowSize() / (1024 * 1024))),
});
} else if (type === ImageUploadStatusType.ERROR_IMAGE_TYPE) {
this._messageService.show({
@@ -274,7 +274,7 @@ export class SheetDrawingUpdateController extends Disposable implements IRenderM
if (type === ImageUploadStatusType.ERROR_EXCEED_SIZE) {
this._messageService.show({
type: MessageType.Error,
- content: this._localeService.t('update-status.exceedMaxSize', String(DRAWING_IMAGE_ALLOW_SIZE / (1024 * 1024))),
+ content: this._localeService.t('update-status.exceedMaxSize', String(getDrawingImageAllowSize() / (1024 * 1024))),
});
} else if (type === ImageUploadStatusType.ERROR_IMAGE_TYPE) {
this._messageService.show({