Skip to content

Commit

Permalink
feat(backend): Override the file URL rendering in AP
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Sep 19, 2023
1 parent ebbb18e commit 21bc129
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 35 deletions.
23 changes: 13 additions & 10 deletions .config/docker_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ dbReplications: false
# You can configure any number of replicas here
#dbSlaves:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:

# ┌─────────────────────┐
#───┘ Redis configuration └─────────────────────────────────────
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 13 additions & 10 deletions .devcontainer/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ dbReplications: false
# You can configure any number of replicas here
#dbSlaves:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:

# ┌─────────────────────┐
#───┘ Redis configuration └─────────────────────────────────────
Expand Down Expand Up @@ -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

Expand Down
23 changes: 13 additions & 10 deletions chart/files/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ dbReplications: false
# You can configure any number of replicas here
#dbSlaves:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:
# -
# host:
# port:
# db:
# user:
# pass:
# host:
# port:
# db:
# user:
# pass:

# ┌─────────────────────┐
#───┘ Redis configuration └─────────────────────────────────────
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Source = {
logName?: string;
}

apFileBaseUrl?: string;

mediaProxy?: string;
proxyRemoteFiles?: boolean;
videoThumbnailGenerator?: string;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 ?
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/activitypub/ApRendererService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
12 changes: 11 additions & 1 deletion packages/backend/src/core/entities/DriveFileEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 21bc129

Please sign in to comment.