realtime_handler.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'package:jiffy/jiffy.dart';
  2. import 'package:shelf/shelf.dart';
  3. import 'package:supabase/supabase.dart';
  4. Future<Response> handleRealtime(Request request) async {
  5. // Check authorization
  6. final authHeader = request.headers['authorization'];
  7. if (authHeader == null || !authHeader.startsWith('Bearer ')) {
  8. return Response.forbidden('Authorization header missing or invalid');
  9. }
  10. final token = authHeader.substring(7); // Remove 'Bearer ' prefix
  11. final contentType = request.headers['content-type'];
  12. if (contentType == null || !contentType.contains('multipart/form-data')) {
  13. return Response(400, body: 'Unsupported content-type');
  14. }
  15. // Initialize Supabase client with the bearer token
  16. final supabase = SupabaseClient(
  17. 'http://baas.fares.cyou:8000',
  18. token,
  19. );
  20. final channel = supabase.channel('csvhichstorage');
  21. final res = await channel.sendBroadcastMessage(
  22. event: "upload",
  23. payload: {
  24. "filename": "exportPGRGPN.zip",
  25. "updated_at": Jiffy.now().dateTime.toIso8601String(),
  26. },
  27. );
  28. print("res: $res");
  29. supabase.dispose();
  30. return Response.ok(
  31. '{"status": "success","res":$res}',
  32. headers: {'Content-Type': 'application/json'},
  33. );
  34. }