open method

  1. @override
Future<Try<ProtectedAsset, UserException>?> open(
  1. PublicationAsset asset,
  2. Fetcher fetcher,
  3. String? credentials,
  4. bool allowUserInteraction,
  5. Object sender,
)

Attempts to unlock a potentially protected file.

The Streamer will create a leaf fetcher for the low-level file access (e.g. ArchiveFetcher for a ZIP archive), to avoid having each Content Protection open the file to check if it's protected or not.

A publication might be protected in such a way that the package format can't be recognized, in which case the Content Protection will have the responsibility of creating a new leaf Fetcher.

@return A ProtectedAsset in case of success, null if the file is not protected by this technology or a UserException if the file can't be successfully opened, even in restricted mode.

Implementation

@override
Future<Try<ProtectedAsset, UserException>?> open(
    PublicationAsset asset,
    Fetcher fetcher,
    String? credentials,
    bool allowUserInteraction,
    Object sender) async {
  if (asset is! FileAsset) {
    Fimber.e(
        "Only `FileAsset` is supported with the `LcpContentProtection`. Make sure you are trying to open a package from the file system.");
    return Try.failure(OpeningException.unsupportedFormat);
  }

  FileAsset fileAsset = asset;
  if (!await _lcpService.isLcpProtected(fileAsset.file)) {
    return null;
  }

  LcpAuthenticating? authentication = (credentials != null)
      ? LcpPassphraseAuthentication(credentials,
          fallback: this._authentication)
      : this._authentication;

  Try<LcpLicense, LcpException>? license = await _lcpService.retrieveLicense(
      fileAsset.file, authentication, allowUserInteraction, sender);
  if (license == null || license.isFailure) {
    return Try.failure(license?.failure!);
  }

  ProtectedAsset protectedFile = ProtectedAsset(
      asset: asset,
      fetcher: TransformingFetcher.single(
          fetcher, LcpDecryptor(license.getOrNull()).transform),
      onCreatePublication: (servicesBuilder) => servicesBuilder
          .contentProtectionServiceFactory = serviceFactory(license));

  return Try.success(protectedFile);
}