You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.4 KiB

  1. from flask import Flask, request, send_from_directory, abort
  2. import os
  3. from pdf_to_word import pdf_to_word
  4. import logging
  5. # Configure logging
  6. logging.basicConfig(level=logging.DEBUG, filename='/app/logs/app.log', filemode='w',
  7. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  8. app = Flask(__name__)
  9. UPLOAD_FOLDER = '/app/uploads'
  10. OUTPUT_FOLDER = '/app/outputs'
  11. if not os.path.exists(UPLOAD_FOLDER):
  12. os.makedirs(UPLOAD_FOLDER)
  13. if not os.path.exists(OUTPUT_FOLDER):
  14. os.makedirs(OUTPUT_FOLDER)
  15. @app.route('/upload-pdf', methods=['POST'])
  16. def upload_pdf():
  17. file = request.files['file']
  18. if file and file.filename.endswith('.pdf'):
  19. pdf_path = os.path.join(UPLOAD_FOLDER, file.filename)
  20. file.save(pdf_path)
  21. logging.info(f'File uploaded and saved to {pdf_path}')
  22. output_path = pdf_to_word(pdf_path, OUTPUT_FOLDER, lang='fas+eng')
  23. if output_path:
  24. logging.info(f'Sending file {output_path}')
  25. return send_from_directory(OUTPUT_FOLDER, os.path.basename(output_path), as_attachment=True)
  26. else:
  27. logging.error('Conversion failed.')
  28. abort(500, 'Conversion failed.')
  29. else:
  30. logging.warning('Invalid file type or no file uploaded.')
  31. abort(400, 'Invalid file type or no file uploaded.')
  32. if __name__ == '__main__':
  33. app.run(debug=True, host='0.0.0.0', port=5000)