registerStaticFilesCache method

dynamic registerStaticFilesCache({
  1. String path = '*',
  2. ResponseProcessor? responseProcessor,
  3. required String baseurl,
  4. Function? errorFunction,
})

Implementation

registerStaticFilesCache({String path = '*',ResponseProcessor? responseProcessor,required String baseurl,Function? errorFunction}) async {
  jaguarMux.Route route = jaguarMux.Route.get(path, (context) async {
    try {
      String directoryPath = await Simplify.rootAppDirectoryPath + "/localwebcache";
      String localPath = directoryPath + "/" + context.path;
      File cacheFile = new File(localPath);
      String downloadUrl = baseurl;
      if (!await cacheFile.exists()) {

        if (downloadUrl.lastIndexOf("/") != downloadUrl.length - 1) {
          downloadUrl = downloadUrl + "/";
        }
        downloadUrl += path;
        await WebServiceHelper.downloadFile(
            url: downloadUrl, downloadPath: localPath);
      }
      dynamic response=await loadFileCacheResource(url: downloadUrl,errorFunction: errorFunction,downloadPath: localPath);
      String? mimeType = "text/html";
      if (!context.path.endsWith('/')) {
        if (context.pathSegments.isNotEmpty) {
          final String last = context.pathSegments.last;
          if (last.contains('.')) {
            mimeType = MimeList.fromExtension[last.split('.').last];
          }
        }
      }
      context.response = ByteResponse(
          body: response, mimeType: mimeType);
    } catch (ex,stack) {
      context.response = Response.html(Simplify.getExceptionMessage(ex,stack: stack));
      if (errorFunction != null) {
        dynamic errorResponse =
            errorFunction(Simplify.getExceptionMessage(ex,stack: stack));
        if (errorResponse != null) {
          context.response = Response.html(errorResponse);
        }
      }
    }
  }, responseProcessor: responseProcessor);
  routes.add(route);
}