Skip to content

Commit ece22e1

Browse files
committed
fix(editor): get max file size from blob sync engine
1 parent 814dfab commit ece22e1

File tree

13 files changed

+82
-149
lines changed

13 files changed

+82
-149
lines changed

blocksuite/affine/all/src/extensions/migrating-store.ts

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { HighlightSelectionExtension } from '@blocksuite/affine-shared/selection
2525
import {
2626
BlockMetaService,
2727
FeatureFlagService,
28-
FileSizeLimitService,
2928
LinkPreviewerService,
3029
} from '@blocksuite/affine-shared/services';
3130
import {
@@ -86,7 +85,6 @@ const MigratingStoreExtensions: ExtensionType[] = [
8685

8786
FeatureFlagService,
8887
LinkPreviewerService,
89-
FileSizeLimitService,
9088
BlockMetaService,
9189
].flat();
9290

blocksuite/affine/blocks/attachment/src/attachment-block.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import {
1010
type AttachmentBlockModel,
1111
AttachmentBlockStyles,
1212
} from '@blocksuite/affine-model';
13-
import {
14-
FileSizeLimitService,
15-
ThemeProvider,
16-
} from '@blocksuite/affine-shared/services';
13+
import { ThemeProvider } from '@blocksuite/affine-shared/services';
1714
import { humanFileSize } from '@blocksuite/affine-shared/utils';
1815
import { BlockSelection } from '@blocksuite/std';
1916
import { Slice } from '@blocksuite/store';
@@ -43,7 +40,7 @@ export class AttachmentBlockComponent extends CaptionedBlockComponent<Attachment
4340
});
4441

4542
private get _maxFileSize() {
46-
return this.std.store.get(FileSizeLimitService).maxFileSize;
43+
return this.std.store.blobSync.maxFileSize;
4744
}
4845

