fix(mobile): upload file operation hang

This commit is contained in:
Alex 2025-04-30 09:16:09 -05:00
parent e3812a0e36
commit cd6d356a55
No known key found for this signature in database
GPG Key ID: 53CD082B3A5E1082

View File

@ -56,6 +56,8 @@ class BackupService {
final IFileMediaRepository _fileMediaRepository; final IFileMediaRepository _fileMediaRepository;
final IAssetRepository _assetRepository; final IAssetRepository _assetRepository;
final IAssetMediaRepository _assetMediaRepository; final IAssetMediaRepository _assetMediaRepository;
static const Duration _fileOperationTimeout = Duration(seconds: 30);
static const Duration _loadFileTimeout = Duration(minutes: 5);
BackupService( BackupService(
this._apiService, this._apiService,
@ -305,12 +307,29 @@ class BackupService {
), ),
); );
file = file = await asset.local!
await asset.local!.loadFile(progressHandler: pmProgressHandler); .loadFile(progressHandler: pmProgressHandler)
.timeout(
_loadFileTimeout,
onTimeout: () {
throw TimeoutException(
"Loading iCloud asset timed out",
_loadFileTimeout,
);
},
);
if (asset.local!.isLivePhoto) { if (asset.local!.isLivePhoto) {
livePhotoFile = await asset.local!.loadFile( livePhotoFile = await asset.local!
withSubtype: true, .loadFile(withSubtype: true, progressHandler: pmProgressHandler)
progressHandler: pmProgressHandler, .timeout(
_loadFileTimeout,
onTimeout: () {
throw TimeoutException(
"Loading iCloud asset timed out",
_loadFileTimeout,
);
},
); );
} }
} else { } else {
@ -336,12 +355,24 @@ class BackupService {
} }
} }
final fileStream = file.openRead(); final assetRawUploadData = await Future.sync(() {
final assetRawUploadData = http.MultipartFile( final fileStream = file!.openRead();
"assetData", final length = file.lengthSync();
fileStream,
file.lengthSync(), return http.MultipartFile(
filename: originalFileName, "assetData",
fileStream,
length,
filename: originalFileName,
);
}).timeout(
_fileOperationTimeout,
onTimeout: () {
throw TimeoutException(
"Reading file properties timed out",
_fileOperationTimeout,
);
},
); );
final baseRequest = MultipartRequest( final baseRequest = MultipartRequest(
@ -369,7 +400,7 @@ class BackupService {
: asset.fileCreatedAt, : asset.fileCreatedAt,
fileName: originalFileName, fileName: originalFileName,
fileType: _getAssetType(asset.type), fileType: _getAssetType(asset.type),
fileSize: file.lengthSync(), fileSize: assetRawUploadData.length,
iCloudAsset: false, iCloudAsset: false,
), ),
); );
@ -448,6 +479,15 @@ class BackupService {
debugPrint("Backup was cancelled by the user"); debugPrint("Backup was cancelled by the user");
anyErrors = true; anyErrors = true;
break; break;
} on TimeoutException catch (error, stackTrace) {
_log.severe(
"Timeout error during backup for asset ${asset.localId} | ${asset.fileName} | Created on ${asset.fileCreatedAt}",
error,
stackTrace,
);
anyErrors = true;
continue;
} catch (error, stackTrace) { } catch (error, stackTrace) {
debugPrint("Error backup asset: ${error.toString()}: $stackTrace"); debugPrint("Error backup asset: ${error.toString()}: $stackTrace");
anyErrors = true; anyErrors = true;