2024-11-22 09:13:31 -05:00
#! /usr/bin/env nix-shell
2024-11-25 14:21:02 -05:00
#! nix-shell /etc/nixos/nixos/script-dependencies.nix -i python
2024-11-22 09:13:31 -05:00
import argparse
import mimetypes
import os
from pathlib import Path
from shutil import which
from typing import Any
import requests # type: ignore
2024-11-29 06:35:15 -05:00
from common . common import ( # type: ignore
does_desktop_entry_exist ,
notify ,
read_secret_file ,
)
2024-11-22 10:05:40 -05:00
from pyperclip import copy # type: ignore
2024-11-22 09:13:31 -05:00
def zipline (
file_path : Path ,
instance_url : str ,
2024-11-29 06:35:15 -05:00
application_name : str | None = None ,
desktop_entry : str | None = None ,
2024-11-22 09:13:31 -05:00
) - > Any :
token = read_secret_file ( " zipline " )
if not os . path . isfile ( file_path ) :
raise FileNotFoundError ( f " File at { file_path } does not exist. " )
2024-11-22 10:35:19 -05:00
use_notify_send = False
2024-11-22 09:13:31 -05:00
if application_name and desktop_entry :
if not does_desktop_entry_exist ( desktop_entry = desktop_entry ) :
raise FileNotFoundError ( " Desktop entry does not exist. " )
if not which ( " notify-send " ) :
raise FileNotFoundError ( " notify-send is not installed. " )
2024-11-22 10:35:19 -05:00
use_notify_send = True
2024-11-22 09:13:31 -05:00
content_type = mimetypes . guess_type ( file_path ) [ 0 ] or " application/octet-stream "
try :
headers = { " authorization " : token }
files = {
" file " : ( os . path . basename ( file_path ) , open ( file_path , " rb " ) , content_type )
}
response = requests . post (
f " { instance_url . rstrip ( ' / ' ) } /api/upload " , headers = headers , files = files
)
if response . status_code == 200 :
response_data = response . json ( )
link = response_data . get ( " files " , [ None ] ) [ 0 ]
if link :
2024-11-22 10:04:55 -05:00
copy ( text = link )
2024-11-22 09:50:05 -05:00
print ( f " Link copied to clipboard: { link } " )
2024-11-22 09:13:31 -05:00
2024-11-22 10:35:19 -05:00
if use_notify_send :
2024-11-22 09:13:31 -05:00
notify (
application_name = application_name ,
title = " Upload Successful " ,
message = f " Link copied to clipboard: { link } " ,
urgency = " low " ,
category = " transfer.complete " ,
icon = file_path ,
)
else :
raise ValueError ( " Invalid response format. " )
else :
error_message = response . text
raise Exception ( error_message )
2024-11-25 14:21:02 -05:00
except BaseException as e :
2024-11-22 10:35:19 -05:00
if use_notify_send :
2024-11-22 09:13:31 -05:00
notify (
application_name = application_name ,
title = " Upload Failed " ,
message = f " An error occurred: { e } " ,
2024-11-29 06:35:15 -05:00
urgency = " normal " ,
2024-11-22 09:13:31 -05:00
category = " transfer.error " ,
icon = file_path ,
)
raise e
if __name__ == " __main__ " :
parser = argparse . ArgumentParser (
2024-11-22 09:52:38 -05:00
prog = " zipline.py " ,
2024-11-22 09:13:31 -05:00
description = " Upload a file to a Zipline instance. " ,
2024-11-22 09:52:38 -05:00
epilog = " Example usage: zipline.py /path/to/file.txt " ,
2024-11-22 09:13:31 -05:00
)
parser . add_argument ( " file " , help = " The file to upload. " )
parser . add_argument (
" --url " ,
help = " The URL of the Zipline instance. Defaults to ' https://csw.im ' . " ,
default = " https://csw.im " ,
)
parser . add_argument (
" --application-name " ,
help = " The name of the application that is uploading the file. Defaults to ' Zipline ' . " ,
default = " Zipline " ,
)
parser . add_argument (
" --desktop-entry " ,
2024-11-22 10:47:30 -05:00
help = " The desktop entry file for the application that is uploading the file. If this is provided, notify-send will be invoked to display a notification if the upload succeeds or fails. " ,
2024-11-22 09:13:31 -05:00
default = None ,
)
args = parser . parse_args ( )
zipline (
file_path = args . file ,
instance_url = args . url ,
application_name = args . application_name ,
desktop_entry = args . desktop_entry ,
)