mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
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:
@@ -3,6 +3,7 @@ export * from './copy';
|
||||
export * from './doc-link';
|
||||
export * from './file-name';
|
||||
export * from './paste';
|
||||
export * from './proxy';
|
||||
export * from './replace-id';
|
||||
export * from './surface-ref-to-embed';
|
||||
export * from './title';
|
||||
|
||||
53
blocksuite/affine/shared/src/adapters/middlewares/proxy.ts
Normal file
53
blocksuite/affine/shared/src/adapters/middlewares/proxy.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { TransformerMiddleware } from '@blocksuite/store';
|
||||
import { StoreExtension } from '@blocksuite/store';
|
||||
|
||||
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '../../consts';
|
||||
|
||||
export const customImageProxyMiddleware = (
|
||||
imageProxyURL: string
|
||||
): TransformerMiddleware => {
|
||||
return ({ adapterConfigs }) => {
|
||||
adapterConfigs.set('imageProxy', imageProxyURL);
|
||||
};
|
||||
};
|
||||
|
||||
const imageProxyMiddlewareBuilder = () => {
|
||||
let middleware = customImageProxyMiddleware(DEFAULT_IMAGE_PROXY_ENDPOINT);
|
||||
return {
|
||||
get: () => middleware,
|
||||
set: (url: string) => {
|
||||
middleware = customImageProxyMiddleware(url);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const defaultImageProxyMiddlewarBuilder = imageProxyMiddlewareBuilder();
|
||||
|
||||
export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
|
||||
|
||||
export const defaultImageProxyMiddleware =
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user