-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilebrowser_uploader.py
More file actions
156 lines (132 loc) · 5.1 KB
/
filebrowser_uploader.py
File metadata and controls
156 lines (132 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import asyncio
import os
from urllib.parse import quote
import aiohttp
import asyncpg
from dotenv import load_dotenv
load_dotenv()
# Database settings
db_settings = {
"host": os.getenv("POSTGRES_HOST"),
"port": int(os.getenv("POSTGRES_PORT", 5434)),
"database": os.getenv("POSTGRES_DB"),
"user": os.getenv("POSTGRES_USER"),
"password": os.getenv("POSTGRES_PASSWORD"),
}
FILEBROWSER_URL = os.getenv("FILEBROWSER_URL")
FILEBROWSER_USERNAME = os.getenv("FILEBROWSER_USERNAME")
FILEBROWSER_PASSWORD = os.getenv("FILEBROWSER_PASSWORD")
FILEBROWSER_UPLOAD_PATH = os.getenv("FILEBROWSER_UPLOAD_PATH")
async def insert_data_to_db(
pool, file_name, download_url, date, description, length, host, share_hash
):
async with pool.acquire() as conn:
query = """
INSERT INTO audioarchives (filename, date, description, download_link, length, host, share_hash, visit_count, latest_visit)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
"""
await conn.execute(
query,
file_name,
date,
description,
download_url,
length,
host,
share_hash,
0,
None,
)
async def get_public_share_url(file_relative_path: str, token: str) -> str:
headers = {"X-Auth": token.strip(), "accept": "*/*"}
async with aiohttp.ClientSession() as session:
async with session.post(
f"{FILEBROWSER_URL}/api/share/{file_relative_path}",
headers=headers,
json={"path": f"/{file_relative_path}"},
) as response:
if response.status != 200:
body = await response.text()
raise Exception(
f"Failed to create share link: {response.status} - {body}"
)
data = await response.json()
return data["hash"]
async def get_filebrowser_token():
async with aiohttp.ClientSession() as session:
async with session.post(
f"{FILEBROWSER_URL}/api/login",
json={"username": FILEBROWSER_USERNAME, "password": FILEBROWSER_PASSWORD},
) as response:
token = await response.text()
return token.strip()
async def delete_existing_file(file_relative_path: str, token: str):
async with aiohttp.ClientSession() as session:
async with session.delete(
f"{FILEBROWSER_URL}/api/resources/{file_relative_path}",
headers={"X-Auth": token},
) as response:
if response.status not in [200, 204, 404]:
body = await response.text()
raise Exception(
f"Failed to delete existing file: {response.status} - {body}"
)
async def upload_to_filebrowser(file_path, file_name) -> str:
token = await get_filebrowser_token()
headers = {"X-Auth": token.strip(), "accept": "*/*"}
file_relative_path = f"{FILEBROWSER_UPLOAD_PATH}/{file_name}"
await delete_existing_file(file_relative_path, token)
async with aiohttp.ClientSession() as session:
with open(file_path, "rb") as f:
form = aiohttp.FormData()
form.add_field("files", f, filename=file_name, content_type="audio/mpeg")
async with session.post(
f"{FILEBROWSER_URL}/api/resources/{FILEBROWSER_UPLOAD_PATH}/{file_name}",
headers=headers,
data=form,
) as response:
if response.status != 200:
body = await response.text()
raise Exception(f"Upload failed: {response.status} - {body}")
share_url = await get_public_share_url(file_relative_path, token)
return share_url
async def upload(
file_name: str,
file_path: str,
host: str,
description: str,
date: str,
length: float,
):
sanitized_file_name = (
file_name.replace("&", "&")
.replace("&Amp;", "&")
.replace("&", "and")
.replace("/", " or ")
)
try:
share_hash = await upload_to_filebrowser(file_path, sanitized_file_name)
except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file_path}")
except PermissionError:
raise PermissionError(f"Permission denied for reading file: {file_path}")
except Exception as e:
raise RuntimeError(f"Unexpected error while uploading file: {str(e)}")
pool = await asyncpg.create_pool(**db_settings)
download_url = f"https://broadcasting.hbni.net/play_recording/{quote(file_name)}"
await insert_data_to_db(
pool, file_name, download_url, date, description, length, host, share_hash
)
await pool.close()
if __name__ == "__main__":
asyncio.run(
upload(
"Glenwayprivate - Unspecified description - February 20 Thursday 2025 08_50 PM - 1h 36m 34s.mp3",
"CURRENTLY_RECORDING/Glenwayprivate - Unspecified description - February 20 Thursday 2025 08_50 PM - 1h 36m 34s.mp3",
"glenway",
"Unspecified description",
"February 20 Thursday 2025 08_50 PM",
96.53333333333334,
),
debug=True,
)