|
|
@ -21,8 +21,8 @@ def index(): |
|
|
|
if hash is None: |
|
|
|
if hash is None: |
|
|
|
return redirect(url_for('index')) |
|
|
|
return redirect(url_for('index')) |
|
|
|
|
|
|
|
|
|
|
|
death = calcDeath(form.retention.data, form.retention_type.data) |
|
|
|
death = calcDeath(form.retention.data, int(form.retention_type.data)) |
|
|
|
view_counter = None if form.retention_type.data != "Time" else form.retention.data |
|
|
|
view_counter = None if form.retention_type.data != 4 else form.retention.data |
|
|
|
if death is None and view_counter is None: |
|
|
|
if death is None and view_counter is None: |
|
|
|
app.logger.warning("Neither death nor view_counter was recieved for url") |
|
|
|
app.logger.warning("Neither death nor view_counter was recieved for url") |
|
|
|
flash('Please specify a retention time') |
|
|
|
flash('Please specify a retention time') |
|
|
@ -32,7 +32,7 @@ def index(): |
|
|
|
db.session.add(url) |
|
|
|
db.session.add(url) |
|
|
|
db.session.commit() |
|
|
|
db.session.commit() |
|
|
|
death = None if url.death is None else url.death.strftime('%Y-%m-%d %H:%M:%S') |
|
|
|
death = None if url.death is None else url.death.strftime('%Y-%m-%d %H:%M:%S') |
|
|
|
app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}") |
|
|
|
app.logger.info(f"{getClientIp()} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}") |
|
|
|
link = url_for("resolve_hash", hash=hash, _external = True) |
|
|
|
link = url_for("resolve_hash", hash=hash, _external = True) |
|
|
|
flash(Markup(f'Your url is shortend to <a href="{link}">{link}</a>')) |
|
|
|
flash(Markup(f'Your url is shortend to <a href="{link}">{link}</a>')) |
|
|
|
return redirect(url_for('index')) |
|
|
|
return redirect(url_for('index')) |
|
|
@ -48,14 +48,20 @@ def getHash(): |
|
|
|
flash('Failed generating a unique hash. Please try again.') |
|
|
|
flash('Failed generating a unique hash. Please try again.') |
|
|
|
return None |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getClientIp(): |
|
|
|
|
|
|
|
if app.config['REVERSE_PROXY'] == 0: |
|
|
|
|
|
|
|
return request.remote_addr |
|
|
|
|
|
|
|
if app.config['REVERSE_PROXY'] == 1: |
|
|
|
|
|
|
|
return request.environ['HTTP_X_FORWARDED_FOR'] |
|
|
|
|
|
|
|
|
|
|
|
def calcDeath(retention, retention_type): |
|
|
|
def calcDeath(retention, retention_type): |
|
|
|
if retention_type == 'Minute': |
|
|
|
if retention_type == 1: |
|
|
|
return datetime.now() + timedelta(minutes=retention) |
|
|
|
return datetime.now() + timedelta(minutes=retention) |
|
|
|
if retention_type == 'Hour': |
|
|
|
if retention_type == 2: |
|
|
|
return datetime.now() + timedelta(hours=retention) |
|
|
|
return datetime.now() + timedelta(hours=retention) |
|
|
|
if retention_type == 'Day': |
|
|
|
if retention_type == 3: |
|
|
|
return datetime.now() + timedelta(days=retention) |
|
|
|
return datetime.now() + timedelta(days=retention) |
|
|
|
if retention_type == 'Time': |
|
|
|
if retention_type == 4: |
|
|
|
return None |
|
|
|
return None |
|
|
|
app.logger.error(f"Retention_type out of range: '{retention_type}'") |
|
|
|
app.logger.error(f"Retention_type out of range: '{retention_type}'") |
|
|
|
|
|
|
|
|
|
|
@ -66,11 +72,11 @@ def resolve_hash(hash): |
|
|
|
return redirect(url_for('index')) |
|
|
|
return redirect(url_for('index')) |
|
|
|
|
|
|
|
|
|
|
|
url = Url.query.filter_by(hash=hash).first_or_404() |
|
|
|
url = Url.query.filter_by(hash=hash).first_or_404() |
|
|
|
if check_url(url) is None: |
|
|
|
if check_url(url) is not None: abort(404) |
|
|
|
if url.view_counter is not None: |
|
|
|
if url.view_counter is not None: |
|
|
|
url.view_counter -= 1 |
|
|
|
url.view_counter -= 1 |
|
|
|
db.session.commit() |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
countermessage = "" if url.view_counter is None else f". View counter was lowered to {url.view_counter}" |
|
|
|
countermessage = "" if url.view_counter is None else f". View counter was lowered to {url.view_counter}" |
|
|
|
app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}") |
|
|
|
app.logger.info(f"{getClientIp()} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}") |
|
|
|
return redirect(url.url, 301) |
|
|
|
return redirect(url.url, 301) |
|
|
|