checkFileUploadSize static method

bool checkFileUploadSize(
  1. String path,
  2. String mediaType
)

Checks if the size of a file at the specified path is within the acceptable upload limits for the given mediaType. Returns true if the file size is within the limits, otherwise false.

Implementation

static bool checkFileUploadSize(String path, String mediaType) {
  // Retrieve file information from the provided path
  var file = File(path);

  // Get the size of the file in bytes
  int sizeInBytes = file.lengthSync();
  debugPrint("file size --> $sizeInBytes");

  // Convert the file size from bytes to megabytes
  double sizeInMb = sizeInBytes / (1024 * 1024);
  debugPrint("sizeInBytes $sizeInMb");

  // Determine the maximum acceptable file size based on the media type
  debugPrint("MediaUtils.maxImageFileSize $maxImageFileSize");
  if (mediaType == Constants.mImage && sizeInMb <= maxImageFileSize) {
    return true;
  } else if (mediaType == Constants.mAudio && sizeInMb <= maxAudioFileSize) {
    return true;
  } else if (mediaType == Constants.mVideo && sizeInMb <= maxVideoFileSize) {
    return true;
  } else if (mediaType == Constants.mDocument && sizeInMb <= maxDocFileSize) {
    return true;
  } else {
    // File size exceeds the acceptable limit for the specified media type
    return false;
  }
}