|
| 1 | +# ========================================================== |
| 2 | +# FileShare PRO - Secure File Sharing Web App |
| 3 | +# Professional Web Tool |
| 4 | +# ========================================================== |
| 5 | + |
| 6 | +import os |
| 7 | +import uuid |
| 8 | +import hashlib |
| 9 | +from datetime import datetime |
| 10 | + |
| 11 | +from flask import ( |
| 12 | + Flask, |
| 13 | + render_template_string, |
| 14 | + request, |
| 15 | + redirect, |
| 16 | + url_for, |
| 17 | + send_from_directory, |
| 18 | + abort |
| 19 | +) |
| 20 | + |
| 21 | +# =================== APP CONFIG =================== |
| 22 | + |
| 23 | +APP_NAME = "FileShare PRO" |
| 24 | +APP_VERSION = "1.0.0" |
| 25 | + |
| 26 | +UPLOAD_FOLDER = "shared_files" |
| 27 | +MAX_FILE_SIZE = 1024 * 1024 * 500 # 500 MB |
| 28 | + |
| 29 | +os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
| 30 | + |
| 31 | +app = Flask(__name__) |
| 32 | +app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER |
| 33 | +app.config["MAX_CONTENT_LENGTH"] = MAX_FILE_SIZE |
| 34 | + |
| 35 | + |
| 36 | +# =================== MEMORY DATABASE =================== |
| 37 | + |
| 38 | +files_db = {} |
| 39 | + |
| 40 | + |
| 41 | +# =================== UTIL =================== |
| 42 | + |
| 43 | +def generate_id(): |
| 44 | + return str(uuid.uuid4())[:8] |
| 45 | + |
| 46 | + |
| 47 | +def hash_file(path): |
| 48 | + |
| 49 | + md5 = hashlib.md5() |
| 50 | + |
| 51 | + with open(path, "rb") as f: |
| 52 | + for chunk in iter(lambda: f.read(4096), b""): |
| 53 | + md5.update(chunk) |
| 54 | + |
| 55 | + return md5.hexdigest() |
| 56 | + |
| 57 | + |
| 58 | +def get_file_size(path): |
| 59 | + return round(os.path.getsize(path) / 1024 / 1024, 2) |
| 60 | + |
| 61 | + |
| 62 | +# =================== HTML TEMPLATE =================== |
| 63 | + |
| 64 | +PAGE = """ |
| 65 | +
|
| 66 | +<!DOCTYPE html> |
| 67 | +<html> |
| 68 | +<head> |
| 69 | +<title>{{app_name}}</title> |
| 70 | +
|
| 71 | +<style> |
| 72 | +
|
| 73 | +body{ |
| 74 | +font-family:Segoe UI; |
| 75 | +background:#0f172a; |
| 76 | +color:white; |
| 77 | +padding:40px; |
| 78 | +} |
| 79 | +
|
| 80 | +.container{ |
| 81 | +max-width:900px; |
| 82 | +margin:auto; |
| 83 | +} |
| 84 | +
|
| 85 | +h1{ |
| 86 | +color:#38bdf8; |
| 87 | +} |
| 88 | +
|
| 89 | +.upload{ |
| 90 | +margin-bottom:30px; |
| 91 | +padding:20px; |
| 92 | +background:#1e293b; |
| 93 | +border-radius:10px; |
| 94 | +} |
| 95 | +
|
| 96 | +.files{ |
| 97 | +background:#1e293b; |
| 98 | +padding:20px; |
| 99 | +border-radius:10px; |
| 100 | +} |
| 101 | +
|
| 102 | +table{ |
| 103 | +width:100%; |
| 104 | +border-collapse:collapse; |
| 105 | +} |
| 106 | +
|
| 107 | +td,th{ |
| 108 | +padding:10px; |
| 109 | +border-bottom:1px solid #334155; |
| 110 | +} |
| 111 | +
|
| 112 | +a{ |
| 113 | +color:#22c55e; |
| 114 | +text-decoration:none; |
| 115 | +} |
| 116 | +
|
| 117 | +button{ |
| 118 | +background:#38bdf8; |
| 119 | +border:none; |
| 120 | +padding:8px 14px; |
| 121 | +border-radius:6px; |
| 122 | +cursor:pointer; |
| 123 | +} |
| 124 | +
|
| 125 | +</style> |
| 126 | +
|
| 127 | +</head> |
| 128 | +
|
| 129 | +<body> |
| 130 | +
|
| 131 | +<div class="container"> |
| 132 | +
|
| 133 | +<h1>{{app_name}} v{{version}}</h1> |
| 134 | +
|
| 135 | +<div class="upload"> |
| 136 | +
|
| 137 | +<form method="POST" enctype="multipart/form-data"> |
| 138 | +
|
| 139 | +<input type="file" name="file" required> |
| 140 | +<button type="submit">Upload File</button> |
| 141 | +
|
| 142 | +</form> |
| 143 | +
|
| 144 | +</div> |
| 145 | +
|
| 146 | +<div class="files"> |
| 147 | +
|
| 148 | +<h2>Shared Files</h2> |
| 149 | +
|
| 150 | +<table> |
| 151 | +
|
| 152 | +<tr> |
| 153 | +<th>Name</th> |
| 154 | +<th>Size</th> |
| 155 | +<th>Date</th> |
| 156 | +<th>Download</th> |
| 157 | +<th>Delete</th> |
| 158 | +</tr> |
| 159 | +
|
| 160 | +{% for id,file in files.items() %} |
| 161 | +
|
| 162 | +<tr> |
| 163 | +
|
| 164 | +<td>{{file.name}}</td> |
| 165 | +<td>{{file.size}} MB</td> |
| 166 | +<td>{{file.date}}</td> |
| 167 | +
|
| 168 | +<td> |
| 169 | +<a href="/download/{{id}}">Download</a> |
| 170 | +</td> |
| 171 | +
|
| 172 | +<td> |
| 173 | +<a href="/delete/{{id}}">Delete</a> |
| 174 | +</td> |
| 175 | +
|
| 176 | +</tr> |
| 177 | +
|
| 178 | +{% endfor %} |
| 179 | +
|
| 180 | +</table> |
| 181 | +
|
| 182 | +</div> |
| 183 | +
|
| 184 | +</div> |
| 185 | +
|
| 186 | +</body> |
| 187 | +</html> |
| 188 | +
|
| 189 | +""" |
| 190 | + |
| 191 | + |
| 192 | +# =================== ROUTES =================== |
| 193 | + |
| 194 | +@app.route("/", methods=["GET", "POST"]) |
| 195 | +def index(): |
| 196 | + |
| 197 | + if request.method == "POST": |
| 198 | + |
| 199 | + f = request.files["file"] |
| 200 | + |
| 201 | + if not f: |
| 202 | + return redirect("/") |
| 203 | + |
| 204 | + file_id = generate_id() |
| 205 | + |
| 206 | + filename = f.filename |
| 207 | + path = os.path.join(app.config["UPLOAD_FOLDER"], file_id + "_" + filename) |
| 208 | + |
| 209 | + f.save(path) |
| 210 | + |
| 211 | + files_db[file_id] = { |
| 212 | + "name": filename, |
| 213 | + "path": path, |
| 214 | + "size": get_file_size(path), |
| 215 | + "hash": hash_file(path), |
| 216 | + "date": datetime.now().strftime("%Y-%m-%d %H:%M") |
| 217 | + } |
| 218 | + |
| 219 | + return redirect("/") |
| 220 | + |
| 221 | + return render_template_string( |
| 222 | + PAGE, |
| 223 | + app_name=APP_NAME, |
| 224 | + version=APP_VERSION, |
| 225 | + files=files_db |
| 226 | + ) |
| 227 | + |
| 228 | + |
| 229 | +# =================== DOWNLOAD =================== |
| 230 | + |
| 231 | +@app.route("/download/<file_id>") |
| 232 | +def download(file_id): |
| 233 | + |
| 234 | + file = files_db.get(file_id) |
| 235 | + |
| 236 | + if not file: |
| 237 | + abort(404) |
| 238 | + |
| 239 | + directory = os.path.dirname(file["path"]) |
| 240 | + filename = os.path.basename(file["path"]) |
| 241 | + |
| 242 | + return send_from_directory(directory, filename, as_attachment=True) |
| 243 | + |
| 244 | + |
| 245 | +# =================== DELETE =================== |
| 246 | + |
| 247 | +@app.route("/delete/<file_id>") |
| 248 | +def delete(file_id): |
| 249 | + |
| 250 | + file = files_db.get(file_id) |
| 251 | + |
| 252 | + if not file: |
| 253 | + abort(404) |
| 254 | + |
| 255 | + try: |
| 256 | + os.remove(file["path"]) |
| 257 | + except: |
| 258 | + pass |
| 259 | + |
| 260 | + files_db.pop(file_id, None) |
| 261 | + |
| 262 | + return redirect("/") |
| 263 | + |
| 264 | + |
| 265 | +# =================== API =================== |
| 266 | + |
| 267 | +@app.route("/api/files") |
| 268 | +def api_files(): |
| 269 | + |
| 270 | + return { |
| 271 | + "app": APP_NAME, |
| 272 | + "version": APP_VERSION, |
| 273 | + "files": files_db |
| 274 | + } |
| 275 | + |
| 276 | + |
| 277 | +# =================== START =================== |
| 278 | + |
| 279 | +if __name__ == "__main__": |
| 280 | + |
| 281 | + print(f"{APP_NAME} v{APP_VERSION} starting...") |
| 282 | + print("Open browser: http://127.0.0.1:5000") |
| 283 | + |
| 284 | + app.run(host="0.0.0.0", port=5000) |
0 commit comments