def upload_file(): if request.method == 'POST': controller.clean_files(dbfile=app.config['FILE_LIST'], expire=app.config['EXPIRE']) return controller.upload_file(request=request, config=app.config) else: # In case no file, return help return abort(404)
def list_all_files(): try: if 'ls' in app.config['DISABLED_FEATURE']: LOG.info("[LS] Tried to call /ls but this url is disabled") return 'Administrator disabled the /ls option.\n' except (KeyError, TypeError): pass controller.clean_files(dbfile=app.config['FILE_LIST'], expire=app.config['EXPIRE']) return jsonify(controller.get_all_files(request=request, config=app.config))
def list_all_files(): try: if 'ls' in app.config['DISABLED_FEATURE']: LOG.info("[LS] Tried to call /ls but this url is disabled") return 'Administrator disabled the /ls option.\n' except (KeyError, TypeError): pass controller.clean_files(dbfile=app.config['FILE_LIST'], expire=app.config['EXPIRE']) return jsonify(controller.get_all_files( request=request, config=app.config))
def test_clean_files(self): # Try to upload 2 file and force one to expire in the db. # file 1 _file1 = osjoin(self.testdir, 'test_file1') file1_md5 = write_random_file(_file1) self.app.post('/', data={ 'file': (open(_file1, 'r'), 'test_pastefile_random1.file'), }) # file 2 _file2 = osjoin(self.testdir, 'test_file2') file2_md5 = write_random_file(_file2) self.app.post('/', data={ 'file': (open(_file2, 'r'), 'test_pastefile_random2.file'), }) # Should do nothing, no file expired controller.clean_files(dbfile=flaskr.app.config['FILE_LIST']) for md5 in [file1_md5, file2_md5]: self.assertTrue( os.path.isfile(osjoin(flaskr.app.config['UPLOAD_FOLDER'], md5))) # Set expire on one file with JsonDB(dbfile=flaskr.app.config['FILE_LIST']) as db: db.db[file2_md5]['timestamp'] = 0 # if we can't lock the database should do noting with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): controller.clean_files(dbfile=flaskr.app.config['FILE_LIST']) for md5 in [file1_md5, file2_md5]: self.assertTrue( os.path.isfile(osjoin(flaskr.app.config['UPLOAD_FOLDER'], md5))) # If we acquire the lock, file2 should be removed on disk and db controller.clean_files(dbfile=flaskr.app.config['FILE_LIST']) self.assertTrue( os.path.isfile( osjoin(flaskr.app.config['UPLOAD_FOLDER'], file1_md5))) self.assertFalse( os.path.isfile( osjoin(flaskr.app.config['UPLOAD_FOLDER'], file2_md5))) with JsonDB(dbfile=flaskr.app.config['FILE_LIST']) as db: self.assertTrue(file1_md5 in db.db.keys()) self.assertFalse(file2_md5 in db.db.keys())