Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE-4885: use new downloader on share file page #1443

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/blocs/file_download/file_download_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:ardrive/entities/constants.dart';
import 'package:ardrive/models/models.dart';
import 'package:ardrive/services/services.dart';
import 'package:ardrive/utils/logger/logger.dart';
import 'package:ardrive_http/ardrive_http.dart';
import 'package:ardrive_io/ardrive_io.dart' as io;
import 'package:ardrive_io/ardrive_io.dart';
import 'package:ardrive_utils/ardrive_utils.dart';
Expand Down
89 changes: 54 additions & 35 deletions lib/blocs/file_download/shared_file_download_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ class SharedFileDownloadCubit extends FileDownloadCubit {
final SecretKey? fileKey;
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 +29,71 @@ 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!));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weren't checking if the dataTxId was null before... Should we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the parentheses are unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: cipherTag may be a more appropriate name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using this expression many times, let's perhaps use a final var for it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cipher: cipher,
cipherIvString: cipherIvTag,
fileKey: fileKey,
isManifest: revision.contentType == ContentType.manifest,
);

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's perhaps too spammy to have this log here on every progress update?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. 045ee86


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
5 changes: 5 additions & 0 deletions lib/components/file_download_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ Future<void> promptToDownloadSharedFile({
required ARFSFileEntity revision,
}) {
final cubit = SharedFileDownloadCubit(
arDriveDownloader: ArDriveDownloader(
ardriveIo: ArDriveIO(),
ioFileAdapter: IOFileAdapter(),
arweave: context.read<ArweaveService>(),
),
crypto: ArDriveCrypto(),
revision: revision,
fileKey: fileKey,
Expand Down