downloadMedia method

Future<void> downloadMedia(
  1. List<Attachment> attachments, {
  2. String? to,
})

Downloads the provided image or video attachments.

Implementation

Future<void> downloadMedia(List<Attachment> attachments, {String? to}) async {
  try {
    for (Attachment attachment in attachments) {
      if (attachment is! LocalAttachment) {
        await CacheWorker.instance
            .download(
              attachment.original.url,
              attachment.filename,
              attachment.original.size,
              checksum: attachment.original.checksum,
              to:
                  attachments.length > 1 && to != null
                      ? '$to/${attachment.filename}'
                      : to,
            )
            .future;
      } else {
        // TODO: Implement [LocalAttachment] download.
        throw UnimplementedError();
      }
    }

    MessagePopup.success(
      attachments.length > 1
          ? 'label_files_downloaded'.l10n
          : attachments.first is ImageAttachment
          ? 'label_image_downloaded'.l10n
          : 'label_video_downloaded'.l10n,
    );
  } catch (e) {
    MessagePopup.error('err_could_not_download'.l10n);
    rethrow;
  }
}