def shorten(): url = request.form.get('url') if url is None: return utils.response(HTTPStatus.BAD_REQUEST, 'Invalid URL') short_url = ShortUrl(url) if short_url.is_valid() is False: return utils.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'Invalid URL') # Add URL to database short_url.add() # Create HMAC for URL using token token = short_url.get_token() hmac_hash = utils.create_hmac_hash(token, current_app.secret_key) # Create URLs short_url = url_for('main.short_url', token=token, _external=True) delete_url = url_for('api.delete_url', hmac_hash=hmac_hash, token=token, _external=True) # Send data to Discord webhook discord_webhook.send(short_url, delete_url, EmbedType.SHORT_URL, original_url=url, shortened_url=short_url) return jsonify({'url': short_url, 'delete_url': delete_url})
def delete_url(hmac_hash, token): new_hmac_hash = utils.create_hmac_hash(token, current_app.secret_key) # If hash does not match if utils.is_valid_hash(hmac_hash, new_hmac_hash) is False: return abort(404) if ShortUrl.delete(token) is False: return abort(404) return utils.response(message='This short URL has been deleted, you can now close this page')
def delete_file(hmac_hash, filename): new_hmac_hash = utils.create_hmac_hash(filename, current_app.secret_key) # If hash does not match if utils.is_valid_hash(hmac_hash, new_hmac_hash) is False: return abort(404) if File.delete(filename) is False: return abort(404) message = render_template_string('{{ filename }} has been deleted, you can now close this page', filename=filename) return utils.response(message=message)
def upload(): uploaded_file = request.files.get('file') if uploaded_file is None: return utils.response(400, 'Invalid file') # Our own class which utilises werkzeug.datastructures.FileStorage f = File(uploaded_file) # Convert HTTP header value to boolean use_og_filename = request.headers.get('X-Use-Original-Filename', type=int) == 1 f.use_original_filename = use_og_filename # Get the filename filename = f.get_filename() # Save the file f.save() # Generate HMAC hash using Flask's secret key and filename hmac_hash = utils.create_hmac_hash(filename, current_app.secret_key) # Create URLs file_url = url_for('main.uploads', filename=filename, _external=True) delete_url = url_for('api.delete_file', hmac_hash=hmac_hash, filename=filename, _external=True) # Send data to Discord webhook if discord_webhook.is_enabled: discord_webhook.embed( title='New file has been uploaded!', description=file_url, url=file_url, deletion_url=delete_url, is_file=True ) discord_webhook.execute(remove_embeds=True) # Return JSON return jsonify( { 'url': file_url, 'delete_url': delete_url } )
def upload(): uploaded_file = request.files.get('file') if uploaded_file is None: return utils.response(HTTPStatus.BAD_REQUEST, 'Invalid file') # Our own class which utilises werkzeug.datastructures.FileStorage f = File(uploaded_file) # Check if file is allowed if f.is_allowed() is False: return utils.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'Invalid file type') # Convert HTTP header value to boolean use_og_filename = request.headers.get('X-Use-Original-Filename', type=int) == 1 f.use_original_filename = use_og_filename # Get the filename filename = f.get_filename() # Save the file f.save() # Generate HMAC hash using Flask's secret key and filename hmac_hash = utils.create_hmac_hash(filename, current_app.secret_key) # Create URLs file_url = url_for('main.uploads', filename=filename, _external=True) delete_url = url_for('api.delete_file', hmac_hash=hmac_hash, filename=filename, _external=True) # Send data to Discord webhook discord_webhook.send(file_url, delete_url, EmbedType.FILE) # Return JSON return jsonify({'url': file_url, 'delete_url': delete_url})
def shorten(): url = request.form.get('url') if url is None: return utils.response(400, 'Invalid URL') short_url = ShortUrl(url) if short_url.is_valid() is False: return utils.response(400, 'Invalid URL') # Add URL to database short_url.add() # Create HMAC for URL using token token = short_url.get_token() hmac_hash = utils.create_hmac_hash(token, current_app.secret_key) # Create URLs short_url = url_for('main.short_url', token=token, _external=True) delete_url = url_for('api.delete_url', hmac_hash=hmac_hash, token=token, _external=True) # Send data to Discord webhook if discord_webhook.is_enabled: discord_webhook.embed( title='URL has been shortened!', description='{} => {}'.format(url, short_url), url=short_url, deletion_url=delete_url ) discord_webhook.execute(remove_embeds=True) return jsonify( { 'url': short_url, 'delete_url': delete_url } )