mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-23 21:38:44 +08:00
fix: image proxy url (#14505)
#### 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 -->
This commit is contained in:
@@ -21,12 +21,24 @@ const imageProxyMiddlewareBuilder = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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();
|
const defaultImageProxyMiddlewarBuilder = imageProxyMiddlewareBuilder();
|
||||||
|
|
||||||
export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
|
export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
|
||||||
|
|
||||||
export const defaultImageProxyMiddleware =
|
export const defaultImageProxyMiddleware: TransformerMiddleware = args => {
|
||||||
defaultImageProxyMiddlewarBuilder.get();
|
return defaultImageProxyMiddlewarBuilder.get()(args);
|
||||||
|
};
|
||||||
|
|
||||||
// TODO(@mirone): this should be configured when setup instead of runtime
|
// TODO(@mirone): this should be configured when setup instead of runtime
|
||||||
export class ImageProxyService extends StoreExtension {
|
export class ImageProxyService extends StoreExtension {
|
||||||
@@ -40,7 +52,7 @@ export class ImageProxyService extends StoreExtension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildUrl(imageUrl: string) {
|
buildUrl(imageUrl: string) {
|
||||||
if (imageUrl.startsWith(this.imageProxyURL)) {
|
if (imageUrl.startsWith(this.imageProxyURL) || isImageProxyURL(imageUrl)) {
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,10 +60,14 @@ export async function run() {
|
|||||||
corsOriginCallback(
|
corsOriginCallback(
|
||||||
origin,
|
origin,
|
||||||
finalAllowedOrigins,
|
finalAllowedOrigins,
|
||||||
blockedOrigin =>
|
blockedOrigin => {
|
||||||
logger.warn(`Blocked CORS request from origin: ${blockedOrigin}`, {
|
if (!appendedOrigins.length) {
|
||||||
requestPath,
|
logger.warn(
|
||||||
}),
|
`Blocked CORS request from origin: ${blockedOrigin}`,
|
||||||
|
{ requestPath }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
originCallback
|
originCallback
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';
|
import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';
|
||||||
import { unsafeCSSVarV2 } from '@blocksuite/affine/shared/theme';
|
import { unsafeCSSVarV2 } from '@blocksuite/affine/shared/theme';
|
||||||
import { ShadowlessElement } from '@blocksuite/affine/std';
|
import { ShadowlessElement } from '@blocksuite/affine/std';
|
||||||
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '@blocksuite/affine-shared/consts';
|
import { isImageProxyURL } from '@blocksuite/affine-shared/adapters';
|
||||||
import { ToggleDownIcon, ToolIcon } from '@blocksuite/icons/lit';
|
import { ToggleDownIcon, ToolIcon } from '@blocksuite/icons/lit';
|
||||||
import { type Signal } from '@preact/signals-core';
|
import { type Signal } from '@preact/signals-core';
|
||||||
import { css, html, nothing, type TemplateResult } from 'lit';
|
import { css, html, nothing, type TemplateResult } from 'lit';
|
||||||
@@ -250,7 +250,7 @@ export class ToolResultCard extends SignalWatcher(
|
|||||||
@state()
|
@state()
|
||||||
private accessor isCollapsed = true;
|
private accessor isCollapsed = true;
|
||||||
|
|
||||||
private readonly imageProxyURL = DEFAULT_IMAGE_PROXY_ENDPOINT;
|
private readonly imageProxyURL = BUILD_CONFIG.imageProxyUrl;
|
||||||
|
|
||||||
protected override render() {
|
protected override render() {
|
||||||
return html`
|
return html`
|
||||||
@@ -323,7 +323,7 @@ export class ToolResultCard extends SignalWatcher(
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildUrl(imageUrl: string) {
|
buildUrl(imageUrl: string) {
|
||||||
if (imageUrl.startsWith(this.imageProxyURL)) {
|
if (imageUrl.startsWith(this.imageProxyURL) || isImageProxyURL(imageUrl)) {
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
return `${this.imageProxyURL}?url=${encodeURIComponent(imageUrl)}`;
|
return `${this.imageProxyURL}?url=${encodeURIComponent(imageUrl)}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user