4946
convertTo = () => {

blocksuite/affine/blocks/attachment/src/embed.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
type ImageBlockProps,
44
MAX_IMAGE_WIDTH,
55
} from '@blocksuite/affine-model';
6-
import { FileSizeLimitService } from '@blocksuite/affine-shared/services';
76
import {
87
readImageSize,
98
transformModel,
@@ -68,7 +67,7 @@ export const AttachmentEmbedProvider = createIdentifier<AttachmentEmbedService>(
6867

6968
export class AttachmentEmbedService extends Extension {
7069
private get _maxFileSize() {
71-
return this.std.store.get(FileSizeLimitService).maxFileSize;
70+
return this.std.store.blobSync.maxFileSize;
7271
}
7372

7473
get keys() {

blocksuite/affine/blocks/attachment/src/utils.ts

+20-34
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import {
88
EMBED_CARD_HEIGHT,
99
EMBED_CARD_WIDTH,
1010
} from '@blocksuite/affine-shared/consts';
11-
import {
12-
FileSizeLimitService,
13-
TelemetryProvider,
14-
} from '@blocksuite/affine-shared/services';
11+
import { TelemetryProvider } from '@blocksuite/affine-shared/services';
1512
import { humanFileSize } from '@blocksuite/affine-shared/utils';
1613
import { Bound, type IVec, Vec } from '@blocksuite/global/gfx';
1714
import type { BlockStdScope } from '@blocksuite/std';
@@ -192,6 +189,21 @@ export async function getFileType(file: File) {
192189
return fileType ? fileType.mime : '';
193190
}
194191

192+
function hasExceeded(
193+
std: BlockStdScope,
194+
files: File[],
195+
maxFileSize = std.store.blobSync.maxFileSize
196+
) {
197+
const exceeded = files.some(file => file.size > maxFileSize);
198+
199+
if (exceeded) {
200+
const size = humanFileSize(maxFileSize, true, 0);
201+
toast(std.host, `You can only upload files less than ${size}`);
202+
}
203+
204+
return exceeded;
205+
}
206+
195207
/**
196208
* Add a new attachment block before / after the specified block.
197209
*/
@@ -202,23 +214,9 @@ export async function addSiblingAttachmentBlocks(
202214
place: 'before' | 'after' = 'after',
203215
isEmbed?: boolean
204216
) {
205-
if (!files.length) {
206-
return;
207-
}
217+
if (!files.length) return;
208218

209-
const maxFileSize = std.store.get(FileSizeLimitService).maxFileSize;
210-
const isSizeExceeded = files.some(file => file.size > maxFileSize);
211-
if (isSizeExceeded) {
212-
toast(
213-
std.host,
214-
`You can only upload files less than ${humanFileSize(
215-
maxFileSize,
216-
true,
217-
0
218-
)}`
219-
);
220-
return;
221-
}
219+
if (hasExceeded(std, files)) return;
222220

223221
const doc = targetModel.doc;
224222
const flavour = AttachmentBlockSchema.model.flavour;
@@ -263,21 +261,9 @@ export async function addAttachments(
263261
): Promise<string[]> {
264262
if (!files.length) return [];
265263

266-
const gfx = std.get(GfxControllerIdentifier);
267-
const maxFileSize = std.store.get(FileSizeLimitService).maxFileSize;
268-
const isSizeExceeded = files.some(file => file.size > maxFileSize);
269-
if (isSizeExceeded) {
270-
toast(
271-
std.host,
272-
`You can only upload files less than ${humanFileSize(
273-
maxFileSize,
274-
true,
275-
0
276-
)}`
277-
);
278-
return [];
279-
}
264+
if (hasExceeded(std, files)) return [];
280265

266+
const gfx = std.get(GfxControllerIdentifier);
281267
let { x, y } = gfx.viewport.center;
282268
if (point) {
283269
let transform = transformPoint ?? true;

blocksuite/affine/blocks/image/src/commands/insert-images.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FileSizeLimitService } from '@blocksuite/affine-shared/services';
21
import { getImageFilesFromLocal } from '@blocksuite/affine-shared/utils';
32
import type { Command } from '@blocksuite/std';
43
import type { BlockModel } from '@blocksuite/store';
@@ -29,15 +28,7 @@ export const insertImagesCommand: Command<
2928
? selectedModels[0]
3029
: selectedModels[selectedModels.length - 1];
3130

32-
const maxFileSize = std.store.get(FileSizeLimitService).maxFileSize;
33-
34-
const result = addSiblingImageBlock(
35-
std.host,
36-
imageFiles,
37-
maxFileSize,
38-
targetModel,
39-
place
40-
);
31+
const result = addSiblingImageBlock(std, imageFiles, targetModel, place);
4132
if (removeEmptyLine && targetModel.text?.length === 0) {
4233
std.store.deleteBlock(targetModel);
4334
}

blocksuite/affine/blocks/image/src/image-service.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { SurfaceBlockModel } from '@blocksuite/affine-block-surface';
22
import { FileDropConfigExtension } from '@blocksuite/affine-components/drop-indicator';
33
import { ImageBlockSchema, MAX_IMAGE_WIDTH } from '@blocksuite/affine-model';
4-
import {
5-
FileSizeLimitService,
6-
TelemetryProvider,
7-
} from '@blocksuite/affine-shared/services';
4+
import { TelemetryProvider } from '@blocksuite/affine-shared/services';
85
import {
96
isInsideEdgelessEditor,
107
matchModels,
@@ -19,16 +16,8 @@ export const ImageDropOption = FileDropConfigExtension({
1916
const imageFiles = files.filter(file => file.type.startsWith('image/'));
2017
if (!imageFiles.length) return false;
2118

22-
const maxFileSize = std.store.get(FileSizeLimitService).maxFileSize;
23-
2419
if (targetModel && !matchModels(targetModel, [SurfaceBlockModel])) {
25-
addSiblingImageBlock(
26-
std.host,
27-
imageFiles,
28-
maxFileSize,
29-
targetModel,
30-
placement
31-
);
20+
addSiblingImageBlock(std, imageFiles, targetModel, placement);
3221
return true;
3322
}
3423

blocksuite/affine/blocks/image/src/utils.ts

+41-70
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import type {
55
ImageBlockModel,
66
ImageBlockProps,
77
} from '@blocksuite/affine-model';
8-
import {
9-
FileSizeLimitService,
10-
NativeClipboardProvider,
11-
} from '@blocksuite/affine-shared/services';
8+
import { NativeClipboardProvider } from '@blocksuite/affine-shared/services';
129
import {
1310
downloadBlob,
1411
getBlockProps,
@@ -312,6 +309,21 @@ export async function copyImageBlob(
312309
}
313310
}
314311

312+
function hasExceeded(
313+
std: BlockStdScope,
314+
files: File[],
315+
maxFileSize = std.store.blobSync.maxFileSize
316+
) {
317+
const exceeded = files.some(file => file.size > maxFileSize);
318+
319+
if (exceeded) {
320+
const size = humanFileSize(maxFileSize, true, 0);
321+
toast(std.host, `You can only upload files less than ${size}`);
322+
}
323+
324+
return exceeded;
325+
}
326+
315327
export function shouldResizeImage(node: Node, target: EventTarget | null) {
316328
return !!(
317329
target &&
@@ -322,79 +334,51 @@ export function shouldResizeImage(node: Node, target: EventTarget | null) {
322334
}
323335

324336
export function addSiblingImageBlock(
325-
editorHost: EditorHost,
337+
std: BlockStdScope,
326338
files: File[],
327-
maxFileSize: number,
328339
targetModel: BlockModel,
329340
place: 'after' | 'before' = 'after'
330341
) {
331-
const imageFiles = files.filter(file => file.type.startsWith('image/'));
332-
if (!imageFiles.length) {
333-
return;
334-
}
342+
files = files.filter(file => file.type.startsWith('image/'));
343+
if (!files.length) return;
335344

336-
const isSizeExceeded = imageFiles.some(file => file.size > maxFileSize);
337-
if (isSizeExceeded) {
338-
toast(
339-
editorHost,
340-
`You can only upload files less than ${humanFileSize(
341-
maxFileSize,
342-
true,
343-
0
344-
)}`
345-
);
346-
return;
347-
}
345+
if (hasExceeded(std, files)) return;
348346

349347
const imageBlockProps: Partial<ImageBlockProps> &
350348
{
351349
flavour: 'affine:image';
352-
}[] = imageFiles.map(file => ({
350+
}[] = files.map(file => ({
353351
flavour: 'affine:image',
354352
size: file.size,
355353
}));
356354

357-
const doc = editorHost.doc;
358-
const blockIds = doc.addSiblingBlocks(targetModel, imageBlockProps, place);
355+
const blockIds = std.store.addSiblingBlocks(
356+
targetModel,
357+
imageBlockProps,
358+
place
359+
);
359360
blockIds.forEach(
360-
(blockId, index) =>
361-
void uploadBlobForImage(editorHost, blockId, imageFiles[index])
361+
(blockId, index) => void uploadBlobForImage(std.host, blockId, files[index])
362362
);
363363
return blockIds;
364364
}
365365

366366
export function addImageBlocks(
367-
editorHost: EditorHost,
367+
std: BlockStdScope,
368368
files: File[],
369-
maxFileSize: number,
370369
parent?: BlockModel | string | null,
371370
parentIndex?: number
372371
) {
373-
const imageFiles = files.filter(file => file.type.startsWith('image/'));
374-
if (!imageFiles.length) {
375-
return;
376-
}
372+
files = files.filter(file => file.type.startsWith('image/'));
373+
if (!files.length) return;
377374

378-
const isSizeExceeded = imageFiles.some(file => file.size > maxFileSize);
379-
if (isSizeExceeded) {
380-
toast(
381-
editorHost,
382-
`You can only upload files less than ${humanFileSize(
383-
maxFileSize,
384-
true,
385-
0
386-
)}`
387-
);
388-
return;
389-
}
375+
if (hasExceeded(std, files)) return;
390376

391-
const doc = editorHost.doc;
392-
const blockIds = imageFiles.map(file =>
393-
doc.addBlock('affine:image', { size: file.size }, parent, parentIndex)
377+
const blockIds = files.map(file =>
378+
std.store.addBlock('affine:image', { size: file.size }, parent, parentIndex)
394379
);
395380
blockIds.forEach(
396-
(blockId, index) =>
397-
void uploadBlobForImage(editorHost, blockId, imageFiles[index])
381+
(blockId, index) => void uploadBlobForImage(std.host, blockId, files[index])
398382
);
399383
return blockIds;
400384
}
@@ -445,25 +429,12 @@ export async function addImages(
445429
transformPoint?: boolean; // determines whether we should use `toModelCoord` to convert the point
446430
}
447431
): Promise<string[]> {
448-
const imageFiles = [...files].filter(file => file.type.startsWith('image/'));
449-
if (!imageFiles.length) return [];
432+
files = [...files].filter(file => file.type.startsWith('image/'));
433+
if (!files.length) return [];
450434

451-
const gfx = std.get(GfxControllerIdentifier);
452-
453-
const maxFileSize = std.store.get(FileSizeLimitService).maxFileSize;
454-
const isSizeExceeded = imageFiles.some(file => file.size > maxFileSize);
455-
if (isSizeExceeded) {
456-
toast(
457-
std.host,
458-
`You can only upload files less than ${humanFileSize(
459-
maxFileSize,
460-
true,
461-
0
462-
)}`
463-
);
464-
return [];
465-
}
435+
if (hasExceeded(std, files)) return [];
466436

437+
const gfx = std.get(GfxControllerIdentifier);
467438
const { point, maxWidth, transformPoint = true } = options;
468439
let { x, y } = gfx.viewport.center;
469440
if (point) {
@@ -476,11 +447,11 @@ export async function addImages(
476447

477448
const dropInfos: { point: Point; blockId: string }[] = [];
478449
const IMAGE_STACK_GAP = 32;
479-
const isMultipleFiles = imageFiles.length > 1;
450+
const isMultipleFiles = files.length > 1;
480451
const inTopLeft = isMultipleFiles ? true : false;
481452

482453
// create image cards without image data
483-
imageFiles.forEach((file, index) => {
454+
files.forEach((file, index) => {
484455
const point = new Point(
485456
x + index * IMAGE_STACK_GAP,
486457
y + index * IMAGE_STACK_GAP
@@ -500,7 +471,7 @@ export async function addImages(
500471
});
501472

502473
// upload image data and update the image model
503-
const uploadPromises = imageFiles.map(async (file, index) => {
474+
const uploadPromises = files.map(async (file, index) => {
504475
const { point, blockId } = dropInfos[index];
505476
const block = std.store.getBlock(blockId);
506477
const imageSize = await readImageSize(file);

blocksuite/affine/shared/src/services/file-size-limit-service.ts

-10
This file was deleted.

blocksuite/affine/shared/src/services/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export * from './editor-setting-service';
88
export * from './embed-iframe';
99
export * from './embed-option-service';
1010
export * from './feature-flag-service';
11-
export * from './file-size-limit-service';
1211
export * from './font-loader';
1312
export * from './generate-url-service';
1413
export * from './link-previewer-service';

0 commit comments

Comments
 (0)