From ee281cb6ef11b8e115e02032a7c899927d2e92e5 Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Fri, 16 Aug 2024 15:23:07 +0800 Subject: chore: init project --- Pipefile | 10 +++ app.py | 127 +++++++++++++++++++++++++++++ files/109951167368046405.jpg | Bin 0 -> 25916 bytes files/3B42EE8DE456CDB990D27EAC440A150C.jpg | Bin 0 -> 238970 bytes files/GoodbyeDPI-master.zip | Bin 0 -> 68677 bytes files/security-key.txt | 1 + requirements.txt | 1 + static/style.css | 0 templates/file_info.html | 25 ++++++ templates/files.html | 17 ++++ templates/index.html | 0 templates/login.html | 19 +++++ templates/upload.html | 16 ++++ 13 files changed, 216 insertions(+) create mode 100644 Pipefile create mode 100644 app.py create mode 100644 files/109951167368046405.jpg create mode 100644 files/3B42EE8DE456CDB990D27EAC440A150C.jpg create mode 100644 files/GoodbyeDPI-master.zip create mode 100644 files/security-key.txt create mode 100644 requirements.txt create mode 100644 static/style.css create mode 100644 templates/file_info.html create mode 100644 templates/files.html create mode 100644 templates/index.html create mode 100644 templates/login.html create mode 100644 templates/upload.html diff --git a/Pipefile b/Pipefile new file mode 100644 index 0000000..5ece07a --- /dev/null +++ b/Pipefile @@ -0,0 +1,10 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +flask = "*" + +[requires] +python_version = "3.9" \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..7c3810e --- /dev/null +++ b/app.py @@ -0,0 +1,127 @@ +from flask import Flask, render_template, request, redirect, send_file, url_for, send_from_directory, session, flash +from werkzeug.utils import secure_filename +import os +import zipfile +import io +import urllib.parse + + +app = Flask(__name__) +app.secret_key = 'supersecretkey' +app.config['UPLOAD_FOLDER'] = 'files' +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB + +ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'zip'} + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +@app.route('/') +def index(): + if 'username' in session: + return redirect(url_for('upload_file')) + return redirect(url_for('login')) + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + # 简单的用户名和密码验证 + if username == 'admin' and password == 'password': + session['username'] = username + return redirect(url_for('upload_file')) + else: + flash('Invalid credentials') + return render_template('login.html') + +@app.route('/logout') +def logout(): + session.pop('username', None) + return redirect(url_for('login')) + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if 'username' not in session: + return redirect(url_for('login')) + if request.method == 'POST': + if 'file' not in request.files: + flash('No file part') + return redirect(request.url) + file = request.files['file'] + if file.filename == '': + flash('No selected file') + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + flash('File successfully uploaded') + return redirect(url_for('uploaded_files')) + return render_template('upload.html') + +@app.route('/files') +def uploaded_files(): + if 'username' not in session: + return redirect(url_for('login')) + files = os.listdir(app.config['UPLOAD_FOLDER']) + return render_template('files.html', files=files) + +@app.route('/files/') +def file_info(filename): + if 'username' not in session: + return redirect(url_for('login')) + file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) + if os.path.exists(file_path): + file_ext = filename.rsplit('.', 1)[1].lower() + if file_ext in {'png', 'jpg', 'jpeg', 'gif'}: + file_type = 'image' + elif file_ext in {'txt', 'pdf'}: + file_type = 'text' + elif file_ext == 'zip': + file_type = 'zip' + with zipfile.ZipFile(file_path, 'r') as zip_ref: + zip_contents = zip_ref.namelist() + return render_template('file_info.html', filename=filename, file_type=file_type, zip_contents=zip_contents) + else: + file_type = 'other' + return render_template('file_info.html', filename=filename, file_type=file_type) + else: + flash('File not found') + return redirect(url_for('uploaded_files')) + +@app.route('/files//') +def view_inner_file(zip_filename, inner_filename): + if 'username' not in session: + return redirect(url_for('login')) + zip_path = os.path.join(app.config['UPLOAD_FOLDER'], zip_filename) + if os.path.exists(zip_path): + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + if inner_filename in zip_ref.namelist(): + file_ext = inner_filename.rsplit('.', 1)[1].lower() + if file_ext in {'png', 'jpg', 'jpeg', 'gif'}: + file_type = 'image' + file_data = zip_ref.read(inner_filename) + return send_file(io.BytesIO(file_data), mimetype=f'image/{file_ext}') + elif file_ext in {'txt', 'pdf'}: + file_type = 'text' + file_data = zip_ref.read(inner_filename) + return send_file(io.BytesIO(file_data), mimetype=f'application/{file_ext}') + else: + flash('Unsupported file type inside zip') + return redirect(url_for('file_info', filename=zip_filename)) + else: + flash('File not found inside zip') + return redirect(url_for('file_info', filename=zip_filename)) + else: + flash('Zip file not found') + return redirect(url_for('uploaded_files')) + + +@app.route('/download/') +def download_file(filename): + if 'username' not in session: + return redirect(url_for('login')) + return send_from_directory(app.config['UPLOAD_FOLDER'], filename) + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/files/109951167368046405.jpg b/files/109951167368046405.jpg new file mode 100644 index 0000000..78c5593 Binary files /dev/null and b/files/109951167368046405.jpg differ diff --git a/files/3B42EE8DE456CDB990D27EAC440A150C.jpg b/files/3B42EE8DE456CDB990D27EAC440A150C.jpg new file mode 100644 index 0000000..9a57384 Binary files /dev/null and b/files/3B42EE8DE456CDB990D27EAC440A150C.jpg differ diff --git a/files/GoodbyeDPI-master.zip b/files/GoodbyeDPI-master.zip new file mode 100644 index 0000000..4462fb2 Binary files /dev/null and b/files/GoodbyeDPI-master.zip differ diff --git a/files/security-key.txt b/files/security-key.txt new file mode 100644 index 0000000..4fa34a5 --- /dev/null +++ b/files/security-key.txt @@ -0,0 +1 @@ +EsTw da7P ZCmG bw2F Mvhd BJHQ TMDf dVT3 vXBp 861C q6P4 PH9q \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2db5aea --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask==2.0.2 \ No newline at end of file diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..e69de29 diff --git a/templates/file_info.html b/templates/file_info.html new file mode 100644 index 0000000..9e88c64 --- /dev/null +++ b/templates/file_info.html @@ -0,0 +1,25 @@ + + + + + File Info + + +

File Info: {{ filename }}

+ {% if file_type == 'image' %} + {{ filename }} + {% elif file_type == 'text' %} + + {% elif file_type == 'zip' %} +

Contents of {{ filename }}:

+
    + {% for item in zip_contents %} +
  • {{ item }}
  • + {% endfor %} +
+ {% else %} +

Cannot display this file type.

+ {% endif %} + Back to files + + \ No newline at end of file diff --git a/templates/files.html b/templates/files.html new file mode 100644 index 0000000..5095242 --- /dev/null +++ b/templates/files.html @@ -0,0 +1,17 @@ + + + + + Uploaded Files + + +

Uploaded Files

+
    + {% for file in files %} +
  • {{ file }}
  • + {% endfor %} +
+ Upload More Files + Logout + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..e0c5d68 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,19 @@ + + + + + Login + + +

Login

+
+ + +
+ + +
+ +
+ + \ No newline at end of file diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..e024330 --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,16 @@ + + + + + Upload File + + +

Upload File

+
+ + +
+ View Uploaded Files + Logout + + \ No newline at end of file -- cgit v1.2.3-70-g09d2