downloadFile static method

Future<bool> downloadFile({
  1. required String url,
  2. String fileName = "",
  3. bool addTimestamp = true,
})

Implementation

static Future<bool> downloadFile({required String url, String fileName = "", bool addTimestamp = true}) async {
  try {
    if (url.isEmpty) {
      throw EticonDownloadError(error: 'Url is empty');
    }
    if (await _requestPermission(Permission.storage)) {
      http.Response response = await http.get(Uri.parse(url));
      if (response.statusCode.toString().startsWith('30')) {
        throw DownloadException(response.statusCode);
      }
      if (response.statusCode >= 400) {
        throw DownloadException(response.statusCode);
      }
      if (response.statusCode.toString().startsWith('20')) {
        if (fileName.isEmpty) {
          fileName = basename(url);
        }
        return await saveToDownloadDir(bytes: response.bodyBytes, fileName: fileName, addTimestamp: addTimestamp);
      }
    } else {
      return false;
    }
  } catch (error) {
    throw EticonDownloadError(error: 'Problem download file, error: $error');
  }
  return true;
}