Spaces:
Paused
Paused
| import os | |
| import shutil | |
| from flask import Flask, render_template, request, send_from_directory, redirect, url_for | |
| import fitz | |
| app = Flask(__name__) | |
| # 配置 | |
| app.config['UPLOAD_FOLDER'] = '/tmp/uploads' | |
| app.config['ALLOWED_EXTENSIONS'] = {'pdf'} | |
| app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024 # 64MB 上传限制 | |
| app.config['PORT'] = 7860 | |
| # 确保上传目录存在 | |
| os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) | |
| # Utility Functions | |
| def allowed_file(filename): | |
| return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] | |
| def list_pdfs(search_query=None): | |
| """List PDFs with optional search query.""" | |
| pdfs = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if f.endswith('.pdf')] | |
| if search_query: | |
| pdfs = [f for f in pdfs if search_query.lower() in f.lower()] | |
| return pdfs | |
| def allowed_file(filename): | |
| return '.' in filename and \ | |
| filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] | |
| def index(): | |
| search = request.args.get('search', '') | |
| pdf_files = list_pdfs(search_query=search) | |
| return render_template('index.html', pdf_files=pdf_files, search=search) | |
| def upload_file(): | |
| # 检查是否有文件被上传 | |
| if 'files[]' not in request.files: | |
| return redirect(request.url) | |
| files = request.files.getlist('files[]') | |
| # 检查是否选择了文件 | |
| if not files or files[0].filename == '': | |
| return redirect(request.url) | |
| # 遍历处理每个文件 | |
| for file in files: | |
| if file and allowed_file(file.filename): | |
| filename = file.filename | |
| file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
| return redirect(url_for('index')) | |
| def view_pdf(filename): | |
| return render_template('view.html', filename=filename) | |
| def delete_pdf(filename): | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| return redirect(url_for('index')) | |
| def delete_all_pdfs(): | |
| folder = app.config['UPLOAD_FOLDER'] | |
| for filename in os.listdir(folder): | |
| file_path = os.path.join(folder, filename) | |
| try: | |
| if os.path.isfile(file_path) or os.path.islink(file_path): | |
| os.unlink(file_path) | |
| elif os.path.isdir(file_path): | |
| shutil.rmtree(file_path) | |
| except Exception as e: | |
| print(f'Failed to delete {file_path}. Reason: {e}') | |
| return redirect(url_for('index')) | |
| def get_page_count(filename): | |
| try: | |
| doc = fitz.open(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
| page_count = len(doc) | |
| doc.close() | |
| return {'page_count': page_count} | |
| except Exception as e: | |
| return {'error': str(e)}, 500 | |
| def get_page(filename, page_num): | |
| try: | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| if not os.path.exists(file_path): | |
| print(f"File not found: {file_path}") | |
| return "File not found", 404 | |
| doc = fitz.open(file_path) | |
| if page_num < 1 or page_num > len(doc): | |
| print(f"Invalid page number: {page_num}") | |
| return "Invalid page number", 400 | |
| page = doc.load_page(page_num - 1) | |
| zoom = 2.0 | |
| mat = fitz.Matrix(zoom, zoom) | |
| pix = page.get_pixmap(matrix=mat, alpha=False) | |
| img_data = pix.tobytes("png") | |
| doc.close() | |
| return img_data, 200, {'Content-Type': 'image/png'} | |
| except fitz.FileDataError as e: | |
| print(f"PDF file is corrupted or invalid: {str(e)}") | |
| return "Invalid PDF file", 400 | |
| except Exception as e: | |
| print(f"Error processing PDF: {str(e)}") | |
| return str(e), 500 | |
| def download_pdf(filename): | |
| return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True) | |
| def rename_pdf(): | |
| old_filename = request.form['old_filename'] | |
| new_filename = request.form['new_filename'] | |
| if not new_filename.endswith('.pdf'): | |
| new_filename += '.pdf' | |
| old_path = os.path.join(app.config['UPLOAD_FOLDER'], old_filename) | |
| new_path = os.path.join(app.config['UPLOAD_FOLDER'], new_filename) | |
| if os.path.exists(old_path): | |
| os.rename(old_path, new_path) | |
| return redirect(url_for('index')) | |
| def update_pdf(): | |
| if 'file' not in request.files: | |
| return redirect(url_for('index')) | |
| file = request.files['file'] | |
| filename = request.form['filename'] | |
| if file and allowed_file(file.filename): | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| file.save(file_path) | |
| return redirect(url_for('index')) | |
| if __name__ == '__main__': | |
| app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |