Spaces:
Runtime error
Runtime error
Delete api.py
Browse files
api.py
DELETED
|
@@ -1,126 +0,0 @@
|
|
| 1 |
-
from flask import Flask, request, send_file
|
| 2 |
-
import os
|
| 3 |
-
import zipfile
|
| 4 |
-
import tempfile
|
| 5 |
-
import shutil
|
| 6 |
-
import rarfile
|
| 7 |
-
from werkzeug.utils import secure_filename
|
| 8 |
-
from pdf2image import convert_from_path
|
| 9 |
-
from PIL import Image
|
| 10 |
-
from moviepy.editor import VideoFileClip
|
| 11 |
-
from pydub import AudioSegment
|
| 12 |
-
|
| 13 |
-
app = Flask(__name__)
|
| 14 |
-
ALLOWED_EXTENSIONS = {'zip', 'rar', 'pdf', 'wav', 'mp4', 'jpg', 'png', 'jpeg'}
|
| 15 |
-
|
| 16 |
-
def allowed_file(filename):
|
| 17 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 18 |
-
|
| 19 |
-
def convert_single_file(input_path):
|
| 20 |
-
base_name, ext = os.path.splitext(input_path)
|
| 21 |
-
|
| 22 |
-
if ext.lower() == '.pdf':
|
| 23 |
-
# PDF تبدیل به WEBP سپس به PDF
|
| 24 |
-
images = convert_from_path(input_path)
|
| 25 |
-
output_path = f"{base_name}_converted.pdf"
|
| 26 |
-
webp_images = []
|
| 27 |
-
|
| 28 |
-
for i, image in enumerate(images):
|
| 29 |
-
webp_path = f"{base_name}_page_{i+1}.webp"
|
| 30 |
-
image.save(webp_path, 'WEBP')
|
| 31 |
-
webp_images.append(Image.open(webp_path))
|
| 32 |
-
|
| 33 |
-
webp_images[0].save(output_path, 'PDF', save_all=True, append_images=webp_images[1:])
|
| 34 |
-
return output_path
|
| 35 |
-
|
| 36 |
-
elif ext.lower() == '.mp4':
|
| 37 |
-
# MP4 تبدیل به MKV
|
| 38 |
-
output_path = f"{base_name}.mkv"
|
| 39 |
-
VideoFileClip(input_path).write_videofile(output_path, codec='libx264')
|
| 40 |
-
return output_path
|
| 41 |
-
|
| 42 |
-
elif ext.lower() == '.wav':
|
| 43 |
-
# WAV تبدیل به MP3
|
| 44 |
-
output_path = f"{base_name}.mp3"
|
| 45 |
-
AudioSegment.from_wav(input_path).export(output_path, format='mp3')
|
| 46 |
-
return output_path
|
| 47 |
-
|
| 48 |
-
elif ext.lower() in {'.png', '.jpg', '.jpeg'}:
|
| 49 |
-
# تصویر تبدیل به WEBP
|
| 50 |
-
output_path = f"{base_name}.webp"
|
| 51 |
-
Image.open(input_path).save(output_path, 'WEBP')
|
| 52 |
-
return output_path
|
| 53 |
-
|
| 54 |
-
# در صورت نداشتن تغییر، همان مسیر بازگردانده میشود.
|
| 55 |
-
return input_path
|
| 56 |
-
|
| 57 |
-
@app.route('/convert', methods=['POST'])
|
| 58 |
-
def handle_conversion():
|
| 59 |
-
if 'file' not in request.files:
|
| 60 |
-
return {"error": "No file uploaded"}, 400
|
| 61 |
-
|
| 62 |
-
file = request.files['file']
|
| 63 |
-
if not file or file.filename == '':
|
| 64 |
-
return {"error": "Empty filename"}, 400
|
| 65 |
-
|
| 66 |
-
if not allowed_file(file.filename):
|
| 67 |
-
return {"error": "Unsupported file type"}, 400
|
| 68 |
-
|
| 69 |
-
try:
|
| 70 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
| 71 |
-
filename = secure_filename(file.filename)
|
| 72 |
-
upload_path = os.path.join(temp_dir, filename)
|
| 73 |
-
file.save(upload_path)
|
| 74 |
-
|
| 75 |
-
file_ext = filename.split('.')[-1].lower()
|
| 76 |
-
|
| 77 |
-
# اگر فایل تکی باشد، عملیات تبدیل روی همان فایل اعمال شود.
|
| 78 |
-
if file_ext in {'pdf', 'wav', 'mp4', 'jpg', 'png', 'jpeg'}:
|
| 79 |
-
converted_path = convert_single_file(upload_path)
|
| 80 |
-
converted_filename = os.path.basename(converted_path)
|
| 81 |
-
return send_file(
|
| 82 |
-
converted_path,
|
| 83 |
-
as_attachment=True,
|
| 84 |
-
download_name=converted_filename
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
# در صورتی که فایل آرشیو باشد: ابتدا استخراج شود
|
| 88 |
-
if filename.endswith('.zip'):
|
| 89 |
-
with zipfile.ZipFile(upload_path, 'r') as z:
|
| 90 |
-
z.extractall(temp_dir)
|
| 91 |
-
else:
|
| 92 |
-
with rarfile.RarFile(upload_path, 'r') as r:
|
| 93 |
-
r.extractall(temp_dir)
|
| 94 |
-
|
| 95 |
-
# تبدیل تمام فایلهای موجود در آرشیو
|
| 96 |
-
for root, _, files in os.walk(temp_dir):
|
| 97 |
-
for f in files:
|
| 98 |
-
if f != filename:
|
| 99 |
-
input_file = os.path.join(root, f)
|
| 100 |
-
convert_single_file(input_file)
|
| 101 |
-
|
| 102 |
-
# ایجاد فایل آرشیو خروجی
|
| 103 |
-
output_path = os.path.join(temp_dir, "converted.zip")
|
| 104 |
-
with zipfile.ZipFile(output_path, 'w') as z:
|
| 105 |
-
for root, _, files in os.walk(temp_dir):
|
| 106 |
-
for f in files:
|
| 107 |
-
if f != filename:
|
| 108 |
-
full_path = os.path.join(root, f)
|
| 109 |
-
z.write(full_path, arcname=f)
|
| 110 |
-
|
| 111 |
-
return send_file(
|
| 112 |
-
output_path,
|
| 113 |
-
as_attachment=True,
|
| 114 |
-
download_name="converted_files.zip"
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
return {"error": f"Processing failed: {str(e)}"}, 500
|
| 119 |
-
finally:
|
| 120 |
-
try:
|
| 121 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 122 |
-
except Exception:
|
| 123 |
-
pass
|
| 124 |
-
|
| 125 |
-
if __name__ == '__main__':
|
| 126 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|