from my_modules.app.setup import LIMITER
from my_modules.functions import is_valid_uuid

from quart import Blueprint, send_from_directory, current_app, Response, redirect, abort

basic_bp = Blueprint('basic', __name__)

@basic_bp.route('/favicon', methods=['GET'])
@basic_bp.route('/favicon.ico', methods=['GET'])
@LIMITER.exempt
async def favicon():
  file_data = await current_app.convex.get_current_favicon()
  return redirect(file_data['file_id'])

@basic_bp.route('/robots.txt', methods=['GET'])
@LIMITER.limit('3 per day')
async def robots():
  return await send_from_directory(current_app.static_folder, f'robots.txt')

@basic_bp.route("/storage/<path:file_id>")
async def convex_storage_proxy(file_id:str):
  clean_file_id = file_id.split("?", 1)[0]
  if not is_valid_uuid(clean_file_id):
    return abort(404, "Not a valid uuid")

  return Response(
    current_app.convex.stream_from_storage(file_id, add_api_path=True),
    mimetype="application/octet-stream"
  )
