def get_file_info(id_file, config, env): infos = get_infos_file_from_md5(md5=id_file, dbfile=config['FILE_LIST']) if not infos: return False try: size = utils.human_readable( os.stat(infos['storage_full_filename']).st_size) expire = datetime.datetime.fromtimestamp( int(infos['timestamp']) + int(config['EXPIRE'])).strftime('%d-%m-%Y %H:%M:%S'), file_infos = { 'name': infos['real_name'], 'md5': id_file, 'burn_after_read': infos['burn_after_read'], 'timestamp': infos['timestamp'], 'expire': expire, 'mime_type': infos['mime_type'], 'type': infos['type'], 'size': size, 'url': "%s/%s" % (utils.build_base_url(env=env), id_file) } return file_infos except: LOG.error('Unable to gather infos for file %s' % id_file) return False
def upload_file(request, config): value_burn_after_read = strtobool(request.form.get('burn', 'false')) if value_burn_after_read: burn_after_read = True else: burn_after_read = False # Write tmp file on disk try: file_md5, tmp_full_filename = utils.write_tmpfile_to_disk( file=request.files['file'], dest_dir=config['TMP_FOLDER']) except IOError: return 'Server error, contact administrator\n' secure_name = secure_filename(request.files['file'].filename) with JsonDB(dbfile=config['FILE_LIST']) as db: # Just inform for debug purpose if db.lock_error: LOG.error("Unable to get lock during file upload %s" % file_md5) # Try to write file on disk and db. Return false if file is not writed storage_full_filename = os.path.join(config['UPLOAD_FOLDER'], file_md5) mime_type = magic.from_file(tmp_full_filename, mime=True) _type = magic.from_file(tmp_full_filename) succed_add_file = add_new_file(filename=secure_name, source=tmp_full_filename, dest=storage_full_filename, mime_type=mime_type, type=_type, db=db, md5=file_md5, burn_after_read=burn_after_read) if not succed_add_file: # In the case the file is not in db, we have 2 reason : # * We was not able to have the lock and write the file in the db. # * Or an error occure during the file processing # In any case just tell the user to try later try: os.remove(tmp_full_filename) except OSError as e: LOG.error("Can't remove tmp file: %s" % e) LOG.info('Unable lock the db and find' 'the file %s in db during upload' % file_md5) return 'Unable to upload the file, try again later ...\n' LOG.info("[POST] Client %s has successfully uploaded: %s (%s)" % (request.remote_addr, storage_full_filename, file_md5)) return "%s/%s\n" % (utils.build_base_url(env=request.environ), file_md5)
def upload_file(request, config): value_burn_after_read = request.form.getlist('burn') if value_burn_after_read: burn_after_read = True else: burn_after_read = False # Write tmp file on disk try: file_md5, tmp_full_filename = utils.write_tmpfile_to_disk(file=request.files['file'], dest_dir=config['TMP_FOLDER']) except IOError: return 'Server error, contact administrator\n' secure_name = secure_filename(request.files['file'].filename) with JsonDB(dbfile=config['FILE_LIST']) as db: # Just inform for debug purpose if db.lock_error: LOG.error("Unable to get lock during file upload %s" % file_md5) # Try to write file on disk and db. Return false if file is not writed storage_full_filename = os.path.join(config['UPLOAD_FOLDER'], file_md5) mime_type = magic.from_file(tmp_full_filename, mime=True) _type = magic.from_file(tmp_full_filename) succed_add_file = add_new_file(filename=secure_name, source=tmp_full_filename, dest=storage_full_filename, mime_type=mime_type, type=_type, db=db, md5=file_md5, burn_after_read=burn_after_read) if not succed_add_file: # In the case the file is not in db, we have 2 reason : # * We was not able to have the lock and write the file in the db. # * Or an error occure during the file processing # In any case just tell the user to try later try: os.remove(tmp_full_filename) except OSError as e: LOG.error("Can't remove tmp file: %s" % e) LOG.info('Unable lock the db and find the file %s in db during upload' % file_md5) return 'Unable to upload the file, try again later ...\n' LOG.info("[POST] Client %s has successfully uploaded: %s (%s)" % (request.remote_addr, storage_full_filename, file_md5)) return "%s/%s\n" % (utils.build_base_url(env=request.environ), file_md5)
def page_not_found(e): # request.method == 'GET' base_url = utils.build_base_url(env=request.environ) helps = ( ("Upload a file:", "curl %s -F file=@**filename**" % base_url), ("View all uploaded files:", "curl %s/ls" % base_url), ("Get infos about one file:", "curl %s/**file_id**/infos" % base_url), ("Get a file:", "curl -JO %s/**file_id**" % base_url), ("Delete a file:", "curl -XDELETE %s/**id**" % base_url), ("Create an alias for cli usage", 'pastefile() { curl -F file=@"$1" %s; }' % base_url), ) context = {'user_agent': request.headers.get('User-Agent', ''), 'helps': helps} return render_template('404.html', **context), 404
def page_not_found(e): # request.method == 'GET' base_url = utils.build_base_url(env=request.environ) helps = ( ("Upload a file:", "curl %s -F file=@**filename**" % base_url), ("Upload a file with burn after read:", "curl %s -F burn=true -F file=@**filename**" % base_url), ("View all uploaded files:", "curl %s/ls" % base_url), ("Get infos about one file:", "curl %s/**file_id**/infos" % base_url), ("Get a file:", "curl -JO %s/**file_id**" % base_url), ("Delete a file:", "curl -XDELETE %s/**id**" % base_url), ("Create an alias for cli usage", 'pastefile() { curl -F file=@"$1" %s; }' % base_url), ) context = {'user_agent': request.headers.get('User-Agent', ''), 'helps': helps} return render_template('404.html', **context), 404
def get_file_info(id_file, config, env): infos = get_infos_file_from_md5(md5=id_file, dbfile=config['FILE_LIST']) if not infos: return False try: size = utils.human_readable(os.stat(infos['storage_full_filename']).st_size) expire = datetime.datetime.fromtimestamp( int(infos['timestamp']) + int(config['EXPIRE'])).strftime('%d-%m-%Y %H:%M:%S'), file_infos = { 'name': infos['real_name'], 'md5': id_file, 'burn_after_read': infos['burn_after_read'], 'timestamp': infos['timestamp'], 'expire': expire, 'mime_type': infos['mime_type'], 'type': infos['type'], 'size': size, 'url': "%s/%s" % (utils.build_base_url(env=env), id_file) } return file_infos except: LOG.error('Unable to gather infos for file %s' % id_file) return False