save method

  1. @override
Future<LCFile> save({
  1. bool fetchWhenSave = false,
  2. LCQuery<LCObject>? query,
  3. void onProgress(
    1. int count,
    2. int total
    )?,
})
override

Saves the file to LeanCloud.

Unless the file is constructed fromUrl, this will automatically upload the file content to LeanCloud.

Implementation

@override
Future<LCFile> save(
    {bool fetchWhenSave = false,
    LCQuery<LCObject>? query,
    void Function(int count, int total)? onProgress}) async {
  if (url != null) {
    // 外链方式
    await super.save();
  } else {
    // 上传文件
    Map<String, dynamic> uploadToken = await _getUploadToken();
    String uploadUrl = uploadToken['upload_url'];
    String key = uploadToken['key'];
    String token = uploadToken['token'];
    String provider = uploadToken['provider'];
    try {
      if (provider == 's3') {
        // AWS
        String mimeType = uploadToken['mime_type'];
        await _LCAWSUploader.upload(
            uploadUrl, mimeType, file != null ? file : data, onProgress);
      } else if (provider == 'qiniu') {
        // Qiniu
        await _LCQiniuUploader.upload(
            uploadUrl, token, key, file != null ? file : data, onProgress);
      } else {
        throw ('$provider is not support.');
      }
      _LCObjectData objectData = _LCObjectData.decode(uploadToken);
      super._merge(objectData);
      LeanCloud._httpClient.post('fileCallback',
          data: {'result': true, 'token': token}).catchError((_) {});
    } catch (err) {
      LeanCloud._httpClient.post('fileCallback',
          data: {'result': false, 'token': token}).catchError((_) {});
      throw err;
    }
  }
  return this;
}