This commit is contained in:
Mylloon 2022-10-27 22:07:17 +02:00
parent 33112cf060
commit ed56263d58
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 85 additions and 2 deletions

View file

@ -3,6 +3,7 @@ from os import mkdir
from utils.font import init as init_font
from utils.libjs import init as init_libjs
from utils.misc import exist
from utils.sqlite import FilesDB
class Config:
@ -15,11 +16,14 @@ class Config:
# Desc of the index page
desc = "Sand permet le partage de fichiers. Attention, je n'accepte que \
les petits fichiers de quelques <code>ko</code> car je chiffre ton \
fichier via RSA 🙂."
fichier via RSA 🙂.<br><br>Les fichiers sont herbergés 24 heures."
# Directory name where the uploads are stored
uploads_dir = "uploads"
# Database
database = FilesDB(uploads_dir, "db.sqlite3")
def init() -> None:
# Download dependencies
@ -29,3 +33,5 @@ def init() -> None:
# Create upload folder if doesn't exists
if not exist(Config.uploads_dir):
mkdir(Config.uploads_dir)
# TODO: Clear every t mins the uploads directory and database

View file

@ -1,3 +1,5 @@
from time import time
from config import Config
from flask import Blueprint, redirect, request
from utils.misc import h
@ -13,7 +15,9 @@ def upload() -> Response:
if json:
data = "".join(json["file"])
data_hash = h(data)
with open(f"{Config.uploads_dir}/{data_hash}", "w") as f:
with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f:
f.write(data)
Config.database.add_file(data_hash, int(time()))
return redirect("/index")

73
src/utils/sqlite.py Normal file
View file

@ -0,0 +1,73 @@
from os import mkdir
from sqlite3 import connect
from utils.misc import exist
class Database:
"""Handle SQLite3 database"""
def __init__(self, path: str, filename: str):
if not exist(path):
mkdir(path)
fullpath = f"{path}/{filename}"
if not exist(fullpath):
open(fullpath, 'x')
self.fullpath = fullpath
def request(self, request: str, valeurs=None):
"""Send a request to the database"""
connection = connect(self.fullpath)
cursor = connection.cursor()
if valeurs:
if type(valeurs) not in [list, tuple]:
valeurs = [valeurs]
cursor.execute(request, valeurs)
else:
cursor.execute(request)
connection.commit()
return cursor, cursor.lastrowid
def format(self, keys, cursor: tuple) -> dict:
"""Format sqlite request's result as dict"""
values = []
if cursor != None:
datas = cursor[0].fetchall()
for data in datas[0]:
values.append(data)
if type(keys) not in [list, tuple]:
keys = [keys]
if len(keys) != len(values):
print(keys, values)
raise IndexError
return dict(zip(keys, values))
class FilesDB(Database):
"""Handle files in sqlite3 database"""
def __init__(self, path: str, filename: str):
super().__init__(path, filename)
self.table_name = "files"
self.request(
f"CREATE TABLE IF NOT EXISTS {self.table_name} \
(filename TEXT, date INTEGER);")
def add_file(self, filename: str, date: int):
"""Add a file"""
self.request(
f"INSERT INTO {self.table_name} (filename, date) VALUES (?, ?);",
[filename, date])
def remove_file(self, filename: str) -> None:
"""Remove a file"""
self.request(
f"DELETE FROM {self.table_name} WHERE filename = ?", filename)