Skip to content

Commit

Permalink
feat(downloads)
Browse files Browse the repository at this point in the history
- adds the new downloader on the share page
  • Loading branch information
thiagocarvalhodev committed Oct 25, 2023
1 parent 687b0db commit 7a802d4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
86 changes: 53 additions & 33 deletions lib/blocs/file_download/shared_file_download_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ class SharedFileDownloadCubit extends FileDownloadCubit {
final ARFSFileEntity revision;
final ArweaveService _arweave;
final ArDriveCrypto _crypto;
final ArDriveDownloader _arDriveDownloader;

SharedFileDownloadCubit({
this.fileKey,
required this.revision,
required ArweaveService arweave,
required ArDriveCrypto crypto,
required ArDriveDownloader arDriveDownloader,
}) : _arweave = arweave,
_crypto = crypto,
_arDriveDownloader = arDriveDownloader,
super(FileDownloadStarting()) {
download();
}
Expand All @@ -28,53 +31,70 @@ class SharedFileDownloadCubit extends FileDownloadCubit {
}

Future<void> _downloadFile(ARFSFileEntity revision) async {
late Uint8List dataBytes;

emit(
FileDownloadInProgress(
fileName: revision.name,
totalByteCount: revision.size,
),
);

final dataRes = await ArDriveHTTP().getAsBytes(
'${_arweave.client.api.gatewayUrl.origin}/${revision.dataTxId}');

if (fileKey != null) {
final isPinFile = revision.pinnedDataOwnerAddress != null;
if (isPinFile) {
emit(
FileDownloadSuccess(
bytes: dataRes.data,
fileName: revision.name,
mimeType: revision.contentType ?? io.lookupMimeType(revision.name),
lastModified: revision.lastModifiedDate,
),
);
return;
}
String? cipher;
String? cipherIvTag;
final isPinFile = revision.pinnedDataOwnerAddress != null;

if (fileKey != null && !isPinFile) {
final dataTx = await (_arweave.getTransactionDetails(revision.dataTxId!));

if (dataTx != null) {
dataBytes = await _crypto.decryptDataFromTransaction(
dataTx,
dataRes.data,
fileKey!,
);
if (dataTx == null) {
throw StateError('Data transaction not found');
}
} else {
dataBytes = dataRes.data;

cipher = dataTx.getTag(EntityTag.cipher);
cipherIvTag = dataTx.getTag(EntityTag.cipherIv);
}

emit(
FileDownloadSuccess(
bytes: dataBytes,
fileName: revision.name,
mimeType: revision.contentType ?? io.lookupMimeType(revision.name),
lastModified: revision.lastModifiedDate,
),
final downloadStream = _arDriveDownloader.downloadFile(
dataTx: revision.dataTxId!,
fileName: revision.name,
fileSize: revision.size,
lastModifiedDate: revision.lastModifiedDate,
contentType:
revision.contentType ?? lookupMimeTypeWithDefaultType(revision.name),
cipher: cipher,
cipherIvString: cipherIvTag,
fileKey: fileKey,
);

logger.d(
'Downloading file ${revision.name} and dataTxId is ${revision.txId} of size ${revision.size}');

await for (var progress in downloadStream) {
if (state is FileDownloadAborted) {
return;
}

if (progress == 100) {
emit(FileDownloadFinishedWithSuccess(fileName: revision.name));
logger.d('Download finished');
return;
}

logger.d('Download progress: $progress');

emit(
FileDownloadWithProgress(
fileName: revision.name,
progress: progress.toInt(),
fileSize: revision.size,
contentType: revision.contentType ??
lookupMimeTypeWithDefaultType(revision.name),
),
);
}

logger.d('Download finished');

emit(FileDownloadFinishedWithSuccess(fileName: revision.name));
}

@override
Expand Down
2 changes: 2 additions & 0 deletions lib/components/file_download_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Future<void> promptToDownloadSharedFile({
required ARFSFileEntity revision,
}) {
final cubit = SharedFileDownloadCubit(
arDriveDownloader: ArDriveDownloader(
ardriveIo: ArDriveIO(), ioFileAdapter: IOFileAdapter()),
crypto: ArDriveCrypto(),
revision: revision,
fileKey: fileKey,
Expand Down

0 comments on commit 7a802d4

Please sign in to comment.