run method
Implementation
@override
Future<TaskController> run(controller) async {
final slide = controller.slide;
var content = slide.data;
// Do not cache remot edata if cacheRemoteAssets is false
// Get any url of images that are in the markdown
// Save it the local path on the device
// and replace the url with the local path
final imageRegex = RegExp(r'!\[.*?\]\((.*?)\)');
final matches = imageRegex.allMatches(content);
Future<void> saveAsset(String url) async {
if (ProjectService.instance.isAssetFile(File(url))) return;
// Look by hashcode to see if the asset is already cached
final file = SlideAsset.cached(url);
if (await file.exists()) {
controller.markNeeded(file);
return;
}
final client = HttpClient();
final request = await client.getUrl(Uri.parse(url));
final response = await request.close();
final contentType = response.headers.contentType;
// Default to .jpg if no extension is found
var assetData = await consolidateHttpClientResponseBytes(response);
final extension = contentType?.subType ?? 'jpg';
final fileType = AssetFileType.tryParse(extension);
if (fileType == null) {
log('Invalid file type: $extension');
return;
}
final codec = await ui.instantiateImageCodec(assetData);
if (codec.frameCount > 1) {
// get half of the frame count
final frameCount = codec.frameCount ~/ 2;
for (var i = 0; i < frameCount; i++) {
await codec.getNextFrame();
}
final frame = await codec.getNextFrame();
final bytes =
await frame.image.toByteData(format: ui.ImageByteFormat.png);
assetData = bytes!.buffer.asUint8List();
}
await file.writeAsBytes(assetData);
controller.markNeeded(file);
}
for (final Match match in matches) {
final assetUri = match.group(1);
if (assetUri == null) continue;
await saveAsset(assetUri);
}
final background = slide.background;
if (background != null) {
await saveAsset(background);
}
if (slide is ImageSlide) {
final imageSource = slide.options.src;
await saveAsset(imageSource);
}
return controller;
}