Download app.py from izuemon/any2: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/izuemon/any2/resolve/main/app.py
- Command line
-
hf download hf://spaces/izuemon/any2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/izuemon/any2/resolve/main/app.py
2.69 kB
| from flask import Flask, request, jsonify, send_from_directory | |
| from flask_cors import CORS | |
| import os | |
| import uuid | |
| import json | |
| from datetime import datetime, timedelta | |
| from werkzeug.utils import secure_filename | |
| app = Flask(__name__) | |
| CORS(app) | |
| UPLOAD_FOLDER = 'uploads' | |
| METADATA_FOLDER = 'metadata' | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| os.makedirs(METADATA_FOLDER, exist_ok=True) | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB limit | |
| def index(): | |
| return send_from_directory('.', 'uploader.html') | |
| def upload_file(): | |
| if 'file' not in request.files: | |
| return jsonify({'error': 'No file part'}), 400 | |
| file = request.files['file'] | |
| if file.filename == '': | |
| return jsonify({'error': 'No selected file'}), 400 | |
| # Generate unique ID for the file | |
| file_id = str(uuid.uuid4()) | |
| original_filename = secure_filename(file.filename) | |
| extension = os.path.splitext(original_filename)[1] | |
| stored_filename = f"{file_id}{extension}" | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], stored_filename) | |
| file.save(file_path) | |
| file_size = os.path.getsize(file_path) | |
| # Store metadata | |
| metadata = { | |
| 'id': file_id, | |
| 'original_filename': original_filename, | |
| 'stored_filename': stored_filename, | |
| 'size': file_size, | |
| 'upload_time': datetime.now().isoformat(), | |
| 'password': None, | |
| 'expiry_days': None | |
| } | |
| with open(os.path.join(METADATA_FOLDER, f"{file_id}.json"), 'w') as f: | |
| json.dump(metadata, f) | |
| return jsonify({ | |
| 'id': file_id, | |
| 'original_filename': original_filename, | |
| 'size': file_size | |
| }) | |
| def set_sharing(): | |
| data = request.json | |
| file_id = data.get('id') | |
| password = data.get('password') | |
| expiry_days = data.get('expiry_days') | |
| metadata_path = os.path.join(METADATA_FOLDER, f"{file_id}.json") | |
| if not os.path.exists(metadata_path): | |
| return jsonify({'error': 'File not found'}), 404 | |
| with open(metadata_path, 'r') as f: | |
| metadata = json.load(f) | |
| metadata['password'] = password if password else None | |
| metadata['expiry_days'] = expiry_days | |
| metadata['share_url'] = f"/share/{file_id}" | |
| with open(metadata_path, 'w') as f: | |
| json.dump(metadata, f) | |
| # Generate share URL | |
| share_url = f"/share/{file_id}" | |
| return jsonify({ | |
| 'share_url': share_url, | |
| 'id': file_id | |
| }) | |
| if __name__ == '__main__': | |
| app.run(debug=True, port=7860, host="0.0.0.0") |