#!/usr/bin/env python -- coding: utf-8 -- from pathlib import Path from threading import…
- User
#!/usr/bin/env python
-- coding: utf-8 --
from pathlib import Path from threading import Lock from collections import defaultdict import shutil import argparse import uuid import zlib
from bottle import route, run, request, error, response, HTTPError, static_file from werkzeug.utils import secure_filename
storage_path: Path = Path(file).parent / "storage" chunk_path: Path = Path(file).parent / "chunk"
allow_downloads = True dropzone_cdn = "https://cdnjs.cloudflare.com/ajax/libs/dropzone" dropzone_version = "5.7.6" dropzone_timeout = "120000" dropzone_max_file_size = "100000" dropzone_chunk_size = "1000000" dropzone_parallel_chunks = "true" dropzone_force_chunking = "true"
lock = Lock() chucks = defaultdict(list)
@error(500) def handle_500(error_message): response.status = 500 response.body = f"Error: {error_message}" return response
@route("/") def index(): index_file = Path(file) / "index.html" if index_file.exists(): return index_file.read_text() return f"""
<div id="content" style="width: 800px; margin: 0 auto;"> <h2>Upload new files</h2> <form method="POST" action='/upload' class="dropzone dz-clickable" id="dropper" enctype="multipart/form-data"> </form> <h2> Uploaded <input type="button" value="Clear" onclick="clearCookies()" /> </h2> <div id="uploaded"> </div> <script type="application/javascript"> function clearCookies() {{ document.cookie = "files=; Max-Age=0"; document.getElementById("uploaded").innerHTML = ""; }} function getFilesFromCookie() {{ try {{ return document.cookie.split("=", 2)[1].split("||");}} catch (error) {{ return []; }} }} function saveCookie(new_file) {{ let all_files = getFilesFromCookie(); all_files.push(new_file); document.cookie = `files=${{all_files.join("||")}}`; }} function generateLink(combo){{ const uuid = combo.split('|^^|')[0]; const name = combo.split('|^^|')[1]; if ({'true' if allow_downloads else 'false'}) {{ return `<a href="/download/${{uuid}}" download="${{name}}">${{name}}</a>`; }} return name; }} function init() {{ Dropzone.options.dropper = {{ paramName: 'file', chunking: true, forceChunking: {dropzone_force_chunking}, url: '/upload', retryChunks: true, parallelChunkUploads: {dropzone_parallel_chunks}, timeout: {dropzone_timeout}, // microseconds maxFilesize: {dropzone_max_file_size}, // megabytes chunkSize: {dropzone_chunk_size}, // bytes init: function () {{ this.on("complete", function (file) {{ let combo = `${{file.upload.uuid}}|^^|${{file.upload.filename}}`; saveCookie(combo); document.getElementById("uploaded").innerHTML += generateLink(combo) + "<br />"; }}); }} }} if (typeof document.cookie !== 'undefined' ) {{ let content = ""; getFilesFromCookie().forEach(function (combo) {{ content += generateLink(combo) + "<br />"; }}); document.getElementById("uploaded").innerHTML = content; }} }} init(); </script> </div> - Assistant
You can create a function called
clear_all_filesthat will remove all files from the storage folder. Here's an example implementation:import os def clear_all_files(): for file in os.listdir(storage_path): file_path = os.path.join(storage_path, file) if os.path.isfile(file_path): os.remove(file_path) print("All files in the storage folder have been removed.") # Call the function to clear all files in the storage folder clear_all_files()Just call the
clear_all_filesfunction whenever you want to clear all the files in the storage folder. This function will loop through all the files in the storage folder and remove them one by one.