jkjnkjkllkjjk
This commit is contained in:
@@ -27,6 +27,7 @@ import requests
|
||||
import re
|
||||
|
||||
from helper.logger import log, debug
|
||||
from helper.utils_constant import mime_maps
|
||||
|
||||
|
||||
class StorageBackend(ABC):
|
||||
@@ -707,6 +708,18 @@ class HydrusStorageBackend(StorageBackend):
|
||||
if title != f"Hydrus File {file_id}":
|
||||
break
|
||||
|
||||
# Resolve extension from MIME type
|
||||
mime_type = meta.get("mime")
|
||||
ext = ""
|
||||
if mime_type:
|
||||
for category in mime_maps.values():
|
||||
for ext_key, info in category.items():
|
||||
if mime_type in info.get("mimes", []):
|
||||
ext = info.get("ext", "").lstrip('.')
|
||||
break
|
||||
if ext:
|
||||
break
|
||||
|
||||
# Filter results based on query type
|
||||
# If user provided explicit namespace (has ':'), don't do substring filtering
|
||||
# Just include what the tag search returned
|
||||
@@ -726,7 +739,8 @@ class HydrusStorageBackend(StorageBackend):
|
||||
"origin": "hydrus",
|
||||
"tags": all_tags,
|
||||
"file_id": file_id,
|
||||
"mime": meta.get("mime"),
|
||||
"mime": mime_type,
|
||||
"ext": ext,
|
||||
})
|
||||
else:
|
||||
# Free-form search: check if search terms match the title or tags
|
||||
@@ -758,7 +772,8 @@ class HydrusStorageBackend(StorageBackend):
|
||||
"origin": "hydrus",
|
||||
"tags": all_tags,
|
||||
"file_id": file_id,
|
||||
"mime": meta.get("mime"),
|
||||
"mime": mime_type,
|
||||
"ext": ext,
|
||||
})
|
||||
|
||||
debug(f"Found {len(results)} result(s)")
|
||||
@@ -971,6 +986,60 @@ class MatrixStorageBackend(StorageBackend):
|
||||
def get_name(self) -> str:
|
||||
return "matrix"
|
||||
|
||||
def list_rooms(self, config: Dict[str, Any]) -> List[Dict[str, Any]]:
|
||||
"""List joined rooms with their names."""
|
||||
matrix_conf = config.get('storage', {}).get('matrix', {})
|
||||
homeserver = matrix_conf.get('homeserver')
|
||||
access_token = matrix_conf.get('access_token')
|
||||
|
||||
if not homeserver or not access_token:
|
||||
return []
|
||||
|
||||
if not homeserver.startswith('http'):
|
||||
homeserver = f"https://{homeserver}"
|
||||
|
||||
headers = {"Authorization": f"Bearer {access_token}"}
|
||||
|
||||
try:
|
||||
# Get joined rooms
|
||||
resp = requests.get(f"{homeserver}/_matrix/client/v3/joined_rooms", headers=headers, timeout=10)
|
||||
if resp.status_code != 200:
|
||||
return []
|
||||
|
||||
room_ids = resp.json().get('joined_rooms', [])
|
||||
rooms = []
|
||||
|
||||
for rid in room_ids:
|
||||
# Try to get room name
|
||||
name = "Unknown Room"
|
||||
try:
|
||||
# Get state event for name
|
||||
name_resp = requests.get(
|
||||
f"{homeserver}/_matrix/client/v3/rooms/{rid}/state/m.room.name",
|
||||
headers=headers,
|
||||
timeout=2
|
||||
)
|
||||
if name_resp.status_code == 200:
|
||||
name = name_resp.json().get('name', name)
|
||||
else:
|
||||
# Try canonical alias
|
||||
alias_resp = requests.get(
|
||||
f"{homeserver}/_matrix/client/v3/rooms/{rid}/state/m.room.canonical_alias",
|
||||
headers=headers,
|
||||
timeout=2
|
||||
)
|
||||
if alias_resp.status_code == 200:
|
||||
name = alias_resp.json().get('alias', name)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
rooms.append({'id': rid, 'name': name})
|
||||
|
||||
return rooms
|
||||
except Exception as e:
|
||||
log(f"Error listing Matrix rooms: {e}", file=sys.stderr)
|
||||
return []
|
||||
|
||||
def upload(self, file_path: Path, **kwargs: Any) -> str:
|
||||
"""Upload file to Matrix room.
|
||||
|
||||
@@ -993,8 +1062,8 @@ class MatrixStorageBackend(StorageBackend):
|
||||
access_token = matrix_conf.get('access_token')
|
||||
room_id = matrix_conf.get('room_id')
|
||||
|
||||
if not homeserver or not room_id:
|
||||
raise ValueError("Matrix homeserver and room_id required")
|
||||
if not homeserver:
|
||||
raise ValueError("Matrix homeserver required")
|
||||
|
||||
# Ensure homeserver has protocol
|
||||
if not homeserver.startswith('http'):
|
||||
@@ -1004,6 +1073,39 @@ class MatrixStorageBackend(StorageBackend):
|
||||
if not access_token:
|
||||
raise ValueError("Matrix access_token required (login not yet implemented)")
|
||||
|
||||
# Handle room selection if not provided
|
||||
if not room_id:
|
||||
log("No room_id configured. Fetching joined rooms...", file=sys.stderr)
|
||||
rooms = self.list_rooms(config)
|
||||
|
||||
if not rooms:
|
||||
raise ValueError("No joined rooms found or failed to fetch rooms.")
|
||||
|
||||
from result_table import ResultTable
|
||||
table = ResultTable("Matrix Rooms")
|
||||
for i, room in enumerate(rooms):
|
||||
row = table.add_row()
|
||||
row.add_column("#", str(i + 1))
|
||||
row.add_column("Name", room['name'])
|
||||
row.add_column("ID", room['id'])
|
||||
|
||||
print(table)
|
||||
|
||||
# Simple interactive selection
|
||||
try:
|
||||
selection = input("Select room # to upload to: ")
|
||||
idx = int(selection) - 1
|
||||
if 0 <= idx < len(rooms):
|
||||
room_id = rooms[idx]['id']
|
||||
log(f"Selected room: {rooms[idx]['name']} ({room_id})", file=sys.stderr)
|
||||
else:
|
||||
raise ValueError("Invalid selection")
|
||||
except Exception:
|
||||
raise ValueError("Invalid room selection")
|
||||
|
||||
if not room_id:
|
||||
raise ValueError("Matrix room_id required")
|
||||
|
||||
# 1. Upload Media
|
||||
upload_url = f"{homeserver}/_matrix/media/r3/upload"
|
||||
headers = {
|
||||
|
||||
Reference in New Issue
Block a user