upload method

Future<CoffeeFile?> upload({
  1. required String filePath,
  2. required String storageName,
  3. bool scheduleResize = false,
})

Uploads a file to a specified storage.

This method uploads a file, represented by its file path, to a defined storage location. The file is sent as part of a form data payload. Optionally, the file can be scheduled for resizing after the upload, which is controlled by the scheduleResize parameter.

The method sends a POST request to the server, targeting the '/coffee/file/storage/${storageName}' endpoint, and expects a response which is then converted into a CoffeeFile object.

filePath is the local path of the file to be uploaded. storageName is the name of the storage destination where the file should be uploaded. scheduleResize (default is false) determines whether the file should be scheduled for resizing.

Returns a Future<CoffeeFile>, which is the representation of the uploaded file information as received from the server.

If the upload process encounters any issues (e.g., network errors, server errors), the method gracefully handles these exceptions and returns null.

Usage example:

var coffeeFile = await coffeeService.fileStorage().upload(
  filePath: 'path/to/file.jpg',
  storageName: 'myStorage',
  scheduleResize: true
);

Ensure the server endpoint is correctly configured to handle the file upload and the optional resize operation.

Implementation

Future<CoffeeFile?> upload({
  required String filePath,
  required String storageName,
  bool scheduleResize = false
}) async {
  Map<String, dynamic> formData = { "file": File(filePath) };

  try {
    final response = await _coffeeService.create(
      '/coffee/file/storage/$storageName?scheduleResize=$scheduleResize',
      formData,
      true,
    );

    return CoffeeFile.fromJson(response);
  } catch (e) {
    return null;
  }
}