57 lines
1.6 KiB
Python
Executable file
57 lines
1.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Read a file and upload its contents to bin.xlion.tw."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
# Allow importing pastebin.py from the same directory without PYTHONPATH
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from pastebin import detect_extension, format_output, upload # noqa: E402
|
|
|
|
MAX_FILE_SIZE = 1_048_576 # 1 MB, matches Wastebin server default
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="Upload a file to bin.xlion.tw")
|
|
parser.add_argument("file_path", help="Path to the file to upload")
|
|
parser.add_argument(
|
|
"-e",
|
|
"--extension",
|
|
help="File extension for syntax highlighting (auto-detected from path if not provided)",
|
|
default="",
|
|
)
|
|
parser.add_argument(
|
|
"--expires",
|
|
type=int,
|
|
default=31536000,
|
|
help="Expiration time in seconds (default: 31536000 = 1 year)",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if not os.path.exists(args.file_path):
|
|
print(f"Error: File not found: {args.file_path}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
file_size = os.path.getsize(args.file_path)
|
|
if file_size > MAX_FILE_SIZE:
|
|
print(
|
|
f"Error: File too large ({file_size} bytes). Maximum size is 1 MB.",
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
|
|
ext = args.extension or detect_extension(args.file_path)
|
|
|
|
with open(args.file_path, "r", encoding="utf-8") as f:
|
|
text = f.read()
|
|
|
|
result = upload(text, extension=ext, expires=args.expires)
|
|
print(format_output(result, extension=ext))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|