downloadFile method

Future<DownloadFileResult> downloadFile(
  1. String channel,
  2. String fileId,
  3. String fileName, {
  4. CipherKey? cipherKey,
  5. Keyset? keyset,
  6. String? using,
})

This method allows to download the file with fileId and fileName from channel channel It returns file content in bytes format List<int>.

Provided cipherKey overrides Keyset.cipherKey.

If keyset is not provided, then it tries to obtain a keyset using name. If that fails, then it uses the default keyset. If that fails as well, then it will throw InvariantException.

Implementation

Future<DownloadFileResult> downloadFile(
    String channel, String fileId, String fileName,
    {CipherKey? cipherKey, Keyset? keyset, String? using}) async {
  _logger.info('Download file API call');
  // Validate input parameters to prevent path traversal attacks
  FileValidation.validateChannelName(channel);
  FileValidation.validateFileId(fileId);
  FileValidation.validateFileName(fileName);

  keyset ??= _core.keysets[using];

  var params = DownloadFileParams(
      getFileUrl(channel, fileId, fileName).replace(scheme: '', host: ''));

  _logger.fine(LogEvent(
      message: 'Download file API call with parameters:',
      details: {
        'channel': channel,
        'fileId': fileId,
        'fileName': fileName,
        'cipherKey': cipherKey != null ? 'provided' : 'not provided',
      },
      detailsType: LogEventDetailsType.apiParametersInfo));

  return defaultFlow<DownloadFileParams, DownloadFileResult>(
      keyset: keyset,
      core: _core,
      params: params,
      deserialize: false,
      serialize: (object, [_]) => DownloadFileResult.fromJson(object,
          cipherKey: cipherKey ?? keyset!.cipherKey,
          decryptFunction: cipherKey != null ||
                  !(keyset?.cipherKey ==
                      _core.keysets.defaultKeyset.cipherKey) ||
                  !(_core.crypto is CryptoModule)
              ? _core.crypto.decryptFileData
              : _core.crypto.decrypt));
}