Skip to content
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

fix: out of memory error when uploading large assets on slow internet #224

Merged
merged 5 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 28 additions & 42 deletions mobile/lib/modules/backup/services/backup.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:immich_mobile/utils/files_helper.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;
import 'package:http/http.dart' as http;

class BackupService {
final NetworkService _networkService = NetworkService();
Expand All @@ -35,8 +36,7 @@ class BackupService {
String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
File? file;

MultipartFile assetRawUploadData;
MultipartFile thumbnailUploadData;
http.MultipartFile? thumbnailUploadData;

for (var entity in assetList) {
try {
Expand All @@ -47,75 +47,61 @@ class BackupService {
}

if (file != null) {
FormData formData;
String originalFileName = await entity.titleAsync;
String fileNameWithoutPath = originalFileName.toString().split(".")[0];
var fileExtension = p.extension(file.path);
var mimeType = FileHelper.getMimeType(file.path);
assetRawUploadData = await MultipartFile.fromFile(
file.path,
var fileStream = file.openRead();
var assetRawUploadData = http.MultipartFile(
"assetData",
fileStream,
file.lengthSync(),
filename: fileNameWithoutPath,
contentType: MediaType(
mimeType["type"],
mimeType["subType"],
),
);
formData = FormData.fromMap({
'deviceAssetId': entity.id,
'deviceId': deviceId,
'assetType': _getAssetType(entity.type),
'createdAt': entity.createDateTime.toIso8601String(),
'modifiedAt': entity.modifiedDateTime.toIso8601String(),
'isFavorite': entity.isFavorite,
'fileExtension': fileExtension,
'duration': entity.videoDuration,
'assetData': [assetRawUploadData]
});

// Build thumbnail multipart data
var thumbnailData = await entity.thumbnailDataWithSize(const ThumbnailSize(1440, 2560));
if (thumbnailData != null) {
thumbnailUploadData = MultipartFile.fromBytes(
thumbnailUploadData = http.MultipartFile.fromBytes(
"thumbnailData",
List.from(thumbnailData),
filename: fileNameWithoutPath,
contentType: MediaType(
"image",
"jpeg",
),
);
}

var box = Hive.box(userInfoBox);

var req = http.MultipartRequest('POST', Uri.parse('$savedEndpoint/asset/upload'));
req.headers["Authorization"] = "Bearer ${box.get(accessTokenKey)}";

req.fields['deviceAssetId'] = entity.id;
req.fields['deviceId'] = deviceId;
req.fields['assetType'] = _getAssetType(entity.type);
req.fields['createdAt'] = entity.createDateTime.toIso8601String();
req.fields['modifiedAt'] = entity.modifiedDateTime.toIso8601String();
req.fields['isFavorite'] = entity.isFavorite.toString();
req.fields['fileExtension'] = fileExtension;
req.fields['duration'] = entity.videoDuration.toString();

// Send thumbnail data if it is exist
formData = FormData.fromMap({
'deviceAssetId': entity.id,
'deviceId': deviceId,
'assetType': _getAssetType(entity.type),
'createdAt': entity.createDateTime.toIso8601String(),
'modifiedAt': entity.modifiedDateTime.toIso8601String(),
'isFavorite': entity.isFavorite,
'fileExtension': fileExtension,
'duration': entity.videoDuration,
'thumbnailData': [thumbnailUploadData],
'assetData': [assetRawUploadData]
});
if(thumbnailUploadData != null) {
req.files.add(thumbnailUploadData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, it makes the code cleaner

}
req.files.add(assetRawUploadData);

Response res = await dio.post(
'$savedEndpoint/asset/upload',
data: formData,
cancelToken: cancelToken,
onSendProgress: (sent, total) => uploadProgress(sent, total),
);
var res = await req.send();

if (res.statusCode == 201) {
singleAssetDoneCb(entity.id, deviceId);
}
}
} on DioError catch (e) {
debugPrint("DioError backupAsset: ${e.response}");
if (e.type == DioErrorType.cancel || e.type == DioErrorType.other) {
return;
}
continue;
} catch (e) {
debugPrint("ERROR backupAsset: ${e.toString()}");
continue;
Expand Down
2 changes: 1 addition & 1 deletion mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ packages:
source: hosted
version: "0.15.0"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
Expand Down
1 change: 1 addition & 0 deletions mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
equatable: ^2.0.3
image_picker: ^0.8.5+3
url_launcher: ^6.1.3
http: 0.13.4

dev_dependencies:
flutter_test:
Expand Down