fix: normalize unicode escape sequences and reformat multi-line expressions across codebase
This commit is contained in:
+81
-55
@@ -41,35 +41,35 @@ ALLOWED_UPLOAD_TYPES = {
|
||||
}
|
||||
|
||||
FILE_ICONS = {
|
||||
".pdf": "\U0001F4C4",
|
||||
".zip": "\U0001F4E6",
|
||||
".gz": "\U0001F4E6",
|
||||
".tar": "\U0001F4E6",
|
||||
".rar": "\U0001F4E6",
|
||||
".7z": "\U0001F4E6",
|
||||
".mp4": "\U0001F3AC",
|
||||
".webm": "\U0001F3AC",
|
||||
".ogv": "\U0001F3AC",
|
||||
".mov": "\U0001F3AC",
|
||||
".m4v": "\U0001F3AC",
|
||||
".mp3": "\U0001F3B5",
|
||||
".py": "\U0001F4BB",
|
||||
".js": "\U0001F4BB",
|
||||
".ts": "\U0001F4BB",
|
||||
".html": "\U0001F4BB",
|
||||
".css": "\U0001F4BB",
|
||||
".json": "\U0001F4BB",
|
||||
".md": "\U0001F4BB",
|
||||
".csv": "\U0001F4CA",
|
||||
".xls": "\U0001F4CA",
|
||||
".xlsx": "\U0001F4CA",
|
||||
".doc": "\U0001F4DD",
|
||||
".docx": "\U0001F4DD",
|
||||
".txt": "\U0001F4C4",
|
||||
".pdf": "\U0001f4c4",
|
||||
".zip": "\U0001f4e6",
|
||||
".gz": "\U0001f4e6",
|
||||
".tar": "\U0001f4e6",
|
||||
".rar": "\U0001f4e6",
|
||||
".7z": "\U0001f4e6",
|
||||
".mp4": "\U0001f3ac",
|
||||
".webm": "\U0001f3ac",
|
||||
".ogv": "\U0001f3ac",
|
||||
".mov": "\U0001f3ac",
|
||||
".m4v": "\U0001f3ac",
|
||||
".mp3": "\U0001f3b5",
|
||||
".py": "\U0001f4bb",
|
||||
".js": "\U0001f4bb",
|
||||
".ts": "\U0001f4bb",
|
||||
".html": "\U0001f4bb",
|
||||
".css": "\U0001f4bb",
|
||||
".json": "\U0001f4bb",
|
||||
".md": "\U0001f4bb",
|
||||
".csv": "\U0001f4ca",
|
||||
".xls": "\U0001f4ca",
|
||||
".xlsx": "\U0001f4ca",
|
||||
".doc": "\U0001f4dd",
|
||||
".docx": "\U0001f4dd",
|
||||
".txt": "\U0001f4c4",
|
||||
".exe": "\u2699",
|
||||
".bin": "\u2699",
|
||||
}
|
||||
DEFAULT_FILE_ICON = "\U0001F4CE"
|
||||
DEFAULT_FILE_ICON = "\U0001f4ce"
|
||||
|
||||
|
||||
def _get_max_upload_bytes():
|
||||
@@ -173,29 +173,33 @@ def store_attachment(file_bytes, original_filename, user_uid):
|
||||
if ext not in (".gif",):
|
||||
thumbnail = _generate_thumbnail(file_bytes, file_dir / f"{uid}_thumb.jpg")
|
||||
|
||||
get_table("attachments").insert({
|
||||
"uid": uid,
|
||||
"target_type": "",
|
||||
"target_uid": "",
|
||||
"user_uid": user_uid,
|
||||
"original_filename": original_filename,
|
||||
"stored_name": stored_name,
|
||||
"directory": directory,
|
||||
"file_size": len(file_bytes),
|
||||
"mime_type": mime,
|
||||
"image_width": image_width,
|
||||
"image_height": image_height,
|
||||
"has_thumbnail": 1 if thumbnail else 0,
|
||||
"thumbnail_name": thumbnail,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
})
|
||||
get_table("attachments").insert(
|
||||
{
|
||||
"uid": uid,
|
||||
"target_type": "",
|
||||
"target_uid": "",
|
||||
"user_uid": user_uid,
|
||||
"original_filename": original_filename,
|
||||
"stored_name": stored_name,
|
||||
"directory": directory,
|
||||
"file_size": len(file_bytes),
|
||||
"mime_type": mime,
|
||||
"image_width": image_width,
|
||||
"image_height": image_height,
|
||||
"has_thumbnail": 1 if thumbnail else 0,
|
||||
"thumbnail_name": thumbnail,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
)
|
||||
return {
|
||||
"uid": uid,
|
||||
"original_filename": original_filename,
|
||||
"file_size": len(file_bytes),
|
||||
"mime_type": mime,
|
||||
"url": f"/static/uploads/attachments/{directory}/{stored_name}",
|
||||
"thumbnail_url": f"/static/uploads/attachments/{directory}/{thumbnail}" if thumbnail else None,
|
||||
"thumbnail_url": f"/static/uploads/attachments/{directory}/{thumbnail}"
|
||||
if thumbnail
|
||||
else None,
|
||||
"has_thumbnail": thumbnail is not None,
|
||||
"is_image": is_image,
|
||||
"is_video": mime.startswith("video/"),
|
||||
@@ -203,14 +207,18 @@ def store_attachment(file_bytes, original_filename, user_uid):
|
||||
|
||||
|
||||
def link_attachments(uids, target_type, target_uid):
|
||||
flat = [uid.strip() for raw in uids or [] for uid in str(raw).split(",") if uid.strip()]
|
||||
flat = [
|
||||
uid.strip() for raw in uids or [] for uid in str(raw).split(",") if uid.strip()
|
||||
]
|
||||
if not flat:
|
||||
return
|
||||
placeholders = ",".join(f":p{i}" for i in range(len(flat)))
|
||||
params = {f"p{i}": uid for i, uid in enumerate(flat)}
|
||||
db.query(
|
||||
f"UPDATE attachments SET target_type=:tt, target_uid=:tu WHERE uid IN ({placeholders})",
|
||||
tt=target_type, tu=target_uid, **params,
|
||||
tt=target_type,
|
||||
tu=target_uid,
|
||||
**params,
|
||||
)
|
||||
|
||||
|
||||
@@ -224,7 +232,9 @@ def _unlink_attachment_files(row):
|
||||
file_path.unlink(missing_ok=True)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to delete attachment file {file_path}: {e}")
|
||||
for thumb_path in (ATTACHMENTS_DIR / directory).glob(f"{Path(stored_name).stem}_thumb.*"):
|
||||
for thumb_path in (ATTACHMENTS_DIR / directory).glob(
|
||||
f"{Path(stored_name).stem}_thumb.*"
|
||||
):
|
||||
try:
|
||||
thumb_path.unlink(missing_ok=True)
|
||||
except Exception as e:
|
||||
@@ -243,7 +253,9 @@ def delete_attachment(uid):
|
||||
|
||||
|
||||
def delete_target_attachments(target_type, target_uid):
|
||||
for row in get_table("attachments").find(target_type=target_type, target_uid=target_uid):
|
||||
for row in get_table("attachments").find(
|
||||
target_type=target_type, target_uid=target_uid
|
||||
):
|
||||
_delete_attachment_row(row)
|
||||
|
||||
|
||||
@@ -253,10 +265,13 @@ def delete_attachments_for(target_type, target_uids):
|
||||
return
|
||||
placeholders = ",".join(f":p{i}" for i in range(len(uids)))
|
||||
params = {f"p{i}": uid for i, uid in enumerate(uids)}
|
||||
rows = list(db.query(
|
||||
f"SELECT * FROM attachments WHERE target_type=:tt AND target_uid IN ({placeholders})",
|
||||
tt=target_type, **params,
|
||||
))
|
||||
rows = list(
|
||||
db.query(
|
||||
f"SELECT * FROM attachments WHERE target_type=:tt AND target_uid IN ({placeholders})",
|
||||
tt=target_type,
|
||||
**params,
|
||||
)
|
||||
)
|
||||
if not rows:
|
||||
return
|
||||
for row in rows:
|
||||
@@ -268,7 +283,11 @@ def delete_attachments_for(target_type, target_uids):
|
||||
def get_attachments(target_type, target_uid):
|
||||
if "attachments" not in db.tables:
|
||||
return []
|
||||
rows = list(get_table("attachments").find(target_type=target_type, target_uid=target_uid, order_by=["created_at"]))
|
||||
rows = list(
|
||||
get_table("attachments").find(
|
||||
target_type=target_type, target_uid=target_uid, order_by=["created_at"]
|
||||
)
|
||||
)
|
||||
return [_row_to_attachment(r) for r in rows]
|
||||
|
||||
|
||||
@@ -281,7 +300,8 @@ def get_attachments_batch(target_type, uids):
|
||||
params = {f"p{i}": uid for i, uid in enumerate(uids)}
|
||||
rows = db.query(
|
||||
f"SELECT * FROM attachments WHERE target_type=:tt AND target_uid IN ({placeholders}) ORDER BY created_at",
|
||||
tt=target_type, **params,
|
||||
tt=target_type,
|
||||
**params,
|
||||
)
|
||||
result = {uid: [] for uid in uids}
|
||||
for row in rows:
|
||||
@@ -299,14 +319,20 @@ def _row_to_attachment(row):
|
||||
if not thumb_name:
|
||||
stem = Path(stored_name).stem
|
||||
png = f"{stem}_thumb.png"
|
||||
thumb_name = png if (ATTACHMENTS_DIR / directory / png).exists() else f"{stem}_thumb.jpg"
|
||||
thumb_name = (
|
||||
png
|
||||
if (ATTACHMENTS_DIR / directory / png).exists()
|
||||
else f"{stem}_thumb.jpg"
|
||||
)
|
||||
return {
|
||||
"uid": row["uid"],
|
||||
"original_filename": row.get("original_filename", ""),
|
||||
"file_size": row.get("file_size", 0),
|
||||
"mime_type": row.get("mime_type", ""),
|
||||
"url": f"/static/uploads/attachments/{directory}/{stored_name}",
|
||||
"thumbnail_url": f"/static/uploads/attachments/{directory}/{thumb_name}" if thumb_name else None,
|
||||
"thumbnail_url": f"/static/uploads/attachments/{directory}/{thumb_name}"
|
||||
if thumb_name
|
||||
else None,
|
||||
"has_thumbnail": bool(row.get("has_thumbnail")),
|
||||
"is_image": row.get("mime_type", "").startswith("image/"),
|
||||
"is_video": row.get("mime_type", "").startswith("video/"),
|
||||
|
||||
Reference in New Issue
Block a user