nitin230/File-Converter
0
1from flask import Flask, render_template, request, jsonify, send_file2import os3import tempfile4from pathlib import Path5 6from converters.pdf_converter import pdf_to_docx, pdf_to_images, pdf_to_txt7from converters.word_converter import docx_to_pdf, docx_to_txt, docx_to_images8from converters.image_converter import (9 image_to_pdf, image_to_text_docx,10 resize_image_dimensions, resize_image_filesize, convert_image_format11)12from converters.text_converter import txt_to_pdf, txt_to_docx13 14app = Flask(__name__)15app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB max16 17SUPPORTED_FORMATS = {18 "pdf": ["docx", "txt", "jpg", "png"],19 "docx": ["pdf", "txt", "jpg", "png"],20 "doc": ["pdf", "txt", "jpg", "png"],21 "txt": ["pdf", "docx"],22 "jpg": ["pdf", "docx", "png", "webp", "bmp"],23 "jpeg": ["pdf", "docx", "png", "webp", "bmp"],24 "png": ["pdf", "docx", "jpg", "webp", "bmp"],25 "webp": ["pdf", "docx", "jpg", "png", "bmp"],26 "bmp": ["pdf", "docx", "jpg", "png", "webp"],27 "tiff": ["pdf", "docx", "jpg", "png"],28 "tif": ["pdf", "docx", "jpg", "png"],29 "gif": ["jpg", "png", "pdf"],30}31 32IMAGE_EXTS = {"jpg", "jpeg", "png", "webp", "bmp", "tiff", "tif", "gif"}33 34 35@app.route("/")36def index():37 return render_template("index.html")38 39 40@app.route("/api/formats", methods=["POST"])41def get_formats():42 filename = request.json.get("filename", "")43 ext = Path(filename).suffix.lower().lstrip(".")44 formats = SUPPORTED_FORMATS.get(ext, [])45 return jsonify({"formats": formats, "ext": ext})46 47 48@app.route("/api/convert", methods=["POST"])49def convert():50 if "file" not in request.files:51 return jsonify({"error": "File nahi mila"}), 40052 53 file = request.files["file"]54 output_format = request.form.get("output_format", "").lower()55 ocr_lang = request.form.get("ocr_lang", "eng")56 57 if not file.filename:58 return jsonify({"error": "File select karo"}), 40059 if not output_format:60 return jsonify({"error": "Output format select karo"}), 40061 62 ext = Path(file.filename).suffix.lower().lstrip(".")63 out_fmt = output_format.replace(" (ocr)", "").lower()64 65 # Save uploaded file to temp66 suffix = Path(file.filename).suffix67 with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:68 file.save(tmp.name)69 src = tmp.name70 71 try:72 result_path = None73 74 if ext == "pdf":75 if out_fmt == "docx": result_path = pdf_to_docx(src)76 elif out_fmt == "txt": result_path = pdf_to_txt(src)77 elif out_fmt in ("jpg", "png"): result_path = pdf_to_images(src, out_fmt)78 79 elif ext in ("docx", "doc"):80 if out_fmt == "pdf": result_path = docx_to_pdf(src)81 elif out_fmt == "txt": result_path = docx_to_txt(src)82 elif out_fmt in ("jpg", "png"): result_path = docx_to_images(src, out_fmt)83 84 elif ext == "txt":85 if out_fmt == "pdf": result_path = txt_to_pdf(src)86 elif out_fmt == "docx": result_path = txt_to_docx(src)87 88 elif ext in IMAGE_EXTS:89 if out_fmt == "pdf": result_path = image_to_pdf(src)90 elif out_fmt == "docx": result_path = image_to_text_docx(src, ocr_lang)91 elif out_fmt in IMAGE_EXTS: result_path = convert_image_format(src, out_fmt)92 93 if result_path and os.path.exists(result_path):94 size_kb = os.path.getsize(result_path) / 102495 out_name = Path(file.filename).stem + "." + out_fmt96 return send_file(97 result_path,98 as_attachment=True,99 download_name=out_name100 )101 else:102 return jsonify({"error": "Conversion fail ho gayi"}), 500103 104 except Exception as e:105 return jsonify({"error": str(e)}), 500106 finally:107 try: os.remove(src)108 except: pass109 110 111@app.route("/api/resize", methods=["POST"])112def resize():113 if "file" not in request.files:114 return jsonify({"error": "File nahi mila"}), 400115 116 file = request.files["file"]117 mode = request.form.get("mode", "dimensions")118 width = int(request.form.get("width", 800))119 height = int(request.form.get("height", 600))120 target_kb = int(request.form.get("target_kb", 200))121 quality = int(request.form.get("quality", 85))122 123 ext = Path(file.filename).suffix124 with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:125 file.save(tmp.name)126 src = tmp.name127 128 try:129 if mode == "dimensions":130 result_path = resize_image_dimensions(src, width, height)131 msg = f"Resized to {width}x{height}px"132 else:133 result_path = resize_image_filesize(src, target_kb, quality)134 actual_kb = os.path.getsize(result_path) / 1024135 msg = f"Compressed to {actual_kb:.1f} KB"136 137 out_name = Path(file.filename).stem + "_resized" + ext138 return send_file(result_path, as_attachment=True, download_name=out_name)139 140 except Exception as e:141 return jsonify({"error": str(e)}), 500142 finally:143 try: os.remove(src)144 except: pass145 146 147if __name__ == "__main__":148 app.run(host="0.0.0.0", port=7860, debug=False)149 