Print management token for each uploaded image to stderr
This commit is contained in:
parent
7f09776064
commit
8933388de7
18
oxo.py
18
oxo.py
|
@ -4,6 +4,7 @@ from pathlib import Path
|
|||
|
||||
import httpx
|
||||
import typer
|
||||
from rich.console import Console
|
||||
|
||||
try:
|
||||
from importlib.metadata import version
|
||||
|
@ -16,6 +17,8 @@ app = typer.Typer()
|
|||
|
||||
BASE_URL = typer.Option("https://0x0.st", envvar="OXO_BASE_URL")
|
||||
|
||||
err_console = Console(stderr=True, color_system=None)
|
||||
|
||||
|
||||
def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str:
|
||||
retval = []
|
||||
|
@ -23,6 +26,14 @@ def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str:
|
|||
for d in data:
|
||||
if isinstance(d.get("file"), io.BufferedReader):
|
||||
res = client.post(base_url, files=d)
|
||||
token = res.headers.get("x-token")
|
||||
if token:
|
||||
err_console.print(
|
||||
f"To remove post, curl -Ftoken={token} -Fdelete= {res.text.strip()}"
|
||||
)
|
||||
err_console.print(
|
||||
f"To update expiration date, curl -Ftoken={token} -Fexpires=NUMHOURS {res.text.strip()}"
|
||||
)
|
||||
else:
|
||||
res = client.post(base_url, data=d)
|
||||
res.raise_for_status()
|
||||
|
@ -30,7 +41,7 @@ def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str:
|
|||
return " ".join(retval)
|
||||
|
||||
|
||||
def post_files(base_url, files: t.List[Path]):
|
||||
def post_files(base_url, files: t.List[Path], expires: t.Optional[int]):
|
||||
return post_to(base_url, data=({"file": f.open("rb")} for f in files))
|
||||
|
||||
|
||||
|
@ -70,10 +81,13 @@ def files(
|
|||
files: t.List[Path] = typer.Argument(
|
||||
..., min=1, exists=True, file_okay=True, dir_okay=False, resolve_path=True
|
||||
),
|
||||
expires: t.Optional[int] = typer.Option(
|
||||
None, help="Expiration time, in hours or epoch milliseconds"
|
||||
),
|
||||
base_url: str = BASE_URL,
|
||||
):
|
||||
"""Upload one or more files."""
|
||||
typer.echo(post_files(base_url, files))
|
||||
typer.echo(post_files(base_url, files, expires))
|
||||
|
||||
|
||||
@app.command()
|
||||
|
|
Loading…
Reference in New Issue