Skip to content

Commit

Permalink
[+] Add expiring time support to be able to retrieve (or not) the link
Browse files Browse the repository at this point in the history
[+] Fix oslo config loading and nopaste.conf options
[+] Add helper function to check expiring time
[+] Make sqlite_middleware more generic
  • Loading branch information
fmount committed Dec 7, 2017
1 parent a63c7c9 commit f1dfb18
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
9 changes: 6 additions & 3 deletions basicpastev2.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"""

LOG = logging.getLogger(__name__)
#logging.basicConfig(filename='/tmp/nopaste.log', level=logging.DEBUG)

app = Flask(__name__, static_folder=CONF.default.upload_folder, static_url_path="")
api = Api(app)
Expand Down Expand Up @@ -98,10 +99,12 @@ def home():
def show_me_thefile(url):
identifier = Shorturl.toBase10(url)
LOG.info("Resolved identifier: %s\n" % str(identifier))

Session = sessionmaker(bind=engine)
if sqlite_middleware._find_object(identifier, Session()) is None or not \
os.path.exists(CONF.default.upload_folder + "/" + url):
if sqlite_middleware._get_object(identifier, Session()) is None or not \
os.path.exists(CONF.default.upload_folder + "/" + url) or \
helper.is_expired(sqlite_middleware._get_object(\
identifier, Session()).timestamp, CONF.default.expire_time):
abort(404)

LOG.info("[Rendering] %s\n" % str(CONF.default.upload_folder + "/" + url))
Expand Down
17 changes: 9 additions & 8 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from oslo_config import cfg


# Register the stanza
opt_default_group = cfg.OptGroup(name='default', \
title='Default Options')
Expand Down Expand Up @@ -62,6 +63,7 @@


CONF = cfg.CONF
CONF(default_config_files=['config/nopaste.conf'])
CONF.register_group(opt_default_group)
CONF.register_opts(default_opts, opt_default_group)

Expand All @@ -72,11 +74,10 @@
CONF.default.port = CONF.default.uri.split(":")[2]



if __name__ == '__main__':
print(CONF.default.upload_folder)
#print(CONF.default.upload_folder)
#print(CONF.default.uri)
#print(CONF.default.debug)
#print(CONF.database.dbname)
#print(CONF.database.sql_engine_prefix)
#if __name__ == '__main__':
#print(CONF.default.upload_folder)
#print(CONF.default.upload_folder)
#print(CONF.default.uri)
#print(CONF.default.debug)
#print(CONF.database.dbname)
#print(CONF.database.sql_engine_prefix)
2 changes: 1 addition & 1 deletion config/nopaste.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
upload_folder="uploads"
templates_folder="templates"
uri="http://localhost:5000/"
debug=False
debug=True
expire_time=86400


Expand Down
8 changes: 4 additions & 4 deletions lib/resources/userapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def get(self, uuid):
def post(self):
username = request.json.get('username')
password = request.json.get('password')


if username is None or password is None:
abort(400) # missing arguments

engine = create_engine(CONF.database.sql_engine_prefix + CONF.database.dbname)
Session = sessionmaker(bind=engine)

print(password)
if sqlite_middleware._get_user(username, Session()) is not None:
print(username)
Expand Down
2 changes: 1 addition & 1 deletion sqlite_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _insert_user(user, session):
session.commit()


def _find_object(uid, session):
def _find_objects(uid, session):
result = session.query(Link).filter_by(uuid=uid).all()
return result

Expand Down
25 changes: 20 additions & 5 deletions utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from flask import render_template, make_response, Response
from bs4 import BeautifulSoup
from config import CONF
from datetime import datetime as dt, timedelta
import logging

LOG = logging.getLogger(__name__)
Expand All @@ -42,9 +42,24 @@ def render(resource, ua):
return content


def isexpired(t1):
def is_expired(t1, expire_time):
"""
TBD: Define the mode of setting link expired
Define the mode of setting link expired
"""
print("The expire time is set to: %d\n" % int(CONF.default.expire_time))
pass
LOG.info("Timedelta seconds: %d\n" % int(expire_time))
now = dt.now()
link_dt = dt.strptime(t1, "%Y-%m-%d %H:%M:%S")

if abs(now - link_dt) > timedelta(seconds=expire_time):
LOG.info("[EXPIRED] - TRUE")
return True

LOG.info("[EXPIRED] - FALSE")
return False


# JUST FOR TEST PURPOSES ...
#print(is_expired("2017-12-01 20:32:00", 86400))
#print(is_expired("2017-12-06 15:32:00", 86400))
#print(is_expired("2017-12-07 20:32:00", 86400))
#print(is_expired("2017-12-05 20:32:00", 86400))

0 comments on commit f1dfb18

Please sign in to comment.