Skip to content

Commit eb58a97

Browse files
committed
Fixed chunk size
1 parent 3bbbd6d commit eb58a97

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

lib/api/upload.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Future<Response?> uploadChunk({
144144
String? password,
145145
CancelToken? cancelToken,
146146
}) async {
147+
SharedPreferences prefs = await SharedPreferences.getInstance();
147148
Map<String, String> queries = {
148149
'format': 'json',
149150
'method': 'pwg.images.uploadAsync',
@@ -169,7 +170,7 @@ Future<Response?> uploadChunk({
169170
return await chunkedUploader.upload(
170171
path: '/ws.php',
171172
filePath: photo.absolute.path,
172-
maxChunkSize: 100000,
173+
maxChunkSize: (prefs.getInt(Preferences.uploadChunkSizeKey) ?? 100) * 1000,
173174
params: queries,
174175
method: 'POST',
175176
data: fields,

lib/services/chunked_uploader.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:math';
55

66
import 'package:crypto/crypto.dart';
77
import 'package:dio/dio.dart';
8+
import 'package:flutter/material.dart';
89
import 'package:path/path.dart';
910

1011
class ChunkedUploader {
@@ -34,6 +35,10 @@ class ChunkedUploader {
3435
maxChunkSize: maxChunkSize,
3536
onUploadProgress: onUploadProgress)
3637
.upload();
38+
39+
static Future<String> generateMd5(Stream<List<int>> stream) async {
40+
return (await md5.bind(stream).first).toString();
41+
}
3742
}
3843

3944
class UploadRequest {
@@ -69,13 +74,13 @@ class UploadRequest {
6974
Response? finalResponse;
7075
String originalSum;
7176
List<String> chunkSums = [];
72-
originalSum = await generateMd5(_file.openRead());
73-
77+
originalSum = await ChunkedUploader.generateMd5(_file.openRead());
78+
debugPrint("$originalSum");
7479
for (int i = 0; i < _chunksCount; i++) {
7580
final start = _getChunkStart(i);
7681
final end = _getChunkEnd(i);
7782
final chunkStream = _getChunkStream(start, end);
78-
chunkSums.add(await generateMd5(chunkStream));
83+
chunkSums.add(await ChunkedUploader.generateMd5(chunkStream));
7984
}
8085

8186
for (int i = 0; i < _chunksCount; i++) {
@@ -92,18 +97,25 @@ class UploadRequest {
9297
...data
9398
});
9499

100+
print("${formData.fields}");
101+
95102
Response response = await dio.request(
96103
path,
97104
data: formData,
98105
queryParameters: params,
99106
cancelToken: cancelToken,
100-
options: Options(method: method, contentType: contentType),
107+
options: Options(
108+
method: method,
109+
contentType: contentType,
110+
headers: _getHeaders(start, end),
111+
),
101112
onSendProgress: (current, total) => _updateProgress(i, current, total),
102113
);
103114

104115
if (response.data != null && json.decode(response.data)?['result']?['id'] != null) {
105116
finalResponse = response;
106117
}
118+
debugPrint("[$i] $response");
107119
}
108120
return finalResponse;
109121
}
@@ -123,8 +135,4 @@ class UploadRequest {
123135
Map<String, dynamic> _getHeaders(int start, int end) => {'Content-Range': 'bytes $start-${end - 1}/$_fileSize'};
124136

125137
int get _chunksCount => (_fileSize / _maxChunkSize).ceil();
126-
127-
Future<String> generateMd5(Stream<List<int>> stream) async {
128-
return (await md5.bind(stream).first).toString();
129-
}
130138
}

lib/views/settings/auto_upload_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class _AutoUploadPageState extends State<AutoUploadPage> {
133133
),
134134
// todo: remove
135135
PiwigoButton(
136+
margin: const EdgeInsets.symmetric(vertical: 16.0),
136137
color: Theme.of(context).colorScheme.secondary,
137138
text: 'Debug auto upload',
138139
onPressed: () => AutoUploadManager().autoUpload(),

0 commit comments

Comments
 (0)