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

feat(deta_drive): implement method in DetaDrive #13

Open
wants to merge 1 commit into
base: deta-drive
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/deta/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version: 1.0.0
publish_to: none

environment:
sdk: ">=2.16.1 <3.0.0"
sdk: ">=2.15.0 <3.0.0"

dependencies:
deta:
Expand Down
93 changes: 82 additions & 11 deletions packages/deta/lib/src/deta_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@ abstract class DetaDrive {
const DetaDrive();

/// Upload a file.
///
/// max upload file size using this method is 10Mb only
Future<DetaDriveResponse> uploadFile(
File file,
String fileName, {
String? directory,
void Function(int progress, int total)? onSendProgress,
});

/// Upload a file larger than 10Mb.
///
/// with chunked upload
// Future<DetaDriveResponse> chunkedFileUpload(
// File file,
// String fileName, {
// String? directory,
// void Function(int progress, int total)? onSendProgress,
// });

/// Download a file.
Future<DetaDriveResponse> downloadFile(String fileName);
Future<DetaDriveResponse> downloadFile(
String fileName, {
void Function(int progress, int total)? onDownloadProgress,
});

/// Show a list of files save in the storage.
Future<DetaDriveResponse> listFiles({
Expand Down Expand Up @@ -71,7 +86,7 @@ class _DetaDrive extends DetaDrive {
try {
final resp = await client.post<Map<String, dynamic>>(
Uri.parse('${driveUrl.toString()}/files?name=$fileName'),
data: fileInBytes,
data: Stream.fromIterable(fileInBytes.toList().map((e) => [e])),
headers: <String, String>{
'Content-Type': 'application/json',
'X-API-Key': deta.projectKey,
Expand All @@ -91,25 +106,81 @@ class _DetaDrive extends DetaDrive {
}

@override
Future<DetaDriveResponse> deleteFiles(List<String> filenames) {
// TODO: implement deleteFiles
throw UnimplementedError();
Future<DetaDriveResponse> deleteFiles(List<String> filenames) async {
try {
final resp = await client.delete<Map<String, dynamic>>(
Uri.parse(
'${driveUrl.toString()}/files',
),
data: {'names': filenames},
headers: <String, String>{
'Content-Type': 'application/json',
'X-API-Key': deta.projectKey,
},
);

if (resp.statusCode == 200) {
// todo: verify proper response
// return DetaDriveResponse.fromJson(resp.body!);
}
} on DetaError catch (e) {
throw _handleError(e);
}
throw const DetaDriveException();
}

@override
Future<DetaDriveResponse> downloadFile(String fileName) {
// TODO: implement downloadFile
throw UnimplementedError();
Future<DetaDriveResponse> downloadFile(
String fileName, {
void Function(int progress, int total)? onDownloadProgress,
}) async {
try {
final resp = await client.get<List<int>>(
Uri.parse(
'${driveUrl.toString()}/files/download?name=$fileName',
),
headers: <String, String>{
'X-API-Key': deta.projectKey,
},
onReceiveProgress: onDownloadProgress,
);

if (resp.statusCode == 200) {
// todo: file byte data, verify proper response
// return resp.data;
// return DetaDriveResponse.fromJson(resp.body!);
}
} on DetaError catch (e) {
throw _handleError(e);
}
throw const DetaDriveException();
}

@override
Future<DetaDriveResponse> listFiles({
int limit = 1000,
String prefix = '',
String last = '',
}) {
// TODO: implement listFiles
throw UnimplementedError();
}) async {
try {
final resp = await client.get<Map<String, dynamic>>(
Uri.parse(
'${driveUrl.toString()}/files?limit=$limit&prefix=$prefix&last=$last',
),
headers: <String, String>{
'Content-Type': 'application/json',
'X-API-Key': deta.projectKey,
},
);

if (resp.statusCode == 200) {
// todo: verify proper response for listFiles
return DetaDriveResponse.fromJson(resp.body!);
}
} on DetaError catch (e) {
throw _handleError(e);
}
throw const DetaDriveException();
}

Exception _handleError(DetaError e) {
Expand Down