Aucune description

articles.py 1004B

1234567891011121314151617181920212223242526272829
  1. import os
  2. from flask import send_from_directory, jsonify, Blueprint, current_app
  3. from flask_cors import CORS, cross_origin
  4. articles_api_bp = Blueprint(
  5. "articles", __name__, template_folder="templates")
  6. cors = CORS(articles_api_bp, resources={r"/articles": {"origins": "http://localhost:5500"}})
  7. @articles_api_bp.route("/<path:article>")
  8. @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
  9. def send_text_file(article):
  10. return send_from_directory(directory="articles_files", path=article)
  11. @articles_api_bp.route('/')
  12. @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
  13. def index():
  14. # List files in articles directory
  15. files = list()
  16. for filename in os.listdir(os.path.join(
  17. current_app.root_path, "articles_files")):
  18. path = os.path.join(os.path.join(
  19. current_app.root_path, "articles_files"), filename)
  20. if os.path.isfile(path):
  21. files.append(filename)
  22. return jsonify(files)