Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,41 @@

@app.route('/')
def index():
# return redirect(url_for('upload'))
return render_template('index.html')

@app.route('/dashboard')
def dashboard():
db = l_db['files']
udb = l_db['users']

if session.get('loggedIn', False) == False:
flash('You must be signed in to view the dashboard!', 'error')
return redirect(url_for('upload'))

username = session.get('username')
userfiles = []
quota_usage_gb = 0

if not username:
flash('An error occurred. Please sign in again.', 'error')
session['loggedIn'] = False
session.pop('username', None)
return redirect(url_for('upload'))

for file in db:
if db[file].get('owner') == username:
db[file]['file'] = file
db[file]['code'] = file.split('_')[-1]
db[file]['display_name'] = db[file].get('original_filename', file)
userfiles.append(db[file])

for userfile in userfiles:
usf_mb = userfile['size_megabytes']
if usf_mb:
quota_usage_gb += usf_mb / 1024

quota_usage_gb = round(quota_usage_gb, 1)

# Render template with variables

return render_template(
'dashboard.html',
files=userfiles,
Expand Down Expand Up @@ -166,20 +164,17 @@ def sendfile():
loggedIn = session.get('loggedIn', False)
username = session.get('username', '')


if file:
try:
original_filename = file.filename
extension = os.path.splitext(file.filename)[1].lower()

fileid = str(random.randint(100000, 99999999))
# Use secure_filename for the disk storage but keep original filename in DB
safe_filename = secure_filename(original_filename) or 'file'
dest_name = safe_filename + f'_{fileid}'
dest_path = os.path.join(app.config['UPLOAD_DIRECTORY'], dest_name)

file.save(dest_path)
print(dest_name)

try:
size_bytes = os.path.getsize(dest_path)
Expand Down Expand Up @@ -212,7 +207,7 @@ def sendfile():
remaining_gb = 0.0

entry = {
"reusable": True if reusable else False,
"reusable": True if reusable else False,
"size_megabytes": size_megabytes,
"original_filename": original_filename
}
Expand All @@ -230,11 +225,10 @@ def sendfile():

except RequestEntityTooLarge:
return 'File is larger than the size limit.'

@app.route('/delete/<code>', methods=['GET', 'POST'])
def delete(code):
db = l_db['files']
udb = l_db['users']
for file in db:
if file.split('_')[-1] == code:
if db[file].get('owner') != session.get('username'):
Expand All @@ -243,24 +237,23 @@ def delete(code):
try:
os.remove('uploads/' + file)
except FileNotFoundError:
print('couldnt delete from os bc file not found.')
pass
try:
os.remove('uploads/' + file.split('_')[0])
except FileNotFoundError:
print('couldnt delete from os bc file not found. (2)')
pass
db.pop(file)
flash('File with code ' + code + ' has been deleted.', 'info')
return redirect(url_for('dashboard'))

@app.route('/download')
def download_without_code():
flash('No code provided!', 'error')
return redirect(url_for('upload'))

@app.route('/download/<code>', methods=['GET', 'POST'])
def download(code):
db = l_db['files']
udb = l_db['users']
try:
found = False
filename = ''
Expand All @@ -269,23 +262,17 @@ def download(code):
if file.split('_')[-1] == code:
found = True
filename = file

if not found:
print(filename)
# Search db for the entry with this code and remove it
for file_entry in list(db.keys()):
if file_entry.split('_')[-1] == code:
print(f"Deleting: {file_entry}")
print
db.pop(file_entry)
print(f"Deleted! DB now has {len(db)} files")
break
flash('Invalid code! Check if you typed the correct code, and for one-time codes, make sure nobody else entered the code before you did.', 'error')
return redirect(url_for('upload'))
else:
# Get original filename from database if it exists
original_filename = db[filename].get('original_filename', filename.replace(f'_{code}', ''))

os.rename('uploads/' + filename, 'uploads/' + filename.replace(f'_{code}', ''))
def deleteFile():
db.pop(filename)
Expand All @@ -300,12 +287,8 @@ def backRename():
deleteFunc.start()

return send_from_directory(app.config['UPLOAD_DIRECTORY'], filename.replace(f'_{code}', ''), as_attachment=True, download_name=original_filename)

except Exception as e:
print(f"Error in download: {e}")
print(f"Type: {type(e)}")
import traceback
traceback.print_exc()

except Exception:
flash('Invalid code! Check if you typed the correct code, and for one-time codes, make sure nobody else entered the code before you did.', 'error')
return redirect(url_for('upload'))

Expand All @@ -323,7 +306,6 @@ def admin():
db = l_db['files']
udb = l_db['users']

# Calculate per-user storage usage
user_usage = {}
user_file_count = {}
total_storage_mb = 0.0
Expand Down Expand Up @@ -407,7 +389,6 @@ def admin_delete_user():
return redirect(url_for('admin'))

l_db['users'] = new_users
# Remove files owned by the deleted user
db = l_db['files']
files_to_delete = [fkey for fkey in db if db[fkey].get('owner') == target]
for fkey in files_to_delete:
Expand Down
22 changes: 9 additions & 13 deletions cli/installer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def check_dependencies():
"""Check if required dependencies are installed."""
required_tools = ["git", "curl", "tar", "wget"]
missing_tools = [tool for tool in required_tools if shutil.which(tool) is None]

if missing_tools:
print("The following required tools are missing:")
for tool in missing_tools:
Expand All @@ -42,24 +42,20 @@ def check_python_dependencies():

def detect_installation() -> bool:
"""Detect if LightDB is already installed."""
installation_path = "lightdb"
if os.path.exists(installation_path):
return True
else:
return False

return os.path.exists("lightdb")

def install_lightdb():
check_dependencies()
check_python_dependencies()
print("Starting LightDB installation...")
installation_path = "lightdb"
url = "https://fybe.dev/ldb/ldb.zip"
zip_path = "lightdb.zip"

with Progress() as progress:
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))

