mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-20 19:46:32 +08:00
5fb1c11a96
#### PR Dependency Tree * **PR #14505** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Better image-proxy detection to avoid double-proxying already proxied images. * Improved runtime image proxy configuration so images load consistently across deployments. * More robust image URL handling for optimized image loading and fewer redundant requests. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
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 IMAGE_PROXY_PATH = '/api/worker/image-proxy';
|
|
|
|
export const isImageProxyURL = (imageUrl: string) => {
|
|
try {
|
|
const url = new URL(imageUrl, globalThis.location.origin);
|
|
return url.pathname === IMAGE_PROXY_PATH && url.searchParams.has('url');
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const defaultImageProxyMiddlewarBuilder = imageProxyMiddlewareBuilder();
|
|
|
|
export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
|
|
|
|
export const defaultImageProxyMiddleware: TransformerMiddleware = args => {
|
|
return defaultImageProxyMiddlewarBuilder.get()(args);
|
|
};
|
|
|
|
// 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) || isImageProxyURL(imageUrl)) {
|
|
return imageUrl;
|
|
}
|
|
|
|
return `${this.imageProxyURL}?url=${encodeURIComponent(imageUrl)}`;
|
|
}
|
|
|
|
get imageProxyURL() {
|
|
return this._imageProxyURL;
|
|
}
|
|
}
|