Fakultas Ilmu Komputer UI

cookies_interface.dart 2.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
import 'dart:convert';
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:ppl_disabilitas/config/strings.dart';

class CookiesInterface {
  Future<bool> checkCookieFileAvailability({String fileName}) async {
    Directory dir;
    await getApplicationDocumentsDirectory().then((Directory directory) {
      dir = directory;
    });
13
    File cookieFile = File("${dir.path}/$fileName.json");
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    bool cookiesExist = cookieFile.existsSync();

    return cookiesExist;
  }

  Future<File> createSignInCookie({
      Map<String, dynamic> responseHeaders}) async {
    try {
      String setCookie;
      String csrfToken;
      String sessionId;
      String userKey;
      List<String> cookiesList;
      Directory dir;

      await getApplicationDocumentsDirectory().then((Directory directory) {
        dir = directory;
      });
32
      File cookieFile = File("${dir.path}/usercookies.json");
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      cookieFile.createSync();

      setCookie = responseHeaders["set-cookie"];
      if (setCookie != null) {
        csrfToken = setCookie.split(";")[0].split("=")[1];
        sessionId = setCookie.split(";")[4].split(",")[1].split("=")[1];
        userKey = key;
      }
      cookiesList = <String>[
        csrfToken,
        sessionId,
        userKey,
      ];
      cookieFile.writeAsStringSync(json.encode(cookiesList));
      return cookieFile;
    } on Exception catch (e) {
      print(e.toString());
50
      rethrow;
51
52
53
54
55
    }
  }

  Future<File> createSearchHistoryCookie({
    Map<String, dynamic> recentSearch}) async {
56
    print("recent searrch $recentSearch");
57
    Directory dir;
58
    List currentSearchHistory;
59
60
61
62
    try {
      await getApplicationDocumentsDirectory().then((Directory directory) {
        dir = directory;
      });
63
      File cookieFile = File(dir.path + "/searchhistory.json");
64
      cookieFile.createSync();
65
66
67
68
69
70
71
72
73
74
      await checkCookieFileAvailability(fileName: "searchhistory").then((available) async {
        if (available) {
          await getCookieFile(fileName: "searchhistory").then((cookie) {
            bool test = cookie == null;
            print("cookie is null? $test");
            if (cookie == null) {
              currentSearchHistory = [];
            } else {
              currentSearchHistory = json.decode(cookie);
            }
75
            currentSearchHistory.insert(0, recentSearch);
76
77
78
79
          });
        } else {
          currentSearchHistory = [];
        }
80
        await cookieFile.writeAsString(json.encode(currentSearchHistory));
81
      });
82
83
84
      return cookieFile;
    } on Exception catch (e) {
      print(e.toString());
85
      rethrow;
86
87
88
    }
  }

89
  Future<dynamic> getCookieFile({String fileName}) async {
90
91
92
93
    Directory dir;
    await getApplicationDocumentsDirectory().then((Directory directory) {
      dir = directory;
    });
94
    File file = File("${dir.path}/$fileName.json");
95
    dynamic res = file.readAsStringSync();
96
    return res;
97
98
  }
}