registerAssetsMap method

dynamic registerAssetsMap({
  1. String path = '*',
  2. Map<String, dynamic> assetsMap = const {},
  3. String type = "bytes",
  4. ResponseProcessor? responseProcessor,
  5. Function? errorFunction,
})

Implementation

registerAssetsMap({String path = '*',Map<String,dynamic> assetsMap=const {},String type="bytes",ResponseProcessor? responseProcessor,Function? errorFunction}) {
  jaguarMux.Route route = jaguarMux.Route.get(path, (context) async {
    String mimeType = 'text/html';
    try {
      String routePath = context.path;
      if (routePath.startsWith("/")) {
        routePath = routePath.substring(1);
      }
      _log("Loading Map<String,dynamic >Asset File for path " + routePath + "");
      if(assetsMap.containsKey(routePath)){
        String extension=Simplify.getFileExtensionFromPath(routePath);
        mimeType = MimeList.fromExtension[extension]!;
        _log("Found $routePath : Extension : $extension : Mime : $mimeType");
        if(type=="bytes"){
          context.response = ByteResponse(body: assetsMap[routePath], mimeType: mimeType);
        }
        else if(type=="string"){
          context.response = ByteResponse(body: utf8.encode(assetsMap[routePath]), mimeType: mimeType);
        }
      }
      else{
        context.response = Response(statusCode: HttpStatus.notFound);
      }
    } catch (ex,stack) {
      _log(ex);
      _log(stack);
      context.response = Response(statusCode: HttpStatus.notFound);
      if (errorFunction != null) {
        dynamic errorResponse =
        errorFunction(Simplify.getExceptionMessage(ex,stack: stack));
        if (errorResponse != null) {
          context.response = Response.html(errorResponse);
        }
      }
    }
  }, responseProcessor: responseProcessor);
  routes.add(route);
}