-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextcloud): Implement chunked upload
Signed-off-by: provokateurin <[email protected]>
- Loading branch information
1 parent
6b76021
commit 8e503af
Showing
5 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
packages/nextcloud/lib/src/api/webdav/chunked_upload_client.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import 'dart:math'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:nextcloud/nextcloud.dart'; | ||
import 'package:nextcloud/src/api/webdav/webdav.dart'; | ||
import 'package:nextcloud/src/utils/utils.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
|
||
const _uuid = Uuid(); | ||
|
||
typedef ChunkedUpload = ({String token, PathUri destination}); | ||
|
||
class ChunkedUploadClient { | ||
ChunkedUploadClient( | ||
NextcloudClient rootClient, | ||
String username, | ||
) : _username = username, | ||
_webDavClient = WebDavClient( | ||
rootClient, | ||
endpoint: 'remote.php/dav/uploads/$username', | ||
); | ||
|
||
final WebDavClient _webDavClient; | ||
final String _username; | ||
|
||
Future<ChunkedUpload> start(PathUri path) async { | ||
final destination = PathUri.parse('/remote.php/dav/files/$_username/').join(path); | ||
final token = _uuid.v7(); | ||
|
||
final streamedResponse = await _webDavClient.mkcol( | ||
PathUri.parse(token), | ||
headers: { | ||
'Destination': destination.toString(), | ||
}, | ||
); | ||
if (streamedResponse.statusCode != 201) { | ||
final response = await http.Response.fromStream(streamedResponse); | ||
|
||
throw DynamiteStatusCodeException(response); | ||
} | ||
|
||
return ( | ||
token: token, | ||
destination: destination, | ||
); | ||
} | ||
|
||
Future<void> uploadChunk( | ||
ChunkedUpload chunkedUpload, | ||
int index, | ||
Uint8List chunk, | ||
int totalLength, | ||
) async { | ||
assert( | ||
index >= 1 && index <= 10000, | ||
'Index must be a number between 1 and 10000', | ||
); | ||
assert( | ||
chunk.lengthInBytes >= 5 * pow(1024, 2) && chunk.lengthInBytes <= 5 * pow(1024, 3), | ||
'Chunk size must be between 5MB and 5GB', | ||
); | ||
|
||
final streamedResponse = await _webDavClient.put( | ||
chunk, | ||
PathUri.parse('${chunkedUpload.token}/$index'), | ||
headers: { | ||
'Destination': chunkedUpload.destination.toString(), | ||
'OC-Total-Length': totalLength.toString(), | ||
}, | ||
); | ||
if (streamedResponse.statusCode != 201) { | ||
final response = await http.Response.fromStream(streamedResponse); | ||
|
||
throw DynamiteStatusCodeException(response); | ||
} | ||
} | ||
|
||
Future<void> assembleChunks( | ||
ChunkedUpload chunkedUpload, | ||
int totalLength, { | ||
DateTime? lastModified, | ||
DateTime? created, | ||
}) async { | ||
final request = http.Request( | ||
'MOVE', | ||
constructUri( | ||
_webDavClient.rootClient.baseURL, | ||
'remote.php/dav/uploads/$_username', | ||
PathUri.parse('${chunkedUpload.token}/.file'), | ||
), | ||
); | ||
request.headers.addAll({ | ||
'Destination': chunkedUpload.destination.toString(), | ||
'OC-Total-Length': totalLength.toString(), | ||
if (lastModified != null) 'X-OC-Mtime': lastModified.secondsSinceEpoch.toString(), | ||
if (created != null) 'X-OC-CTime': created.secondsSinceEpoch.toString(), | ||
}); | ||
|
||
final streamedResponse = await _webDavClient.csrfClient.send(request); | ||
if (streamedResponse.statusCode != 201) { | ||
final response = await http.Response.fromStream(streamedResponse); | ||
|
||
throw DynamiteStatusCodeException(response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/nextcloud/test/fixtures/webdav/chunked_upload.regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
MKCOL http://localhost/remote\.php/dav/uploads/user1/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} | ||
authorization: Bearer mock | ||
content-type: application/xml | ||
destination: //:80/remote\.php/dav/files/user1/test/test\.bin | ||
ocs-apirequest: true | ||
requesttoken: token | ||
PUT http://localhost/remote\.php/dav/uploads/user1/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/1 | ||
authorization: Bearer mock | ||
content-length: 10485760 | ||
content-type: application/xml | ||
destination: //:80/remote\.php/dav/files/user1/test/test\.bin | ||
oc-total-length: 15728640 | ||
ocs-apirequest: true | ||
requesttoken: token | ||
.+ | ||
PUT http://localhost/remote\.php/dav/uploads/user1/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/2 | ||
authorization: Bearer mock | ||
content-length: 5242880 | ||
content-type: application/xml | ||
destination: //:80/remote\.php/dav/files/user1/test/test\.bin | ||
oc-total-length: 15728640 | ||
ocs-apirequest: true | ||
requesttoken: token | ||
.+ | ||
MOVE http://localhost/remote\.php/dav/uploads/user1/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/\.file | ||
destination: //:80/remote\.php/dav/files/user1/test/test\.bin | ||
oc-total-length: 15728640 | ||
ocs-apirequest: true | ||
requesttoken: token | ||
x-oc-ctime: 0 | ||
x-oc-mtime: 31536000 | ||
PROPFIND http://localhost/remote\.php/webdav/test/test\.bin | ||
authorization: Bearer mock | ||
content-type: application/xml | ||
ocs-apirequest: true | ||
requesttoken: token | ||
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud\.org/ns" xmlns:nc="http://nextcloud\.org/ns" xmlns:ocs="http://open-collaboration-services\.org/ns" xmlns:ocm="http://open-cloud-mesh\.org/ns"><d:prop><d:getcontentlength/><d:getlastmodified/><nc:creation_time/></d:prop></d:propfind> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters