Merge pull request #371

* feat(backend): Override the file URL rendering in AP
This commit is contained in:
Caipira 2023-09-22 11:34:50 +09:00 committed by GitHub
parent 176af77635
commit 1959194cd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 35 deletions

View file

@ -154,6 +154,9 @@ id: 'aidx'
# saKeyPath: /path/to/service-account-key.json
# logName: cherrypick
# Override the file URL rendering in ActivityPub (Object Storage file only)
#apFileBaseUrl: https://example.com/
# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

View file

@ -172,6 +172,9 @@ id: 'aidx'
# saKeyPath: /path/to/service-account-key.json
# logName: cherrypick
# Override the file URL rendering in ActivityPub (Object Storage file only)
#apFileBaseUrl: https://example.com/
# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

View file

@ -154,6 +154,9 @@ id: 'aidx'
# saKeyPath: /path/to/service-account-key.json
# logName: cherrypick
# Override the file URL rendering in ActivityPub (Object Storage file only)
#apFileBaseUrl: https://example.com/
# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

View file

@ -174,6 +174,9 @@ id: "aidx"
# saKeyPath: /path/to/service-account-key.json
# logName: cherrypick
# Override the file URL rendering in ActivityPub (Object Storage file only)
#apFileBaseUrl: https://example.com/
# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

View file

@ -86,6 +86,8 @@ type Source = {
logName?: string;
}
apFileBaseUrl?: string;
mediaProxy?: string;
proxyRemoteFiles?: boolean;
videoThumbnailGenerator?: string;
@ -145,6 +147,7 @@ export type Config = {
relashionshipJobPerSec: number | undefined;
deliverJobMaxAttempts: number | undefined;
inboxJobMaxAttempts: number | undefined;
apFileBaseUrl: string | undefined;
proxyRemoteFiles: boolean | undefined;
signToActivityPubGet: boolean | undefined;
@ -250,6 +253,7 @@ export function loadConfig(): Config {
inboxJobMaxAttempts: config.inboxJobMaxAttempts,
proxyRemoteFiles: config.proxyRemoteFiles,
signToActivityPubGet: config.signToActivityPubGet,
apFileBaseUrl: config.apFileBaseUrl,
mediaProxy: externalMediaProxy ?? internalMediaProxy,
externalMediaProxyEnabled: externalMediaProxy !== null && externalMediaProxy !== internalMediaProxy,
videoThumbnailGenerator: config.videoThumbnailGenerator ?

View file

@ -166,7 +166,7 @@ export class ApRendererService {
return {
type: 'Document',
mediaType: file.webpublicType ?? file.type,
url: this.driveFileEntityService.getPublicUrl(file),
url: this.driveFileEntityService.getPublicUrl(file, undefined, true),
name: file.comment,
};
}
@ -245,7 +245,7 @@ export class ApRendererService {
public renderImage(file: MiDriveFile): IApImage {
return {
type: 'Image',
url: this.driveFileEntityService.getPublicUrl(file),
url: this.driveFileEntityService.getPublicUrl(file, undefined, true),
sensitive: file.isSensitive,
name: file.comment,
};

View file

@ -250,8 +250,8 @@ export class ApPersonService implements OnModuleInit {
return {
avatarId: avatar?.id ?? null,
bannerId: banner?.id ?? null,
avatarUrl: avatar ? this.driveFileEntityService.getPublicUrl(avatar, 'avatar') : null,
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
avatarUrl: avatar ? this.driveFileEntityService.getPublicUrl(avatar, 'avatar', true) : null,
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner, undefined, true) : null,
avatarBlurhash: avatar?.blurhash ?? null,
bannerBlurhash: banner?.blurhash ?? null,
};

View file

@ -107,7 +107,7 @@ export class DriveFileEntityService {
}
@bindThis
public getPublicUrl(file: MiDriveFile, mode?: 'avatar'): string { // static = thumbnail
public getPublicUrl(file: MiDriveFile, mode?: 'avatar', ap?: boolean): string { // static = thumbnail
// リモートかつメディアプロキシ
if (file.uri != null && file.userHost != null && this.config.externalMediaProxyEnabled) {
return this.getProxiedUrl(file.uri, mode);
@ -129,6 +129,16 @@ export class DriveFileEntityService {
if (mode === 'avatar') {
return this.getProxiedUrl(url, 'avatar');
}
if (ap && this.config.apFileBaseUrl) {
const baseUrl = this.config.apFileBaseUrl;
const isValidBaseUrl = /^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(\/.*)?$/i.test(baseUrl);
if (isValidBaseUrl) {
const trimmedBaseUrl = baseUrl.replace(/\/$/, '');
return url.replace(/^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}/, trimmedBaseUrl);
}
}
return url;
}