extractFilesData method

Future<List<(String, String, String)>> extractFilesData()

Returns a list of fileData elements, one for each file to upload. Each element is a triple containing fileField, full filePath, mimeType

The lists are stored in the similarly named String fields as a JSON list, with each list the same length. For the filenames list, if a filename refers to a file that exists (i.e. it is a full path) then that is the filePath used, otherwise the filename is appended to the Task.baseDirectory and Task.directory to form a full file path. If the filename is represented as a file:// uri then that Uri's filePath is used

Implementation

Future<List<(String, String, String)>> extractFilesData() async {
  final List<String> fileFields = List.from(jsonDecode(fileField));
  final List<String> filenames = List.from(jsonDecode(filename));
  final List<String> mimeTypes = List.from(jsonDecode(mimeType));
  final result = <(String, String, String)>[];
  for (int i = 0; i < fileFields.length; i++) {
    final fileUri = Uri.tryParse(filenames[i]);
    final filenameOrPath = (fileUri?.scheme == 'file')
        ? fileUri!.toFilePath(windows: Platform.isWindows)
        : filenames[i];
    final file = File(filenameOrPath);
    if (await file.exists()) {
      result.add((fileFields[i], filenameOrPath, mimeTypes[i]));
    } else {
      // if file does not exist, assume it is a filename only relative path) and
      // append to baseDirectory and directory
      result.add(
        (
          fileFields[i],
          await filePath(withFilename: filenameOrPath),
          mimeTypes[i],
        ),
      );
    }
  }
  return result;
}