diff --git a/basicpastev2.py b/basicpastev2.py index e336207..a55385e 100644 --- a/basicpastev2.py +++ b/basicpastev2.py @@ -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) @@ -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)) diff --git a/config.py b/config.py index 6467229..b7dc175 100644 --- a/config.py +++ b/config.py @@ -24,6 +24,7 @@ from oslo_config import cfg + # Register the stanza opt_default_group = cfg.OptGroup(name='default', \ title='Default Options') @@ -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) @@ -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) diff --git a/config/nopaste.conf b/config/nopaste.conf index 5787955..43d478f 100644 --- a/config/nopaste.conf +++ b/config/nopaste.conf @@ -2,7 +2,7 @@ upload_folder="uploads" templates_folder="templates" uri="http://localhost:5000/" -debug=False +debug=True expire_time=86400 diff --git a/lib/resources/userapi.py b/lib/resources/userapi.py index 52846dc..8e7d4d7 100644 --- a/lib/resources/userapi.py +++ b/lib/resources/userapi.py @@ -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) diff --git a/sqlite_middleware.py b/sqlite_middleware.py index 92d9bd0..3eb7001 100644 --- a/sqlite_middleware.py +++ b/sqlite_middleware.py @@ -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 diff --git a/utils/helper.py b/utils/helper.py index 40f47ac..d7c21bf 100644 --- a/utils/helper.py +++ b/utils/helper.py @@ -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__) @@ -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))