|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +from werkzeug.security import safe_join |
| 4 | +from flask import Blueprint, jsonify, url_for, abort, request |
| 5 | + |
| 6 | +from app import config, logger |
| 7 | +from app.core.utils import ( |
| 8 | + auth_required, |
| 9 | + create_hmac_hash, |
| 10 | + safe_str_comparison |
| 11 | +) |
| 12 | +from app.core.discord import ( |
| 13 | + create_discord_webhooks, |
| 14 | + create_uploaded_file_embed, |
| 15 | + execute_webhooks_with_embed |
| 16 | +) |
| 17 | +from app.core.files import ( |
| 18 | + get_file_extension_from_file, |
| 19 | + is_file_extension_allowed, |
| 20 | + create_directory, |
| 21 | + generate_filename, |
| 22 | + get_secure_filename, |
| 23 | + delete_file, |
| 24 | +) |
| 25 | + |
| 26 | +files = Blueprint('files', __name__) |
| 27 | + |
| 28 | +@files.get('/sharex/upload') |
| 29 | +def upload_config(): |
| 30 | + return jsonify({ |
| 31 | + "Name": "{} (File uploader)".format(request.host), |
| 32 | + "Version": "1.0.0", |
| 33 | + "DestinationType": "ImageUploader, FileUploader", |
| 34 | + "RequestMethod": "POST", |
| 35 | + "RequestURL": url_for('files.upload', _external=True), |
| 36 | + "Body": "MultipartFormData", |
| 37 | + "FileFormName": "file", |
| 38 | + "URL": "$json:url$", |
| 39 | + "DeletionURL": "$json:delete_url$", |
| 40 | + "Headers": { |
| 41 | + "Authorization": "YOUR-UPLOAD-PASSWORD-HERE", |
| 42 | + }, |
| 43 | + "ErrorMessage": "$json:status$" |
| 44 | + }) |
| 45 | + |
| 46 | +@files.post('/upload') |
| 47 | +@auth_required |
| 48 | +def upload(): |
| 49 | + f = request.files.get('file') |
| 50 | + |
| 51 | + if f is None: |
| 52 | + abort(HTTPStatus.BAD_REQUEST, 'Invalid file.') |
| 53 | + |
| 54 | + file_extension = get_file_extension_from_file(f.stream, config.magic_buffer_bytes) |
| 55 | + |
| 56 | + if is_file_extension_allowed(file_extension, config.allowed_extensions) is False: |
| 57 | + abort(HTTPStatus.UNPROCESSABLE_ENTITY, 'Invalid file type.') |
| 58 | + |
| 59 | + create_directory(config.upload_directory) |
| 60 | + |
| 61 | + filename = generate_filename() |
| 62 | + |
| 63 | + if config.use_original_filename: |
| 64 | + secure_filename = get_secure_filename(f.filename) |
| 65 | + filename = f'{filename}-{secure_filename}' |
| 66 | + |
| 67 | + save_filename = filename + file_extension |
| 68 | + save_path = safe_join(config.upload_directory, save_filename) |
| 69 | + |
| 70 | + f.save(save_path) |
| 71 | + |
| 72 | + hmac_hash = create_hmac_hash(save_filename, config.flask_secret) |
| 73 | + file_url = url_for('main.uploads', filename=save_filename, _external=True) |
| 74 | + deletion_url = url_for('files.delete_file_with_hash', hmac_hash=hmac_hash, filename=save_filename, _external=True) |
| 75 | + |
| 76 | + logger.info(f'Saved file: {save_filename}, URL: {file_url}, deletion URL: {deletion_url}') |
| 77 | + |
| 78 | + # Send data to Discord webhooks |
| 79 | + discord_webhooks = create_discord_webhooks(config.discord_webhook_urls, config.discord_webhook_timeout) |
| 80 | + if discord_webhooks: |
| 81 | + embed = create_uploaded_file_embed(file_url, deletion_url) |
| 82 | + execute_webhooks_with_embed(discord_webhooks, embed) |
| 83 | + |
| 84 | + # Return JSON |
| 85 | + return jsonify(url=file_url, delete_url=deletion_url) |
| 86 | + |
| 87 | +@files.get('/delete-file/<hmac_hash>/<filename>') |
| 88 | +def delete_file_with_hash(hmac_hash, filename): |
| 89 | + new_hmac_hash = create_hmac_hash(filename, config.flask_secret) |
| 90 | + |
| 91 | + # If digest is invalid |
| 92 | + if safe_str_comparison(hmac_hash, new_hmac_hash) is False: |
| 93 | + abort(HTTPStatus.NOT_FOUND) |
| 94 | + |
| 95 | + file_path = safe_join(config.upload_directory, filename) |
| 96 | + file_deleted = delete_file(file_path) |
| 97 | + |
| 98 | + if file_deleted is False: |
| 99 | + abort(HTTPStatus.GONE) |
| 100 | + |
| 101 | + logger.info(f'Deleted a file {filename}') |
| 102 | + |
| 103 | + return jsonify(message='This file has been deleted, you can now close this page.') |
0 commit comments