本地媒体越积越多,服务器空闲空间越来越少,这可不行。

于是该 matrix-media-repo 出场。

使用 matrix-media-repo 将媒体文件存储移至 S3 Storage。

安装 matrix-media-repo

#!/bin/bash

version=$1
TMP=$(mktemp)

mkdir $TMP
cd $TMP
wget https://github.com/turt2live/matrix-media-repo/releases/download/$version/media_repo-linux-x64 -O media_repo
wget https://github.com/turt2live/matrix-media-repo/releases/download/$version/import_synapse-linux-x64 -O import_synapse

cp media_repo import_synapse /usr/local/bin/

cd /usr/local/bin/
chmod +x media_repo import_synapse

rm -r $TMP

将上述内容保存为 install-matrix-media-repo.sh

# 参数为 matrix-media-repo 最新版版本号
bash install-matrix-media-repo.sh  v1.2.8

配置 matrix-media-repo

创建数据库

sudo -u postgres createuser --pwprompt matrix_media
sudo -u postgres psql
CREATE DATABASE matrix_media_repo
ENCODING 'UTF8'
LC_COLLATE='C'
LC_CTYPE='C'
template=template0
OWNER matrix_media;

创建配置文件

参考配置文件模板文档创建配置文件。

本人配置文件:

repo:
    bindAddress: '127.0.0.1'
    port: 18400
    logDirectory: "-"
database:
    postgres: "postgres://matrix_media:<redacted>@localhost/matrix_media_repo"
    pool:
        maxConnections: 25
        maxIdleConnections: 5
homeservers:
  # 特别提醒,此处 name 一定要与 Synapse homeserver 的 serverName 一致
  # 即如果你的用户ID形如:@bgme:bgme.me ,那么此处应当填写 bgme.me
  # 使用 Delegation 的用户尤其应当注意
  - name: bgme.me
    csApi: "http://localhost:8008"
admins:
  - "@bgme:bgme.me"
datastores:
  - type: s3
    enabled: true
    forKinds: ["all"]
    opts:
        tempPath: "/tmp/mediarepo_s3_upload"
        endpoint: <redacted>
        accessKeyId: "<redacted>"
        accessSecret: "<redacted>"
        ssl: true
        bucketName: "<redacted>"                                                                                                                                                         region: "eu-central-1"
        region: "<redacted>"
accessTokens:                                                                                                                                                                        [3/1816]
    maxCacheTimeSeconds: 43200
    useLocalAppserviceConfig: true
    appservices:
        - id: telegram
            as_token: <redacted>
            senderUserId: "@telegrambot:bgme.me"
            userNamespaces:
                - regex: "@telegram_.+:bgme.me"
thumbnails:
    maxSourceBytes: 10485760
    maxPixels: 32000000
    numWorkers: 100
    sizes:
        - width: 32
            height: 32
        - width: 96
            height: 96
        - width: 320
            height: 240
        - width: 640
            height: 480
        - width: 768   # This size is primarily used for audio thumbnailing.
            height: 240
        - width: 800
            height: 600
    dynamicSizing: true
    types:
        - "image/jpeg"
        - "image/jpg"
        - "image/png"
        - "image/apng"
        - "image/gif"
        - "image/heif"
        - "image/webp"
        - "image/svg+xml"
        - "audio/mpeg"
        - "audio/ogg"                                                                                                                                                                                - "audio/wav"                                                                                                                                                                                - "audio/flac"                                                                                                                                                                               - "video/mp4"
    allowAnimated: true
    defaultAnimated: false
    maxAnimateSizeBytes: 10485760
    stillFrame: 0.5
    expireAfterDays: 0

将配置文件写入 /etc/matrix-media-repo/config.yaml

安装依赖

apt install ffmpeg imagemagick

创建 systemd service

# /etc/systemd/system/matrix-media-repo.service
[Unit]
Description=matrix-media-repo is a highly customizable multi-domain media repository for Matrix.
After=network.target
Wants=network.target

[Service]
Type=simple
User=matrix-synapse
ExecStart=/usr/local/bin/media_repo -config /etc/matrix-media-repo/config.yaml
Restart=on-failure

[Install]
WantedBy=multi-user.target

迁移现有媒体文件

import_synapse -config /etc/matrix-media-repo/config.yaml -dbName synapse -dbUsername synapse_user -dbPassword <redacted> -serverName bgme.me

运行如上命令,耐心等待迁移完成。

将流量导向 matrix-media-repo

nginx 可能参考 Example: nginx

caddy 可在 JSON 中添加如下内容:

{
    "handle": [
    {
        "handler": "subroute",
        "routes": [
        {
            "handle": [
            {
                "handler": "reverse_proxy",
                "headers": {
                "request": {
                    "set": {
                    "Host": [
                        "bgme.me"
                    ]
                    }
                },
                "response": {
                    "delete": [
                    "server"
                    ]
                }
                },
                "upstreams": [
                {
                    "dial": "localhost:18400"
                }
                ]
            }
            ]
        }
        ]
    }
    ],
    "match": [
    {
        "path": [
        "/_matrix/media/*"
        ]
    }
    ]
}

此处应保证请求 Host 与前方配置文件中 name 一致。

收尾

启动 matrix-media-repo 服务

systemctl daemon-reload
systemctl start matrix-media-repo.service
systemctl enable matrix-media-repo.service

如 matrix-media-repo 运行正常,可安全移除本地旧有媒体文件。