chore: proxy image preview in frontend (#11957)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Images and icons in bookmark cards are now loaded through an image proxy for improved reliability and consistency.
	- Embed blocks for GitHub, Loom, and YouTube now display banner and creator images via an image proxy service for enhanced image loading.

- **Refactor**
	- Simplified backend URL handling and proxy logic for images, resulting in more efficient processing and reduced complexity.
	- Consolidated image proxy middleware and services into a shared adapter module for streamlined imports and improved maintainability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
forehalo
2025-04-24 10:23:25 +00:00
parent eaa1bc6bf1
commit 4ffa37d1c3
30 changed files with 86 additions and 155 deletions

View File

@@ -1,4 +1,4 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine-block-image'; import { defaultImageProxyMiddleware } from '@blocksuite/affine-shared/adapters';
import { import {
Schema, Schema,
Transformer, Transformer,

View File

@@ -14,6 +14,7 @@ import {
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { import {
HtmlAdapterFactoryExtension, HtmlAdapterFactoryExtension,
ImageProxyService,
MarkdownAdapterFactoryExtension, MarkdownAdapterFactoryExtension,
MixTextAdapterFactoryExtension, MixTextAdapterFactoryExtension,
NotionHtmlAdapterFactoryExtension, NotionHtmlAdapterFactoryExtension,
@@ -83,9 +84,12 @@ const MigratingStoreExtensions: ExtensionType[] = [
getAdapterFactoryExtensions(), getAdapterFactoryExtensions(),
FeatureFlagService, FeatureFlagService,
BlockMetaService,
// TODO(@mirone): maybe merge these services into a file setting service
LinkPreviewerService, LinkPreviewerService,
FileSizeLimitService, FileSizeLimitService,
BlockMetaService, ImageProxyService,
].flat(); ].flat();
export class MigratingStoreExtension extends StoreExtensionProvider { export class MigratingStoreExtension extends StoreExtensionProvider {

View File

@@ -1,5 +1,6 @@
import { getEmbedCardIcons } from '@blocksuite/affine-block-embed'; import { getEmbedCardIcons } from '@blocksuite/affine-block-embed';
import { WebIcon16 } from '@blocksuite/affine-components/icons'; import { WebIcon16 } from '@blocksuite/affine-components/icons';
import { ImageProxyService } from '@blocksuite/affine-shared/adapters';
import { ThemeProvider } from '@blocksuite/affine-shared/services'; import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { getHostName } from '@blocksuite/affine-shared/utils'; import { getHostName } from '@blocksuite/affine-shared/utils';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/lit'; import { SignalWatcher, WithDisposable } from '@blocksuite/global/lit';
@@ -85,11 +86,12 @@ export class BookmarkCard extends SignalWatcher(
const theme = this.bookmark.std.get(ThemeProvider).theme; const theme = this.bookmark.std.get(ThemeProvider).theme;
const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme); const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme);
const imageProxyService = this.bookmark.doc.get(ImageProxyService);
const titleIcon = this.loading const titleIcon = this.loading
? LoadingIcon ? LoadingIcon
: icon : icon
? html`<img src=${icon} alt="icon" />` ? html`<img src=${imageProxyService.buildUrl(icon)} alt="icon" />`
: WebIcon16; : WebIcon16;
const descriptionText = this.loading const descriptionText = this.loading
@@ -102,7 +104,7 @@ export class BookmarkCard extends SignalWatcher(
const bannerImage = const bannerImage =
!this.loading && image !this.loading && image
? html`<img src=${image} alt="banner" />` ? html`<img src=${imageProxyService.buildUrl(image)} alt="banner" />`
: EmbedCardBannerIcon; : EmbedCardBannerIcon;
return html` return html`

View File

@@ -3,6 +3,7 @@ import type {
EmbedGithubModel, EmbedGithubModel,
EmbedGithubStyles, EmbedGithubStyles,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { ImageProxyService } from '@blocksuite/affine-shared/adapters';
import { ThemeProvider } from '@blocksuite/affine-shared/services'; import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { BlockSelection, isGfxBlockComponent } from '@blocksuite/std'; import { BlockSelection, isGfxBlockComponent } from '@blocksuite/std';
import { html, nothing } from 'lit'; import { html, nothing } from 'lit';
@@ -131,6 +132,7 @@ export class EmbedGithubBlockComponent extends EmbedBlockComponent<
const loading = this.loading; const loading = this.loading;
const theme = this.std.get(ThemeProvider).theme; const theme = this.std.get(ThemeProvider).theme;
const imageProxyService = this.doc.get(ImageProxyService);
const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme); const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme);
const titleIcon = loading ? LoadingIcon : GithubIcon; const titleIcon = loading ? LoadingIcon : GithubIcon;
const statusIcon = status const statusIcon = status
@@ -141,9 +143,7 @@ export class EmbedGithubBlockComponent extends EmbedBlockComponent<
const descriptionText = loading ? '' : description; const descriptionText = loading ? '' : description;
const bannerImage = const bannerImage =
!loading && image !loading && image
? html`<object type="image/webp" data=${image} draggable="false"> ? html`<img src=${imageProxyService.buildUrl(image)} alt="banner" />`
${EmbedCardBannerIcon}
</object>`
: EmbedCardBannerIcon; : EmbedCardBannerIcon;
let dateText = ''; let dateText = '';

View File

@@ -1,5 +1,6 @@
import { OpenIcon } from '@blocksuite/affine-components/icons'; import { OpenIcon } from '@blocksuite/affine-components/icons';
import type { EmbedLoomModel, EmbedLoomStyles } from '@blocksuite/affine-model'; import type { EmbedLoomModel, EmbedLoomStyles } from '@blocksuite/affine-model';
import { ImageProxyService } from '@blocksuite/affine-shared/adapters';
import { ThemeProvider } from '@blocksuite/affine-shared/services'; import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { BlockSelection } from '@blocksuite/std'; import { BlockSelection } from '@blocksuite/std';
import { html } from 'lit'; import { html } from 'lit';
@@ -92,15 +93,14 @@ export class EmbedLoomBlockComponent extends EmbedBlockComponent<
const loading = this.loading; const loading = this.loading;
const theme = this.std.get(ThemeProvider).theme; const theme = this.std.get(ThemeProvider).theme;
const imageProxyService = this.doc.get(ImageProxyService);
const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme); const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme);
const titleIcon = loading ? LoadingIcon : LoomIcon; const titleIcon = loading ? LoadingIcon : LoomIcon;
const titleText = loading ? 'Loading...' : title; const titleText = loading ? 'Loading...' : title;
const descriptionText = loading ? '' : description; const descriptionText = loading ? '' : description;
const bannerImage = const bannerImage =
!loading && image !loading && image
? html`<object type="image/webp" data=${image} draggable="false"> ? html`<img src=${imageProxyService.buildUrl(image)} alt="banner" />`
${EmbedCardBannerIcon}
</object>`
: EmbedCardBannerIcon; : EmbedCardBannerIcon;
return this.renderEmbed( return this.renderEmbed(

View File

@@ -3,6 +3,7 @@ import type {
EmbedYoutubeModel, EmbedYoutubeModel,
EmbedYoutubeStyles, EmbedYoutubeStyles,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { ImageProxyService } from '@blocksuite/affine-shared/adapters';
import { ThemeProvider } from '@blocksuite/affine-shared/services'; import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { BlockSelection } from '@blocksuite/std'; import { BlockSelection } from '@blocksuite/std';
import { html, nothing } from 'lit'; import { html, nothing } from 'lit';
@@ -106,24 +107,22 @@ export class EmbedYoutubeBlockComponent extends EmbedBlockComponent<
const loading = this.loading; const loading = this.loading;
const theme = this.std.get(ThemeProvider).theme; const theme = this.std.get(ThemeProvider).theme;
const imageProxyService = this.doc.get(ImageProxyService);
const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme); const { LoadingIcon, EmbedCardBannerIcon } = getEmbedCardIcons(theme);
const titleIcon = loading ? LoadingIcon : YoutubeIcon; const titleIcon = loading ? LoadingIcon : YoutubeIcon;
const titleText = loading ? 'Loading...' : title; const titleText = loading ? 'Loading...' : title;
const descriptionText = loading ? null : description; const descriptionText = loading ? null : description;
const bannerImage = const bannerImage =
!loading && image !loading && image
? html`<object type="image/webp" data=${image} draggable="false"> ? html`<img src=${imageProxyService.buildUrl(image)} alt="banner" />`
${EmbedCardBannerIcon}
</object>`
: EmbedCardBannerIcon; : EmbedCardBannerIcon;
const creatorImageEl = const creatorImageEl =
!loading && creatorImage !loading && creatorImage
? html`<object ? html`<img
type="image/webp" src=${imageProxyService.buildUrl(creatorImage)}
data=${creatorImage} alt="creator"
draggable="false" />`
></object>`
: nothing; : nothing;
return this.renderEmbed( return this.renderEmbed(

View File

@@ -1,4 +1,3 @@
export * from './html.js'; export * from './html.js';
export * from './markdown.js'; export * from './markdown.js';
export * from './middleware.js';
export * from './notion-html.js'; export * from './notion-html.js';

View File

@@ -1,20 +0,0 @@
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '@blocksuite/affine-shared/consts';
import { StoreExtension } from '@blocksuite/store';
import { setImageProxyMiddlewareURL } from './adapters/middleware';
// TODO(@mirone): this should be configured when setup instead of runtime
export class ImageProxyService extends StoreExtension {
static override key = 'image-proxy';
private _imageProxyURL = DEFAULT_IMAGE_PROXY_ENDPOINT;
setImageProxyURL(url: string) {
this._imageProxyURL = url;
setImageProxyMiddlewareURL(url);
}
get imageProxyURL() {
return this._imageProxyURL;
}
}

View File

@@ -6,7 +6,6 @@ import { literal } from 'lit/static-html.js';
import { imageSlashMenuConfig } from './configs/slash-menu'; import { imageSlashMenuConfig } from './configs/slash-menu';
import { createBuiltinToolbarConfigExtension } from './configs/toolbar'; import { createBuiltinToolbarConfigExtension } from './configs/toolbar';
import { ImageProxyService } from './image-proxy-service';
import { ImageDropOption } from './image-service'; import { ImageDropOption } from './image-service';
const flavour = ImageBlockSchema.model.flavour; const flavour = ImageBlockSchema.model.flavour;
@@ -26,5 +25,3 @@ export const ImageBlockSpec: ExtensionType[] = [
createBuiltinToolbarConfigExtension(flavour), createBuiltinToolbarConfigExtension(flavour),
SlashMenuConfigExtension(flavour, imageSlashMenuConfig), SlashMenuConfigExtension(flavour, imageSlashMenuConfig),
].flat(); ].flat();
export const ImageStoreSpec: ExtensionType[] = [ImageProxyService];

View File

@@ -3,7 +3,6 @@ export * from './commands';
export * from './edgeless-clipboard-config'; export * from './edgeless-clipboard-config';
export * from './image-block'; export * from './image-block';
export * from './image-edgeless-block'; export * from './image-edgeless-block';
export { ImageProxyService } from './image-proxy-service';
export * from './image-service'; export * from './image-service';
export * from './image-spec'; export * from './image-spec';
export * from './styles'; export * from './styles';

View File

@@ -4,30 +4,15 @@ import {
} from '@blocksuite/affine-ext-loader'; } from '@blocksuite/affine-ext-loader';
import { ImageBlockSchemaExtension } from '@blocksuite/affine-model'; import { ImageBlockSchemaExtension } from '@blocksuite/affine-model';
import { ImageSelectionExtension } from '@blocksuite/affine-shared/selection'; import { ImageSelectionExtension } from '@blocksuite/affine-shared/selection';
import { z } from 'zod';
import { ImageBlockAdapterExtensions } from './adapters/extension'; import { ImageBlockAdapterExtensions } from './adapters/extension';
import { ImageProxyService } from './image-proxy-service';
const ImageStoreExtensionOptionsSchema = z.object({ export class ImageStoreExtension extends StoreExtensionProvider {
imageProxyURL: z.string().optional(),
});
export class ImageStoreExtension extends StoreExtensionProvider<
z.infer<typeof ImageStoreExtensionOptionsSchema>
> {
override name = 'affine-image-block'; override name = 'affine-image-block';
override schema = ImageStoreExtensionOptionsSchema;
override setup(context: StoreExtensionContext) { override setup(context: StoreExtensionContext) {
super.setup(context); super.setup(context);
context.register([ context.register([ImageBlockSchemaExtension, ImageSelectionExtension]);
ImageBlockSchemaExtension,
ImageProxyService,
ImageSelectionExtension,
]);
context.register(ImageBlockAdapterExtensions); context.register(ImageBlockAdapterExtensions);
// TODO(@mirone): set image proxy url
} }
} }

View File

@@ -1,8 +1,8 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine-block-image';
import { import {
AttachmentAdapter, AttachmentAdapter,
ClipboardAdapter, ClipboardAdapter,
copyMiddleware, copyMiddleware,
defaultImageProxyMiddleware,
HtmlAdapter, HtmlAdapter,
ImageAdapter, ImageAdapter,
MixTextAdapter, MixTextAdapter,

View File

@@ -3,6 +3,7 @@ export * from './copy';
export * from './doc-link'; export * from './doc-link';
export * from './file-name'; export * from './file-name';
export * from './paste'; export * from './paste';
export * from './proxy';
export * from './replace-id'; export * from './replace-id';
export * from './surface-ref-to-embed'; export * from './surface-ref-to-embed';
export * from './title'; export * from './title';

View File

@@ -1,5 +1,7 @@
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '@blocksuite/affine-shared/consts';
import type { TransformerMiddleware } from '@blocksuite/store'; import type { TransformerMiddleware } from '@blocksuite/store';
import { StoreExtension } from '@blocksuite/store';
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '../../consts';
export const customImageProxyMiddleware = ( export const customImageProxyMiddleware = (
imageProxyURL: string imageProxyURL: string
@@ -25,3 +27,27 @@ export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
export const defaultImageProxyMiddleware = export const defaultImageProxyMiddleware =
defaultImageProxyMiddlewarBuilder.get(); defaultImageProxyMiddlewarBuilder.get();
// TODO(@mirone): this should be configured when setup instead of runtime
export class ImageProxyService extends StoreExtension {
static override key = 'image-proxy';
private _imageProxyURL = DEFAULT_IMAGE_PROXY_ENDPOINT;
setImageProxyURL(url: string) {
this._imageProxyURL = url;
setImageProxyMiddlewareURL(url);
}
buildUrl(imageUrl: string) {
if (imageUrl.startsWith(this.imageProxyURL)) {
return imageUrl;
}
return `${this.imageProxyURL}?url=${encodeURIComponent(imageUrl)}`;
}
get imageProxyURL() {
return this._imageProxyURL;
}
}

View File

@@ -1,5 +1,5 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine-block-image';
import { import {
defaultImageProxyMiddleware,
docLinkBaseURLMiddleware, docLinkBaseURLMiddleware,
fileNameMiddleware, fileNameMiddleware,
HtmlAdapter, HtmlAdapter,

View File

@@ -1,5 +1,5 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine-block-image';
import { import {
defaultImageProxyMiddleware,
docLinkBaseURLMiddleware, docLinkBaseURLMiddleware,
fileNameMiddleware, fileNameMiddleware,
MarkdownAdapter, MarkdownAdapter,

View File

@@ -1,5 +1,7 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine-block-image'; import {
import { NotionHtmlAdapter } from '@blocksuite/affine-shared/adapters'; defaultImageProxyMiddleware,
NotionHtmlAdapter,
} from '@blocksuite/affine-shared/adapters';
import { Container } from '@blocksuite/global/di'; import { Container } from '@blocksuite/global/di';
import { sha } from '@blocksuite/global/utils'; import { sha } from '@blocksuite/global/utils';
import { import {

View File

@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-restricted-imports */ /* eslint-disable @typescript-eslint/no-restricted-imports */
import '@shoelace-style/shoelace/dist/components/tab-panel/tab-panel.js'; import '@shoelace-style/shoelace/dist/components/tab-panel/tab-panel.js';
import { defaultImageProxyMiddleware } from '@blocksuite/affine/blocks/image';
import { WithDisposable } from '@blocksuite/affine/global/lit'; import { WithDisposable } from '@blocksuite/affine/global/lit';
import { import {
defaultImageProxyMiddleware,
docLinkBaseURLMiddlewareBuilder, docLinkBaseURLMiddlewareBuilder,
embedSyncedDocMiddleware, embedSyncedDocMiddleware,
type HtmlAdapter, type HtmlAdapter,

View File

@@ -16,7 +16,6 @@ import '@shoelace-style/shoelace/dist/themes/light.css';
import '@shoelace-style/shoelace/dist/themes/dark.css'; import '@shoelace-style/shoelace/dist/themes/dark.css';
import './left-side-panel.js'; import './left-side-panel.js';
import { defaultImageProxyMiddleware } from '@blocksuite/affine/blocks/image';
import { ExportManager } from '@blocksuite/affine/blocks/surface'; import { ExportManager } from '@blocksuite/affine/blocks/surface';
import { toast } from '@blocksuite/affine/components/toast'; import { toast } from '@blocksuite/affine/components/toast';
import { StoreExtensionManagerIdentifier } from '@blocksuite/affine/ext-loader'; import { StoreExtensionManagerIdentifier } from '@blocksuite/affine/ext-loader';
@@ -27,6 +26,7 @@ import {
import type { SerializedXYWH } from '@blocksuite/affine/global/gfx'; import type { SerializedXYWH } from '@blocksuite/affine/global/gfx';
import { ColorScheme, type DocMode } from '@blocksuite/affine/model'; import { ColorScheme, type DocMode } from '@blocksuite/affine/model';
import { import {
defaultImageProxyMiddleware,
docLinkBaseURLMiddleware, docLinkBaseURLMiddleware,
HtmlAdapterFactoryIdentifier, HtmlAdapterFactoryIdentifier,
MarkdownAdapterFactoryIdentifier, MarkdownAdapterFactoryIdentifier,

View File

@@ -67,10 +67,10 @@ Generated by [AVA](https://avajs.dev).
{ {
description: 'Test Description', description: 'Test Description',
favicons: [ favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico', 'http://example.com/favicon.ico',
], ],
images: [ images: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Fimage.png', 'http://example.com/image.png',
], ],
title: 'Test Title', title: 'Test Title',
url: 'http://example.com/page', url: 'http://example.com/page',
@@ -82,7 +82,7 @@ Generated by [AVA](https://avajs.dev).
{ {
charset: 'gbk', charset: 'gbk',
favicons: [ favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico', 'http://example.com/favicon.ico',
], ],
images: [], images: [],
title: '你好,世界。', title: '你好,世界。',
@@ -95,7 +95,7 @@ Generated by [AVA](https://avajs.dev).
{ {
charset: 'shift_jis', charset: 'shift_jis',
favicons: [ favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico', 'http://example.com/favicon.ico',
], ],
images: [], images: [],
title: 'こんにちは、世界。', title: 'こんにちは、世界。',
@@ -108,7 +108,7 @@ Generated by [AVA](https://avajs.dev).
{ {
charset: 'big5', charset: 'big5',
favicons: [ favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico', 'http://example.com/favicon.ico',
], ],
images: [], images: [],
title: '你好,世界。', title: '你好,世界。',
@@ -121,7 +121,7 @@ Generated by [AVA](https://avajs.dev).
{ {
charset: 'euc-kr', charset: 'euc-kr',
favicons: [ favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico', 'http://example.com/favicon.ico',
], ],
images: [], images: [],
title: '안녕하세요, 세계.', title: '안녕하세요, 세계.',

View File

@@ -209,7 +209,6 @@ export class WorkerController {
videos: [], videos: [],
favicons: [], favicons: [],
}; };
const baseUrl = new URL(request.url, this.url.baseUrl).toString();
if (response.body) { if (response.body) {
const resp = await decodeWithCharset(response, res); const resp = await decodeWithCharset(response, res);
@@ -276,7 +275,7 @@ export class WorkerController {
await rewriter.transform(resp).text(); await rewriter.transform(resp).text();
res.images = await reduceUrls(baseUrl, res.images); res.images = await reduceUrls(res.images);
this.logger.debug('Processed response with HTMLRewriter', { this.logger.debug('Processed response with HTMLRewriter', {
origin, origin,
@@ -293,7 +292,7 @@ export class WorkerController {
appendUrl(faviconUrl.toString(), res.favicons); appendUrl(faviconUrl.toString(), res.favicons);
} }
res.favicons = await reduceUrls(baseUrl, res.favicons); res.favicons = await reduceUrls(res.favicons);
} }
const json = JSON.stringify(res); const json = JSON.stringify(res);

View File

@@ -1,5 +1,4 @@
export * from './headers'; export * from './headers';
export * from './proxy';
export * from './url'; export * from './url';
export function parseJson<T>(data: string): T | null { export function parseJson<T>(data: string): T | null {

View File

@@ -1,54 +0,0 @@
const IMAGE_PROXY = '/api/worker/image-proxy';
const httpsDomain = new Set();
async function checkHttpsSupport(url: URL): Promise<boolean> {
const httpsUrl = new URL(url.toString());
httpsUrl.protocol = 'https:';
try {
const response = await fetch(httpsUrl, {
method: 'HEAD',
redirect: 'manual',
});
if (response.ok || (response.status >= 400 && response.status < 600)) {
return true;
}
} catch {}
return false;
}
async function fixProtocol(url: string): Promise<URL> {
const targetUrl = new URL(url);
if (targetUrl.protocol !== 'http:') {
return targetUrl;
} else if (httpsDomain.has(targetUrl.hostname)) {
targetUrl.protocol = 'https:';
return targetUrl;
} else if (await checkHttpsSupport(targetUrl)) {
httpsDomain.add(targetUrl.hostname);
targetUrl.protocol = 'https:';
return targetUrl;
}
return targetUrl;
}
export function imageProxyBuilder(
url: string
): (url: string) => Promise<string | undefined> {
try {
const proxy = new URL(url);
proxy.pathname = IMAGE_PROXY;
return async url => {
try {
const targetUrl = await fixProtocol(url);
proxy.searchParams.set('url', targetUrl.toString());
return proxy.toString();
} catch {}
return;
};
} catch {
return async url => url.toString();
}
}

View File

@@ -1,7 +1,5 @@
import { getDomain, getSubdomain } from 'tldts'; import { getDomain, getSubdomain } from 'tldts';
import { imageProxyBuilder } from './proxy';
const localhost = new Set(['localhost', '127.0.0.1']); const localhost = new Set(['localhost', '127.0.0.1']);
const URL_FIXERS: Record<string, (url: URL) => URL> = { const URL_FIXERS: Record<string, (url: URL) => URL> = {
@@ -58,11 +56,6 @@ export function appendUrl(url: string | null, array?: string[]) {
} }
} }
export async function reduceUrls(baseUrl: string, urls?: string[]) { export async function reduceUrls(urls: string[] = []) {
if (urls && urls.length > 0) { return Array.from(new Set(urls.filter(Boolean) as string[]));
const imageProxy = imageProxyBuilder(baseUrl);
const newUrls = await Promise.all(urls.map(imageProxy));
return newUrls.filter((x): x is string => !!x);
}
return [];
} }

View File

@@ -1,11 +1,11 @@
import { createReactComponentFromLit } from '@affine/component'; import { createReactComponentFromLit } from '@affine/component';
import {
defaultImageProxyMiddleware,
ImageProxyService,
} from '@blocksuite/affine/blocks/image';
import { Container, type ServiceProvider } from '@blocksuite/affine/global/di'; import { Container, type ServiceProvider } from '@blocksuite/affine/global/di';
import { WithDisposable } from '@blocksuite/affine/global/lit'; import { WithDisposable } from '@blocksuite/affine/global/lit';
import { codeBlockWrapMiddleware } from '@blocksuite/affine/shared/adapters'; import {
codeBlockWrapMiddleware,
defaultImageProxyMiddleware,
ImageProxyService,
} from '@blocksuite/affine/shared/adapters';
import { import {
LinkPreviewerService, LinkPreviewerService,
ThemeProvider, ThemeProvider,

View File

@@ -1,5 +1,5 @@
import { defaultImageProxyMiddleware } from '@blocksuite/affine/blocks/image';
import { deleteTextCommand } from '@blocksuite/affine/inlines/preset'; import { deleteTextCommand } from '@blocksuite/affine/inlines/preset';
import { defaultImageProxyMiddleware } from '@blocksuite/affine/shared/adapters';
import { isInsideEdgelessEditor } from '@blocksuite/affine/shared/utils'; import { isInsideEdgelessEditor } from '@blocksuite/affine/shared/utils';
import { import {
type BlockComponent, type BlockComponent,

View File

@@ -10,14 +10,14 @@ import {
} from '@affine/core/modules/editor-setting'; } from '@affine/core/modules/editor-setting';
import { FeatureFlagService } from '@affine/core/modules/feature-flag'; import { FeatureFlagService } from '@affine/core/modules/feature-flag';
import track from '@affine/track'; import track from '@affine/track';
import {
customImageProxyMiddleware,
ImageProxyService,
} from '@blocksuite/affine/blocks/image';
import { appendParagraphCommand } from '@blocksuite/affine/blocks/paragraph'; import { appendParagraphCommand } from '@blocksuite/affine/blocks/paragraph';
import type { DocTitle } from '@blocksuite/affine/fragments/doc-title'; import type { DocTitle } from '@blocksuite/affine/fragments/doc-title';
import { DisposableGroup } from '@blocksuite/affine/global/disposable'; import { DisposableGroup } from '@blocksuite/affine/global/disposable';
import type { DocMode, RootBlockModel } from '@blocksuite/affine/model'; import type { DocMode, RootBlockModel } from '@blocksuite/affine/model';
import {
customImageProxyMiddleware,
ImageProxyService,
} from '@blocksuite/affine/shared/adapters';
import { focusBlockEnd } from '@blocksuite/affine/shared/commands'; import { focusBlockEnd } from '@blocksuite/affine/shared/commands';
import { LinkPreviewerService } from '@blocksuite/affine/shared/services'; import { LinkPreviewerService } from '@blocksuite/affine/shared/services';
import { getLastNoteBlock } from '@blocksuite/affine/shared/utils'; import { getLastNoteBlock } from '@blocksuite/affine/shared/utils';

View File

@@ -1,7 +1,7 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impls/workspace'; import { WorkspaceImpl } from '@affine/core/modules/workspace/impls/workspace';
import { defaultImageProxyMiddleware } from '@blocksuite/affine/blocks/image';
import type { ServiceProvider } from '@blocksuite/affine/global/di'; import type { ServiceProvider } from '@blocksuite/affine/global/di';
import { import {
defaultImageProxyMiddleware,
embedSyncedDocMiddleware, embedSyncedDocMiddleware,
MarkdownAdapter, MarkdownAdapter,
MixTextAdapter, MixTextAdapter,

View File

@@ -21,12 +21,12 @@ import { WorkbenchService } from '@affine/core/modules/workbench';
import { ViewService } from '@affine/core/modules/workbench/services/view'; import { ViewService } from '@affine/core/modules/workbench/services/view';
import { WorkspaceService } from '@affine/core/modules/workspace'; import { WorkspaceService } from '@affine/core/modules/workspace';
import { i18nTime } from '@affine/i18n'; import { i18nTime } from '@affine/i18n';
import { DisposableGroup } from '@blocksuite/affine/global/disposable';
import { RefNodeSlotsProvider } from '@blocksuite/affine/inlines/reference';
import { import {
customImageProxyMiddleware, customImageProxyMiddleware,
ImageProxyService, ImageProxyService,
} from '@blocksuite/affine/blocks/image'; } from '@blocksuite/affine/shared/adapters';
import { DisposableGroup } from '@blocksuite/affine/global/disposable';
import { RefNodeSlotsProvider } from '@blocksuite/affine/inlines/reference';
import { LinkPreviewerService } from '@blocksuite/affine/shared/services'; import { LinkPreviewerService } from '@blocksuite/affine/shared/services';
import { import {
FrameworkScope, FrameworkScope,