|
#!/usr/bin/env python3 |
|
|
|
import requests |
|
import urllib.parse |
|
import argparse |
|
|
|
def get_auth_token(base_url, username, password): |
|
"""Authenticate and get a token.""" |
|
auth_url = f"{base_url}/api/login" |
|
data = { |
|
"username": username, |
|
"password": password |
|
} |
|
response = requests.post(auth_url, json=data) |
|
|
|
# Check the response status |
|
if response.status_code != 200: |
|
print(f"Error: Received status code {response.status_code}") |
|
print(response.text) |
|
response.raise_for_status() |
|
|
|
# Return the JWT token as the authentication token |
|
return response.text |
|
|
|
def generate_share_link(base_url, token, path, expires=0, password=None): |
|
"""Generate a share link.""" |
|
encoded_path = urllib.parse.quote(path) |
|
share_url = f"{base_url}/api/share/{encoded_path}" |
|
headers = { |
|
"X-Auth": token, |
|
"Accept": "application/json", |
|
} |
|
|
|
data = {} |
|
if expires or password: |
|
share_url += f"?expires={expires}&unit=hours" |
|
data["expires"] = str(expires) if expires else '' # The API expects a string here for some reason |
|
data["unit"] = "hours" |
|
data["password"] = password if password else '' |
|
|
|
response = requests.post(share_url, headers=headers, json=data) |
|
response.raise_for_status() |
|
|
|
# Extract the hash from the response and construct the link |
|
hash_value = response.json().get("hash") |
|
shared_link = f"{base_url}/share/{hash_value}" |
|
|
|
return shared_link |
|
|
|
def main(): |
|
parser = argparse.ArgumentParser(description="Generate a share link using Filebrowser API.") |
|
parser.add_argument("base_url", help="Your Filebrowser instance URL.") |
|
parser.add_argument("username", help="Your Filebrowser username.") |
|
parser.add_argument("password", help="Your Filebrowser password.") |
|
parser.add_argument("path", help="Path to be shared. IMPORTANT: When sharing a folder, add a trailing slash!") |
|
parser.add_argument("--expires", type=int, default=0, help="Expiration time in hours. Default is permanent.") |
|
parser.add_argument("--link-password", default=None, help="Password for the shared link.") |
|
|
|
args = parser.parse_args() |
|
|
|
token = get_auth_token(args.base_url, args.username, args.password) |
|
link = generate_share_link(args.base_url, token, args.path, args.expires, args.link_password) |
|
print(link) |
|
|
|
if __name__ == "__main__": |
|
main() |