-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from 1 commit
3b3353e
045ee86
ba6b28d
0b5a1f5
d172c27
6a26d37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
@@ -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!)); | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ba6b28d