if total_size > 0:
task = progress.add_task("Downloading LightDB...", total=total_size)
downloaded = 0
Expand All @@ -75,7 +71,7 @@ def install_lightdb():
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
progress.update(task, advance=len(chunk))

with zipfile.ZipFile(zip_path, 'r') as zip_ref:
files = zip_ref.namelist()
total_files = len(files)
Expand All @@ -85,22 +81,22 @@ def install_lightdb():
sleep(0.3)
zip_ref.extract(file, installation_path)
progress.update(task, advance=1)

os.remove(zip_path)
with console.status("[bold cyan]Verifying LightDB installation...", spinner="dots"):
sleep(0.3)
if os.path.exists(installation_path) and len(os.listdir(installation_path)) > 0:
console.print("[green]✓ LightDB directory verified[/green]")
else:
console.print("[red]✗ Installation verification failed[/red]")

with console.status("[bold cyan]Checking LightDB modules: [bold magenta]base", spinner="dots"):
sleep(0.5)
if os.path.exists(installation_path + '/__init__.py'):
console.print("[green]✓ Module verified: [/green][bold magenta]base[/bold magenta]")
else:
console.print("[red]✗ Module verification failed[/red]")

with console.status("[bold cyan]Checking LightDB modules: [bold magenta]dbconnect", spinner="dots"):
sleep(0.5)
if os.path.exists(installation_path + '/dbconnect.py'):
Expand Down
1 change: 0 additions & 1 deletion lightdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from .lightsql import Table
from .dbconnect import get_connection

# For backward compatibility
connection = get_connection()

__all__ = ['LightDB', 'Table', 'get_connection']
3 changes: 1 addition & 2 deletions lightdb/dbconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import yaml

def get_connection():
# Read database location from config
config_path = os.path.join(os.path.dirname(__file__), '..', 'config', 'db.yaml')
with open(config_path, 'r') as f:
config = yaml.safe_load(f)

db_path = os.path.join(os.path.dirname(__file__), '..', config['DATABASE_LOCATION'])
conn = sqlite3.connect(db_path, check_same_thread=False)
return conn
Loading