Merge remote-tracking branch 'origin/main' into mob-upload-status-fixes

This commit is contained in:
Prateek Sunal
2024-09-09 20:59:38 +05:30
227 changed files with 4996 additions and 4121 deletions

View File

@@ -531,13 +531,16 @@ class FileUploader {
: null;
bool multipartEntryExists = existingMultipartEncFileName != null;
final String uniqueID = const Uuid().v4().toString();
final String uniqueID =
'${const Uuid().v4().toString()}_${file.generatedID}';
final encryptedFilePath = multipartEntryExists
? '$tempDirectory$existingMultipartEncFileName'
: '$tempDirectory$uploadTempFilePrefix${uniqueID}_file.encrypted';
final encryptedThumbnailPath =
'$tempDirectory$uploadTempFilePrefix${uniqueID}_thumb.encrypted';
late final int encFileSize;
late final int encThumbSize;
var uploadCompleted = false;
// This flag is used to decide whether to clear the iOS origin file cache
@@ -585,7 +588,7 @@ class FileUploader {
final encryptedFileExists = File(encryptedFilePath).existsSync();
// If the multipart entry exists but the encrypted file doesn't, it means
// that we'll have to reupload as the nonce is lost
// that we'll have to re-upload as the nonce is lost
if (multipartEntryExists) {
final bool updateWithDiffKey = isUpdatedFile &&
multiPartFileEncResult != null &&
@@ -623,6 +626,7 @@ class FileUploader {
} else {
thumbnailData = mediaUploadData.thumbnail;
}
encFileSize = await encryptedFile.length();
final EncryptionResult encryptedThumbnailData =
await CryptoUtil.encryptChaCha(
@@ -635,21 +639,24 @@ class FileUploader {
final encryptedThumbnailFile = File(encryptedThumbnailPath);
await encryptedThumbnailFile
.writeAsBytes(encryptedThumbnailData.encryptedData!);
encThumbSize = await encryptedThumbnailFile.length();
// Calculate the number of parts for the file.
final count = await _multiPartUploader.calculatePartCount(
await encryptedFile.length(),
);
final count = await _multiPartUploader.calculatePartCount(encFileSize);
late String fileObjectKey;
late String thumbnailObjectKey;
if (count <= 1) {
final thumbnailUploadURL = await _getUploadURL();
thumbnailObjectKey =
await _putFile(thumbnailUploadURL, encryptedThumbnailFile);
thumbnailObjectKey = await _putFile(
thumbnailUploadURL,
encryptedThumbnailFile,
encThumbSize,
);
final fileUploadURL = await _getUploadURL();
fileObjectKey = await _putFile(fileUploadURL, encryptedFile);
fileObjectKey =
await _putFile(fileUploadURL, encryptedFile, encFileSize);
} else {
isMultipartUpload = true;
_logger.finest(
@@ -672,13 +679,14 @@ class FileUploader {
collectionID,
fileUploadURLs,
encFileName,
await encryptedFile.length(),
encFileSize,
fileAttributes.key!,
fileAttributes.header!,
);
fileObjectKey = await _multiPartUploader.putMultipartFile(
fileUploadURLs,
encryptedFile,
encFileSize,
);
}
// in case of multipart, upload the thumbnail towards the end to avoid
@@ -686,8 +694,11 @@ class FileUploader {
// In regular upload, always upload the thumbnail first to keep existing behaviour
//
final thumbnailUploadURL = await _getUploadURL();
thumbnailObjectKey =
await _putFile(thumbnailUploadURL, encryptedThumbnailFile);
thumbnailObjectKey = await _putFile(
thumbnailUploadURL,
encryptedThumbnailFile,
encThumbSize,
);
}
final metadata = await file.getMetadataForUpload(mediaUploadData);
@@ -713,10 +724,10 @@ class FileUploader {
file,
fileObjectKey,
fileDecryptionHeader,
await encryptedFile.length(),
encFileSize,
thumbnailObjectKey,
thumbnailDecryptionHeader,
await encryptedThumbnailFile.length(),
encThumbSize,
encryptedMetadata,
metadataDecryptionHeader,
);
@@ -762,10 +773,10 @@ class FileUploader {
fileAttributes,
fileObjectKey,
fileDecryptionHeader,
await encryptedFile.length(),
encFileSize,
thumbnailObjectKey,
thumbnailDecryptionHeader,
await encryptedThumbnailFile.length(),
encThumbSize,
encryptedMetadata,
metadataDecryptionHeader,
pubMetadata: pubMetadataRequest,
@@ -1266,11 +1277,10 @@ class FileUploader {
Future<String> _putFile(
UploadURL uploadURL,
File file, {
int? contentLength,
File file,
int fileSize, {
int attempt = 1,
}) async {
final fileSize = contentLength ?? await file.length();
_logger.info(
"Putting object for " +
file.toString() +
@@ -1297,22 +1307,14 @@ class FileUploader {
return uploadURL.objectKey;
} on DioError catch (e) {
if (e.message.startsWith(
"HttpException: Content size exceeds specified contentLength.",
) &&
attempt == 1) {
return _putFile(
uploadURL,
file,
contentLength: (await file.readAsBytes()).length,
attempt: 2,
);
if (e.message.startsWith("HttpException: Content size")) {
rethrow;
} else if (attempt < kMaximumUploadAttempts) {
final newUploadURL = await _getUploadURL();
return _putFile(
newUploadURL,
file,
contentLength: (await file.readAsBytes()).length,
fileSize,
attempt: attempt + 1,
);
} else {