-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia.py
More file actions
70 lines (57 loc) · 2.26 KB
/
Copy pathmedia.py
File metadata and controls
70 lines (57 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Media management CLI commands."""
import json as json_mod
import typer
app = typer.Typer(
name="media",
no_args_is_help=True,
help="Manage generated media assets. Run garbage collection to remove orphaned images and other media files that are no longer referenced by any draft.",
)
@app.command("gc")
def media_gc(
ctx: typer.Context,
dry_run: bool = typer.Option(False, "--dry-run", help="Show what would be removed"),
yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"),
):
"""Remove orphaned files from media cache.
Example: social-hook media gc --dry-run
Example: social-hook media gc --yes (skip confirmation)
"""
from social_hook.db.connection import init_database
from social_hook.filesystem import cleanup_orphaned_media, get_db_path
json_output = ctx.obj.get("json", False) if ctx.obj else False
conn = init_database(get_db_path())
try:
# Always preview first
would_remove = cleanup_orphaned_media(conn, dry_run=True)
if not would_remove:
if json_output:
typer.echo(json_mod.dumps({"removed": [], "count": 0}))
else:
typer.echo("No orphaned media found.")
return
if dry_run:
if json_output:
typer.echo(
json_mod.dumps(
{"would_remove": would_remove, "count": len(would_remove)}, indent=2
)
)
else:
typer.echo(f"Would remove {len(would_remove)} orphaned director(ies):")
for p in would_remove:
typer.echo(f" {p}")
return
if not yes:
typer.echo(f"Will remove {len(would_remove)} orphaned director(ies):")
for p in would_remove:
typer.echo(f" {p}")
if not typer.confirm("Proceed?"):
typer.echo("Aborted.")
raise typer.Exit(0)
removed = cleanup_orphaned_media(conn, dry_run=False)
if json_output:
typer.echo(json_mod.dumps({"removed": removed, "count": len(removed)}, indent=2))
else:
typer.echo(f"Removed {len(removed)} orphaned director(ies).")
finally:
conn.close()