1
0
Fork 0
mirror of synced 2024-07-15 11:25:53 +12:00
appwrite/app/sdks/client-flutter/lib/services/storage.dart

191 lines
6.3 KiB
Dart
Raw Normal View History

2020-03-27 02:20:07 +13:00
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
import 'package:dio/dio.dart';
2020-03-27 02:20:07 +13:00
import 'package:meta/meta.dart';
2020-04-06 01:23:15 +12:00
import "../client.dart";
2020-03-27 02:20:07 +13:00
import '../enums.dart';
2020-04-06 01:23:15 +12:00
import "../service.dart";
2020-01-31 05:18:59 +13:00
class Storage extends Service {
2020-04-06 01:23:15 +12:00
Storage(Client client): super(client);
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
/// List Files
///
2020-01-31 05:18:59 +13:00
/// Get a list of all the user files. You can use the query params to filter
/// your results. On admin mode, this endpoint will return a list of all of the
/// project files. [Learn more about different API modes](/docs/admin).
2020-04-11 22:02:21 +12:00
///
2020-04-06 16:55:38 +12:00
Future<Response> listFiles({String search = '', int limit = 25, int offset = 0, OrderType orderType = OrderType.asc}) {
2020-04-06 01:23:15 +12:00
final String path = '/storage/files';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'search': search,
'limit': limit,
'offset': offset,
'orderType': orderType.name(),
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create File
///
2020-01-31 05:18:59 +13:00
/// Create a new file. The user who creates the file will automatically be
/// assigned to read and write access unless he has passed custom values for
/// read and write arguments.
2020-04-11 22:02:21 +12:00
///
2020-04-12 05:47:13 +12:00
Future<Response> createFile({@required MultipartFile file, @required List read, @required List write}) {
2020-04-06 01:23:15 +12:00
final String path = '/storage/files';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'file': file,
'read': read,
'write': write,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'multipart/form-data',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get File
///
2020-01-31 05:18:59 +13:00
/// Get file by its unique ID. This endpoint response returns a JSON object
/// with the file metadata.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> getFile({@required String fileId}) {
final String path = '/storage/files/{fileId}'.replaceAll(RegExp('{fileId}'), fileId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update File
///
2020-01-31 05:18:59 +13:00
/// Update file by its unique ID. Only users with write permissions have access
/// to update this resource.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updateFile({@required String fileId, @required List read, @required List write}) {
final String path = '/storage/files/{fileId}'.replaceAll(RegExp('{fileId}'), fileId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'read': read,
'write': write,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.put, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Delete File
///
2020-01-31 05:18:59 +13:00
/// Delete a file by its unique ID. Only users with write permissions have
/// access to delete this resource.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> deleteFile({@required String fileId}) {
final String path = '/storage/files/{fileId}'.replaceAll(RegExp('{fileId}'), fileId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.delete, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get File for Download
///
2020-01-31 05:18:59 +13:00
/// Get file content by its unique ID. The endpoint response return with a
/// 'Content-Disposition: attachment' header that tells the browser to start
/// downloading the file to user downloads directory.
2020-04-11 22:02:21 +12:00
///
2020-04-15 02:13:19 +12:00
String getFileDownload({@required String fileId}) {
2020-04-06 01:23:15 +12:00
final String path = '/storage/files/{fileId}/download'.replaceAll(RegExp('{fileId}'), fileId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
2020-04-15 02:13:19 +12:00
'project': client.config['project'],
2020-04-06 01:23:15 +12:00
};
2020-01-31 05:18:59 +13:00
2020-04-15 02:13:19 +12:00
Uri endpoint = Uri.parse(client.endPoint);
2020-05-24 17:30:49 +12:00
Uri location = new Uri(scheme: endpoint.scheme,
2020-04-15 02:13:19 +12:00
host: endpoint.host,
port: endpoint.port,
path: endpoint.path + path,
queryParameters:params,
);
2020-04-09 05:26:18 +12:00
2020-05-24 17:30:49 +12:00
return location.toString();
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get File Preview
///
2020-01-31 05:18:59 +13:00
/// Get a file preview image. Currently, this method supports preview for image
/// files (jpg, png, and gif), other supported formats, like pdf, docs, slides,
/// and spreadsheets, will return the file icon image. You can also pass query
/// string arguments for cutting and resizing your preview image.
2020-04-11 22:02:21 +12:00
///
2020-04-15 02:13:19 +12:00
String getFilePreview({@required String fileId, int width = 0, int height = 0, int quality = 100, String background = '', String output = ''}) {
2020-04-06 01:23:15 +12:00
final String path = '/storage/files/{fileId}/preview'.replaceAll(RegExp('{fileId}'), fileId);
final Map<String, dynamic> params = {
'width': width,
'height': height,
'quality': quality,
'background': background,
'output': output,
2020-04-15 02:13:19 +12:00
'project': client.config['project'],
2020-04-06 01:23:15 +12:00
};
2020-04-15 02:13:19 +12:00
Uri endpoint = Uri.parse(client.endPoint);
2020-05-24 17:30:49 +12:00
Uri location = new Uri(scheme: endpoint.scheme,
2020-04-15 02:13:19 +12:00
host: endpoint.host,
port: endpoint.port,
path: endpoint.path + path,
queryParameters:params,
);
2020-04-09 05:26:18 +12:00
2020-05-24 17:30:49 +12:00
return location.toString();
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get File for View
///
2020-01-31 05:18:59 +13:00
/// Get file content by its unique ID. This endpoint is similar to the download
/// method but returns with no 'Content-Disposition: attachment' header.
2020-04-11 22:02:21 +12:00
///
2020-04-15 02:13:19 +12:00
String getFileView({@required String fileId, String as = ''}) {
2020-04-06 01:23:15 +12:00
final String path = '/storage/files/{fileId}/view'.replaceAll(RegExp('{fileId}'), fileId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'as': as,
2020-04-15 02:13:19 +12:00
'project': client.config['project'],
2020-04-06 01:23:15 +12:00
};
2020-01-31 05:18:59 +13:00
2020-04-15 02:13:19 +12:00
Uri endpoint = Uri.parse(client.endPoint);
2020-05-24 17:30:49 +12:00
Uri location = new Uri(scheme: endpoint.scheme,
2020-04-15 02:13:19 +12:00
host: endpoint.host,
port: endpoint.port,
path: endpoint.path + path,
queryParameters:params,
);
2020-04-09 05:26:18 +12:00
2020-05-24 17:30:49 +12:00
return location.toString();
2020-01-31 05:18:59 +13:00
}
}