@@ -10,6 +10,18 @@ import 'package:nextcloud/src/api/webdav/webdav.dart';
10
10
import 'package:nextcloud/utils.dart' ;
11
11
import 'package:universal_io/io.dart' show File, FileStat;
12
12
13
+ /// The algorithms supported for the oc:checksum prop and OC-Checksum header.
14
+ const supportedChecksumAlgorithms = ['md5' , 'sha1' , 'sha256' , 'sha3-256' , 'adler32' ];
15
+
16
+ /// The pattern of supported checksum algorithms.
17
+ ///
18
+ /// It has to be `<algorithm>:<hash>` with `algorithm` being one of [supportedChecksumAlgorithms] .
19
+ /// The checksum is case-insensitive.
20
+ final checksumPattern = RegExp (
21
+ '^(${supportedChecksumAlgorithms .join ('|' )}):.+\$ ' ,
22
+ caseSensitive: false ,
23
+ );
24
+
13
25
/// WebDavClient class
14
26
class WebDavClient {
15
27
// ignore: public_member_api_docs
@@ -92,13 +104,16 @@ class WebDavClient {
92
104
93
105
/// Request to put a new file at [path] with [localData] as content.
94
106
///
107
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
108
+ ///
95
109
/// See:
96
110
/// * [put] for a complete operation executing this request.
97
111
http.Request put_Request (
98
112
Uint8List localData,
99
113
PathUri path, {
100
114
DateTime ? lastModified,
101
115
DateTime ? created,
116
+ String ? checksum,
102
117
}) {
103
118
final request = http.Request ('PUT' , _constructUri (path))..bodyBytes = localData;
104
119
@@ -107,6 +122,7 @@ class WebDavClient {
107
122
lastModified: lastModified,
108
123
created: created,
109
124
contentLength: localData.length,
125
+ checksum: checksum,
110
126
);
111
127
_addBaseHeaders (request);
112
128
return request;
@@ -116,6 +132,8 @@ class WebDavClient {
116
132
///
117
133
/// [lastModified] sets the date when the file was last modified on the server.
118
134
/// [created] sets the date when the file was created on the server.
135
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
136
+ ///
119
137
/// See:
120
138
/// * http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information.
121
139
/// * [put_Request] for the request sent by this method.
@@ -124,19 +142,23 @@ class WebDavClient {
124
142
PathUri path, {
125
143
DateTime ? lastModified,
126
144
DateTime ? created,
145
+ String ? checksum,
127
146
}) {
128
147
final request = put_Request (
129
148
localData,
130
149
path,
131
150
lastModified: lastModified,
132
151
created: created,
152
+ checksum: checksum,
133
153
);
134
154
135
155
return csrfClient.send (request);
136
156
}
137
157
138
158
/// Request to put a new file at [path] with [localData] as content.
139
159
///
160
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
161
+ ///
140
162
/// See:
141
163
/// * [putStream] for a complete operation executing this request.
142
164
http.StreamedRequest putStream_Request (
@@ -145,6 +167,7 @@ class WebDavClient {
145
167
required int contentLength,
146
168
DateTime ? lastModified,
147
169
DateTime ? created,
170
+ String ? checksum,
148
171
void Function (double progress)? onProgress,
149
172
}) {
150
173
final request = http.StreamedRequest ('PUT' , _constructUri (path));
@@ -155,6 +178,7 @@ class WebDavClient {
155
178
lastModified: lastModified,
156
179
created: created,
157
180
contentLength: contentLength,
181
+ checksum: checksum,
158
182
);
159
183
160
184
if (onProgress != null ) {
@@ -181,6 +205,7 @@ class WebDavClient {
181
205
/// [lastModified] sets the date when the file was last modified on the server.
182
206
/// [created] sets the date when the file was created on the server.
183
207
/// [contentLength] sets the length of the [localData] that is uploaded.
208
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
184
209
/// [onProgress] can be used to watch the upload progress. Possible values range from 0.0 to 1.0. [contentLength] needs to be set for it to work.
185
210
///
186
211
/// See:
@@ -192,13 +217,15 @@ class WebDavClient {
192
217
required int contentLength,
193
218
DateTime ? lastModified,
194
219
DateTime ? created,
220
+ String ? checksum,
195
221
void Function (double progress)? onProgress,
196
222
}) {
197
223
final request = putStream_Request (
198
224
localData,
199
225
path,
200
226
lastModified: lastModified,
201
227
created: created,
228
+ checksum: checksum,
202
229
contentLength: contentLength,
203
230
onProgress: onProgress,
204
231
);
@@ -208,6 +235,8 @@ class WebDavClient {
208
235
209
236
/// Request to put a new file at [path] with [file] as content.
210
237
///
238
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
239
+ ///
211
240
/// See:
212
241
/// * [putFile] for a complete operation executing this request.
213
242
http.StreamedRequest putFile_Request (
@@ -216,6 +245,7 @@ class WebDavClient {
216
245
PathUri path, {
217
246
DateTime ? lastModified,
218
247
DateTime ? created,
248
+ String ? checksum,
219
249
void Function (double progress)? onProgress,
220
250
}) {
221
251
// Authentication and content-type headers are already set by the putStream_Request.
@@ -225,6 +255,7 @@ class WebDavClient {
225
255
path,
226
256
lastModified: lastModified,
227
257
created: created,
258
+ checksum: checksum,
228
259
contentLength: fileStat.size,
229
260
onProgress: onProgress,
230
261
);
@@ -234,6 +265,7 @@ class WebDavClient {
234
265
///
235
266
/// [lastModified] sets the date when the file was last modified on the server.
236
267
/// [created] sets the date when the file was created on the server.
268
+ /// [checksum] has to follow [checksumPattern] . It will not be validated by the server.
237
269
/// [onProgress] can be used to watch the upload progress. Possible values range from 0.0 to 1.0.
238
270
/// See:
239
271
/// * http://www.webdav.org/specs/rfc2518.html#METHOD_PUT for more information.
@@ -244,6 +276,7 @@ class WebDavClient {
244
276
PathUri path, {
245
277
DateTime ? lastModified,
246
278
DateTime ? created,
279
+ String ? checksum,
247
280
void Function (double progress)? onProgress,
248
281
}) {
249
282
final request = putFile_Request (
@@ -252,6 +285,7 @@ class WebDavClient {
252
285
path,
253
286
lastModified: lastModified,
254
287
created: created,
288
+ checksum: checksum,
255
289
onProgress: onProgress,
256
290
);
257
291
@@ -571,13 +605,20 @@ class WebDavClient {
571
605
required int contentLength,
572
606
DateTime ? lastModified,
573
607
DateTime ? created,
608
+ String ? checksum,
574
609
}) {
575
610
if (lastModified != null ) {
576
611
request.headers['X-OC-Mtime' ] = lastModified.secondsSinceEpoch.toString ();
577
612
}
578
613
if (created != null ) {
579
614
request.headers['X-OC-CTime' ] = created.secondsSinceEpoch.toString ();
580
615
}
616
+ if (checksum != null ) {
617
+ if (! checksumPattern.hasMatch (checksum)) {
618
+ throw ArgumentError .value (checksum, 'checksum' , 'Invalid checksum' );
619
+ }
620
+ request.headers['OC-Checksum' ] = checksum;
621
+ }
581
622
request.headers['content-length' ] = contentLength.toString ();
582
623
}
583
624
0 commit comments