Skip to content

Commit

Permalink
Add ability to configure the media path for reverse proxies
Browse files Browse the repository at this point in the history
Resolves hifi#266.

Includes hifi#238.
  • Loading branch information
hashworks committed Sep 25, 2023
1 parent 449935f commit 60aa383
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
23 changes: 18 additions & 5 deletions heisenbridge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class BridgeAppService(AppService):
_rooms: Dict[str, Room]
_users: Dict[str, str]

DEFAULT_MEDIA_PATH = "/_matrix/media/v3/download/{netloc}{path}{filename}"

async def push_bridge_state(
self,
state_event: BridgeStateEvent,
Expand All @@ -67,7 +69,6 @@ async def push_bridge_state(
ttl=None,
remote_id=None,
) -> None:

if "heisenbridge" not in self.registration or "status_endpoint" not in self.registration["heisenbridge"]:
return

Expand Down Expand Up @@ -222,7 +223,6 @@ async def ensure_irc_user_id(self, network, nick, update_cache=True):
return user_id

async def _on_mx_event(self, event):

if event.room_id and event.room_id in self._rooms:
try:
room = self._rooms[event.room_id]
Expand Down Expand Up @@ -340,7 +340,9 @@ def mxc_to_url(self, mxc, filename=None):
else:
filename = "/" + urllib.parse.quote(filename)

return "{}/_matrix/media/r0/download/{}{}{}".format(self.endpoint, mxc.netloc, mxc.path, filename)
media_path = self.media_path.format(netloc=mxc.netloc, path=mxc.path, filename=filename)

return "{}{}".format(self.endpoint, media_path)

async def reset(self, config_file, homeserver_url):
with open(config_file) as f:
Expand Down Expand Up @@ -447,7 +449,6 @@ async def ensure_hidden_room(self):
return use_hidden_room

async def run(self, listen_address, listen_port, homeserver_url, owner, safe_mode):

if "sender_localpart" not in self.registration:
print("Missing sender_localpart from registration file.")
sys.exit(1)
Expand Down Expand Up @@ -573,6 +574,7 @@ async def run(self, listen_address, listen_port, homeserver_url, owner, safe_mod
"max_lines": 0,
"use_pastebin": False,
"media_url": None,
"media_path": None,
"namespace": self.puppet_prefix,
}
logging.debug(f"Default config: {self.config}")
Expand Down Expand Up @@ -601,7 +603,7 @@ async def _resolve_media_endpoint():
# use configured media_url for endpoint if we have it
if "heisenbridge" in self.registration and "media_url" in self.registration["heisenbridge"]:
logging.debug(
f"Overriding media URL from regirstation file to {self.registration['heisenbridge']['media_url']}"
f"Overriding media URL from registration file to {self.registration['heisenbridge']['media_url']}"
)
self.endpoint = self.registration["heisenbridge"]["media_url"]
elif self.config["media_url"]:
Expand All @@ -611,6 +613,17 @@ async def _resolve_media_endpoint():
self.endpoint = str(self.api.base_url)
asyncio.ensure_future(_resolve_media_endpoint())

# use configured media_path for media_path if we have it
if "heisenbridge" in self.registration and "media_path" in self.registration["heisenbridge"]:
logging.debug(
f"Overriding media path from registration file to {self.registration['heisenbridge']['media_path']}"
)
self.media_path = self.registration["heisenbridge"]["media_path"]
elif self.config["media_path"]:
self.media_path = self.config["media_path"]
else:
self.media_path = self.DEFAULT_MEDIA_PATH

logging.info("Starting presence loop")
self._keepalive()

Expand Down
18 changes: 18 additions & 0 deletions heisenbridge/control_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def init(self):
cmd.add_argument("--remove", help="remove URL override (will retry auto-detection)", action="store_true")
self.commands.register(cmd, self.cmd_media_url)

cmd = CommandParser(prog="MEDIAPATH", description="configure media path for links")
cmd.add_argument("path", nargs="?", help="new path override")
cmd.add_argument("--remove", help="remove path override", action="store_true")
self.commands.register(cmd, self.cmd_media_path)

cmd = CommandParser(prog="VERSION", description="show bridge version")
self.commands.register(cmd, self.cmd_version)

Expand Down Expand Up @@ -568,6 +573,19 @@ async def cmd_media_url(self, args):
self.send_notice(f"Media URL override is set to {self.serv.config['media_url']}")
self.send_notice(f"Current active media URL: {self.serv.endpoint}")

async def cmd_media_path(self, args):
if args.remove:
self.serv.config["media_path"] = None
await self.serv.save()
self.serv.media_path = self.serv.DEFAULT_MEDIA_PATH
elif args.path:
self.serv.config["media_path"] = args.path
await self.serv.save()
self.serv.media_path = args.path

self.send_notice(f"Media Path override is set to {self.serv.config['media_path']}")
self.send_notice(f"Current active media path: {self.serv.media_path}")

async def cmd_maxlines(self, args):
if args.lines is not None:
self.serv.config["max_lines"] = args.lines
Expand Down

0 comments on commit 60aa383

Please sign in to comment.