Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix chunked downloads through redirects

Neeraj Guptaae10c2fdfa48
diff --git a/mobile/apps/photos/lib/module/download/manager.dart b/mobile/apps/photos/lib/module/download/manager.dart
index d6e310a801..b92cf6fbd8 100644
--- a/mobile/apps/photos/lib/module/download/manager.dart
+++ b/mobile/apps/photos/lib/module/download/manager.dart
@@ -322,15 +322,13 @@ class DownloadManager {
? task.totalBytes - 1
: (startByte + downloadChunkSize) - 1;
_logger.info('Downloading chunk ${chunkIndex + 1}/$totalChunks');
+ final downloadUrl = await _resolveDownloadRedirect(task.id, cancelToken);
await _dio.download(
- FileUrl.getUrl(task.id, FileUrlType.directDownload),
+ downloadUrl,
chunkPath,
- queryParameters: {
- "token": Configuration.instance.getToken(),
- },
options: Options(
headers: {
- "Range": "bytes=$startByte-$endByte",
+ HttpHeaders.rangeHeader: "bytes=$startByte-$endByte",
},
),
cancelToken: cancelToken,
@@ -349,6 +347,36 @@ class DownloadManager {
_updateTask(task);
}
+ Future<String> _resolveDownloadRedirect(
+ int fileID,
+ CancelToken cancelToken,
+ ) async {
+ final response = await _dio.get<void>(
+ FileUrl.getUrl(fileID, FileUrlType.directDownload),
+ options: Options(
+ followRedirects: false,
+ receiveDataWhenStatusError: false,
+ headers: {
+ "X-Auth-Token": Configuration.instance.getToken(),
+ },
+ validateStatus: (status) {
+ return status != null &&
+ status >= HttpStatus.multipleChoices &&
+ status < HttpStatus.badRequest;
+ },
+ ),
+ cancelToken: cancelToken,
+ );
+ final location = response.headers.value(HttpHeaders.locationHeader);
+ if (location == null || location.isEmpty) {
+ throw StateError(
+ 'Missing redirect location for file $fileID '
+ '(status ${response.statusCode})',
+ );
+ }
+ return location;
+ }
+
Future<String> _combineChunks(String basePath, int totalChunks) async {
final finalFile = File(basePath);
final sink = finalFile.openWrite();