Skip to content

Commit

Permalink
chore(storage): Download to temp file
Browse files Browse the repository at this point in the history
First downloads to a non-existent file, then copies the file to the location of the original in order to avoid iOS restrictions on overwriting data and potential failures which would result in deletion of the original file.

commit-id:7fbfb559
  • Loading branch information
Dillon Nys committed Sep 15, 2022
1 parent ef63e4c commit 355dc65
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,37 @@ class DownloadFileRequest {
File local;
DownloadFileOptions? options;

DownloadFileRequest({required this.key, required this.local, this.options}) {
uuid = UUID.getUUID();
factory DownloadFileRequest({
required String key,
required File local,
DownloadFileOptions? options,
}) {
return DownloadFileRequest._(
uuid: UUID.getUUID(),
key: key,
local: local,
options: options,
);
}

DownloadFileRequest._({
required this.uuid,
required this.key,
required this.local,
this.options,
});

DownloadFileRequest copyWith({
String? key,
File? local,
DownloadFileOptions? options,
}) {
return DownloadFileRequest._(
uuid: uuid,
key: key ?? this.key,
local: local ?? this.local,
options: options ?? this.options,
);
}

Map<String, Object?> serializeAsMap() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import 'dart:io';

import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

import 'amplify_storage_s3.dart';

Expand Down Expand Up @@ -169,31 +171,36 @@ class AmplifyStorageS3MethodChannel extends AmplifyStorageS3 {
Future<DownloadFileResult> downloadFile(
{required DownloadFileRequest request,
void Function(TransferProgress)? onProgress}) async {
// Download to a non-existent, empty file as required by iOS, then move
// the file into its correct position as determined by `request`.
final tempDir = await getTemporaryDirectory();
final tempFile = File(
path.join(tempDir.path, 'amplify_temp_${request.uuid}'),
);
try {
if (onProgress != null) {
_transferProgressionCallbackMap[request.uuid] = onProgress;
}
// Delete the file if it already exists since iOS will only write to
// a non-existent file; it will not overwrite the contents of an existing
// one.
if (Platform.isIOS && await request.local.exists()) {
await request.local.delete();
}
final Map<String, dynamic>? data =
(await _channel.invokeMapMethod<String, dynamic>(
'downloadFile',
request.serializeAsMap(),
request.copyWith(local: tempFile).serializeAsMap(),
));
if (data == null) {
throw AmplifyException(
AmplifyExceptionMessages.nullReturnedFromMethodChannel);
}
DownloadFileResult result = _formatDownloadFileResult(data);
return result;
if (await request.local.exists()) {
await request.local.delete();
}
return DownloadFileResult(
file: await tempFile.copy(request.local.path),
);
} on PlatformException catch (e) {
throw _convertToStorageException(e);
} finally {
_transferProgressionCallbackMap.remove(request.uuid);
tempFile.delete().ignore();
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/storage/amplify_storage_s3/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies:
sdk: flutter
meta: ^1.7.0
plugin_platform_interface: ^2.0.0
path_provider: ^2.0.0
path: any

dev_dependencies:
amplify_lints: ^1.0.0
Expand Down

This file was deleted.

Loading

0 comments on commit 355dc65

Please sign in to comment.