uploadFile method

Future<void> uploadFile(
  1. FileData data
)

Upload a file attachment to CAWS, i.e. one that is referenced in a FileData data object.

Implementation

Future<void> uploadFile(FileData data) async {
  if (data.path == null) {
    warning(
        '$runtimeType - No path to local FileData specified when trying to upload file - data: $data.');
    return;
  }

  info(
      "$runtimeType - File attachment upload to CAWS started - path : '${data.path}'");
  final File file = File(data.path!);

  if (!file.existsSync()) {
    warning(
        '$runtimeType - The file attachment is not found - skipping upload.');
  } else {
    final String deviceID = DeviceInfo().deviceID.toString();
    data.metadata!['device_id'] = deviceID;
    data.metadata!['study_deployment_id'] = studyDeploymentId;

    // start upload
    final FileUploadTask uploadTask =
        CarpService().getFileStorageReference().upload(file, data.metadata);

    // await the upload is successful
    CarpFileResponse response = await uploadTask.onComplete;
    int id = response.id;

    addEvent(DataManagerEvent(
      CarpDataManagerEventTypes.fileUploaded,
      file.path,
    ));
    info("$runtimeType - File upload to CAWS finished - server id : $id ");

    // delete the local file once uploaded?
    if (carpEndPoint.deleteWhenUploaded) {
      file.delete();
      addEvent(FileDataManagerEvent(
          FileDataManagerEventTypes.fileDeleted, file.path));
    }
  }
}