downloadMaterial method

  1. @override
Future<MaterialFileDto> downloadMaterial(
  1. MaterialRefDto material,
  2. String directory, {
  3. CancelToken? cancelToken,
  4. ProgressCallback? onReceiveProgress,
})
override

Resolves fresh access information and downloads a file into directory.

Keeps course selection locked until the transfer and partial-file cleanup finish. Preserves the required Referer and authentication cookies. Throws UnsupportedError for iStream resources. The caller owns cleanup of successfully downloaded files.

Implementation

@override
Future<MaterialFileDto> downloadMaterial(
  MaterialRefDto material,
  String directory, {
  CancelToken? cancelToken,
  ProgressCallback? onReceiveProgress,
}) async {
  if (cancelToken?.cancelError case final error?) throw error;
  if ((await getMaterial(material)).streamable) {
    throw UnsupportedError('iStream downloads are not supported');
  }
  const fileName = 'demo-material.txt';
  final file = File(p.join(directory, fileName));
  await file.writeAsString(
    'Tattoo demo course material\n${material.title ?? "Material"}\n',
  );
  final length = await file.length();
  onReceiveProgress?.call(length, length);
  return (path: file.path, fileName: fileName);
}