import 'dart:io';

import 'package:http/http.dart' as http;

import '/utils/encryption/aes_helper.dart';
import '/utils/storage/session_cookie.dart';
import '/utils/storage/write_file.dart';

Future<File> getFile(String link, String imageName, dynamic symmetricKey) async {
  var resp = await http.get(
    Uri.parse(link),
    headers: {
      'cookie': await getSessionCookie(),
    }
  );

  if (resp.statusCode != 200) {
    throw Exception('Could not get attachment file');
  }

  var data = AesHelper.aesDecryptBytes(
    symmetricKey,
    resp.bodyBytes,
  );

  File file = await writeImage(
    imageName,
    data,
  );

  return file;
}
