2022-05-24 21:45:37 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import asyncio
|
|
|
|
import getpass
|
|
|
|
import json
|
|
|
|
import math
|
|
|
|
import pathlib
|
|
|
|
import sys
|
|
|
|
from tqdm import tqdm
|
|
|
|
from websockets import connect
|
|
|
|
|
|
|
|
def file_loader(files):
|
|
|
|
with tqdm(desc="Total", total=sum(size for (path, size) in files), unit='B', unit_scale=True, leave=True, position=1) as total_progress:
|
|
|
|
for (path, size) in files:
|
|
|
|
with tqdm(desc=path.name, total=size, unit='B', unit_scale=True, leave=True, position=0) as file_progress:
|
|
|
|
with path.open("rb") as f:
|
|
|
|
while f.tell() < size:
|
|
|
|
data = f.read(min(16384, size - f.tell()))
|
|
|
|
if data == "":
|
|
|
|
tqdm.write("file ended early!")
|
|
|
|
exit(1)
|
|
|
|
total_progress.update(len(data))
|
|
|
|
file_progress.update(len(data))
|
|
|
|
yield data
|
|
|
|
|
2023-06-11 05:57:17 -04:00
|
|
|
async def send(paths, host, password, lifetime, collection_name=None):
|
2022-05-24 21:45:37 -04:00
|
|
|
paths = [path for path in paths if path.is_file()]
|
|
|
|
fileMetadata = [
|
|
|
|
{
|
|
|
|
"name": path.name,
|
|
|
|
"size": path.stat().st_size,
|
|
|
|
"modtime": math.floor(path.stat().st_mtime * 1000),
|
|
|
|
} for path in paths
|
|
|
|
]
|
|
|
|
manifest = {
|
|
|
|
"files": fileMetadata,
|
|
|
|
"lifetime": lifetime,
|
|
|
|
"password": password,
|
|
|
|
}
|
2022-05-29 11:12:43 -04:00
|
|
|
if collection_name is not None:
|
|
|
|
manifest["collection_name"] = collection_name
|
|
|
|
|
2023-06-11 05:57:17 -04:00
|
|
|
async with connect("wss://{}/upload".format(host)) as ws:
|
2022-05-24 21:45:37 -04:00
|
|
|
await ws.send(json.dumps(manifest))
|
|
|
|
resp = json.loads(await ws.recv())
|
|
|
|
if resp["type"] != "ready":
|
|
|
|
print("unexpected response: {}".format(resp))
|
|
|
|
exit(1)
|
2023-06-11 05:57:17 -04:00
|
|
|
print("Download: https://{}/download?code={}".format(host, resp["code"]))
|
2022-05-24 21:45:37 -04:00
|
|
|
loader = file_loader([(paths[i], fileMetadata[i]["size"]) for i in range(len(paths))])
|
|
|
|
for data in loader:
|
|
|
|
await ws.send(data)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Upload files to transbeam")
|
2022-05-24 22:10:01 -04:00
|
|
|
parser.add_argument("-l", "--lifetime", type=int, default=7, help="Lifetime in days for files (default 7)")
|
2023-06-11 05:57:17 -04:00
|
|
|
parser.add_argument("-H", "--host", type=str, default="transbeam.link", help="transbeam host (default transbeam.link)")
|
2022-05-29 11:12:43 -04:00
|
|
|
parser.add_argument("-n", "--collection-name", type=str, help="Name for a collection of multiple files")
|
2022-05-24 21:45:37 -04:00
|
|
|
parser.add_argument("files", type=pathlib.Path, nargs="+", help="Files to upload")
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
args = parser.parse_args()
|
2022-05-29 11:12:43 -04:00
|
|
|
if len(args.files) == 1 and args.collection_name is not None:
|
|
|
|
print("--collection-name is only applicable when multiple files are being uploaded")
|
|
|
|
exit(1)
|
2022-05-24 21:45:37 -04:00
|
|
|
password = getpass.getpass()
|
2023-06-11 05:57:17 -04:00
|
|
|
await send(args.files, args.host, password, args.lifetime, args.collection_name)
|
2022-05-24 21:45:37 -04:00
|
|
|
|
|
|
|
asyncio.run(main())
|