new short expiring url for files
This commit is contained in:
@@ -4,6 +4,7 @@ import base64
|
||||
#~ from Crypto.Cipher import AES
|
||||
import hashlib
|
||||
|
||||
|
||||
#~ class Crypto:
|
||||
#~ def __init__(self, secret):
|
||||
#~ self.secret = add_pad(secret[0:16])
|
||||
@@ -68,6 +69,14 @@ def random_token():
|
||||
return token
|
||||
|
||||
|
||||
def random_expiring_hash():
|
||||
codes = []
|
||||
for i in range(3):
|
||||
codes.append("".join([random.choice(string.ascii_letters + string.digits) for n in range(4)]))
|
||||
ehash = "-".join(codes)
|
||||
return ehash
|
||||
|
||||
|
||||
def remove_pad(string):
|
||||
""" Remove spaces from right """
|
||||
return string.rstrip(" ")
|
||||
|
||||
@@ -5,6 +5,11 @@ import requests
|
||||
import re
|
||||
import json
|
||||
import stat
|
||||
import time
|
||||
try:
|
||||
import apsw
|
||||
except ImportError as e:
|
||||
pass
|
||||
from .misc import *
|
||||
from .crypt import *
|
||||
|
||||
@@ -76,6 +81,26 @@ def download_url(url, filename):
|
||||
return (True, ("OK", 200 ))
|
||||
|
||||
|
||||
def expire_database_create():
|
||||
connection = apsw.Connection(app.config['SQLITE_FILE'])
|
||||
cursor = connection.cursor()
|
||||
try:
|
||||
cursor.execute("""CREATE TABLE IF NOT EXISTS expiring (
|
||||
hash text PRIMARY KEY,
|
||||
file text NOT NULL,
|
||||
expires integer NOT NULL
|
||||
);""")
|
||||
cursor.execute("DELETE FROM expiring WHERE expires < ?",
|
||||
(
|
||||
time.time(),
|
||||
)
|
||||
)
|
||||
except apsw.BusyError as e:
|
||||
# Other thread is creating the database
|
||||
pass
|
||||
set_rights(app.config['SQLITE_FILE'])
|
||||
|
||||
|
||||
def file_autoremove(path, share, notifier = None):
|
||||
autoremove = get_or_none('autoremove', share, 0)
|
||||
if autoremove == 0:
|
||||
@@ -207,6 +232,17 @@ def get_download_url(share, file, token):
|
||||
))
|
||||
|
||||
|
||||
def get_expiring_file(ehash):
|
||||
connection = apsw.Connection(app.config['SQLITE_FILE'])
|
||||
cursor = connection.cursor()
|
||||
|
||||
for row in cursor.execute("SELECT file, expires FROM expiring WHERE hash = ?",
|
||||
( ehash, )
|
||||
):
|
||||
return row[0], row[1]
|
||||
return None, None
|
||||
|
||||
|
||||
def get_script_url(public_url, share, end_point, token = "[TOKEN]"):
|
||||
cmd = None
|
||||
doc = None
|
||||
@@ -267,6 +303,7 @@ def read_config(app):
|
||||
app.config['SITE_NAME'] = config_values['site_name']
|
||||
app.config['UPLOAD_FOLDER'] = config_values['data_folder']
|
||||
app.config['SHARES_FILE'] = config_values['shares_file']
|
||||
app.config['SQLITE_FILE'] = config_values['sqlite_file']
|
||||
if 'log_file' in config_values:
|
||||
app.config['LOG_FILE'] = config_values['log_file']
|
||||
else:
|
||||
@@ -294,3 +331,41 @@ def set_rights(path):
|
||||
os.chmod(path, st.st_mode | stat.S_IRUSR | stat.S_IWUSR)
|
||||
if app.config['GID'] > 0:
|
||||
os.chmod(path, st.st_mode | stat.S_IRGRP | stat.S_IWGRP)
|
||||
|
||||
|
||||
def set_expiring_file(share, filename, expires):
|
||||
connection = apsw.Connection(app.config['SQLITE_FILE'])
|
||||
cursor = connection.cursor()
|
||||
|
||||
while True:
|
||||
ehash = random_expiring_hash()
|
||||
matches = len(list(cursor.execute("SELECT file FROM expiring WHERE hash = ?",
|
||||
( ehash, )
|
||||
)))
|
||||
if matches == 0:
|
||||
break
|
||||
|
||||
cursor.execute("INSERT INTO expiring (hash, file, expires) VALUES (?,?,?)",
|
||||
(
|
||||
ehash,
|
||||
filename,
|
||||
expires
|
||||
)
|
||||
)
|
||||
return "/".join((
|
||||
app.config['PUBLIC_URL'],
|
||||
'e',
|
||||
ehash,
|
||||
os.path.basename(filename)
|
||||
))
|
||||
|
||||
|
||||
def remove_expiring_file(share, filename):
|
||||
connection = apsw.Connection(app.config['SQLITE_FILE'])
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("DELETE FROM expiring WHERE file = ?",
|
||||
(
|
||||
filename,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user