Fixed CORS

This commit is contained in:
2023-09-22 14:33:33 +05:30
parent dca7cf76e6
commit 9daa3db037
4 changed files with 86 additions and 83 deletions

View File

@@ -16,7 +16,7 @@ const Center noMenuWidget = Center(
style: TextStyle(fontSize: 20, fontStyle: FontStyle.italic)));
const String hashLink =
"http://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.txt";
"https://raw.githubusercontent.com/kphanipavan/IIITB_Menu/menu_scraper/out.txt";
const String dataLink =
"http://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.json";
"https://raw.githubusercontent.com/kphanipavan/IIITB_Menu/menu_scraper/out.json";

View File

@@ -1,15 +1,14 @@
// ignore_for_file: unnecessary_this, file_names
// ignore_for_file: unnecessary_this, file_names, library_prefixes
import "dart:convert";
// import "dart:io";
import "package:iiitb_menu/constants.dart";
// import "package:path_provider/path_provider.dart";
// import 'dart:io';
import 'dart:io';
import "package:flutter/material.dart";
// import "package:crypto/crypto.dart";
// import "package:http/http.dart";
import "package:crypto/crypto.dart";
import "package:http/http.dart";
import "package:intl/intl.dart";
// import "package:shared_preferences/shared_preferences.dart";
import "package:shared_preferences/shared_preferences.dart";
// import "package:dio/dio.dart";
import "package:iiitb_menu/data.dart" as menuData;
@@ -31,9 +30,9 @@ class GlobalModel extends ChangeNotifier {
});
}
// static Future<String> getLatestHash() async {
// try {
// // Request hashRequest = Request("get", Uri.parse(hashLink));
static Future<String> getLatestHash() async {
try {
Response hashRequest = await get(Uri.parse(hashLink));
// Response ret = await Dio().request(hashLink,
// options: Options(method: 'GET', headers: {
// HttpHeaders.acceptHeader: "text/plain",
@@ -42,76 +41,78 @@ class GlobalModel extends ChangeNotifier {
// "Access-Control-Allow-Headers": "*",
// "Access-Control-Max-Age": "1000"
// }));
// if (ret.statusCode == 200) {
// return ret.data.replaceAll("\n", "");
// } else {
// return "";
// }
// } on SocketException catch (exce) {
// print("Unable to download hash");
// print(exce);
// return "";
// }
// }
if (hashRequest.statusCode == 200) {
return hashRequest.body.replaceAll("\n", "");
} else {
return "";
}
} on SocketException catch (exce) {
print("Unable to download hash");
print(exce);
return "";
}
}
// static Future<String> getLatestData() async {
// try {
static Future<String> getLatestData() async {
try {
// Response ret = await Dio().request(dataLink,
// options: Options(method: "GET", headers: {
// HttpHeaders.acceptHeader: "text/plain",
// "Access-Control-Allow-Origin": "*"
// }));
// return ret.data;
// } on SocketException catch (exce) {
// print("Unable to download any data");
// print(exce);
// return "";
// }
// }
Response dataRequest = await get(Uri.parse(dataLink));
return dataRequest.body;
} on SocketException catch (exce) {
print("Unable to download any data");
print(exce);
return "";
}
}
static Future<Map> loadData() async {
late Map returnData;
// Directory appDir = await getApplicationDocumentsDirectory();
// final SharedPreferences prefs = await SharedPreferences.getInstance();
// String? rawData;
// String rawHash;
final SharedPreferences prefs = await SharedPreferences.getInstance();
String? rawData;
String rawHash;
// String fileName = "${appDir.path}/menu.json";
// print(fileName);
// File fileLink = File(fileName);
// rawData = prefs.getString("asdasdasd");
rawData = prefs.getString(storageKey);
print(rawData);
if (rawData != null) {
print("data found");
// rawData = localStor.getItem("fullMenu");
rawHash = md5.convert(utf8.encode(rawData)).toString();
print("Hash of Raw Data:");
print(rawHash);
// print("RawData: ");
// print(rawData);
// if (rawData != null) {
// print("data found");
// // rawData = localStor.getItem("fullMenu");
// rawHash = md5.convert(utf8.encode(rawData)).toString();
// print("Hash of Raw Data:");
// print(rawHash);
// // print("RawData: ");
// // print(rawData);
// returnData = jsonDecode(rawData);
// // menuAvailable = true;
// } else {
// print("Data Not Found");
// returnData = {};
// rawHash = "";
// }
// // String remoteHash = await GlobalModel.getLatestHash();
returnData = jsonDecode(rawData);
// menuAvailable = true;
} else {
print("Data Not Found");
returnData = {};
rawHash = "";
}
String remoteHash = await GlobalModel.getLatestHash();
// String remoteHash = "asd";
// print("Remote Hash: $remoteHash");
// if (remoteHash == "") {
// } else if (remoteHash == rawHash) {
// print("Remote hash $remoteHash is same as local hash $rawHash");
// } else {
// print("Remote hash $remoteHash is NOT the same as local hash $rawHash");
// print("Getting data from remote");
// rawData = await getLatestData();
// if (rawData == "") {
// return {};
// }
// returnData = jsonDecode(rawData);
// // fileLink.writeAsStringSync(rawData, mode: FileMode.write);
// prefs.setString(storageKey, rawData);
// }
print("Remote Hash: $remoteHash");
if (remoteHash == "") {
} else if (remoteHash == rawHash) {
print("Remote hash $remoteHash is same as local hash $rawHash");
} else {
print("Remote hash $remoteHash is NOT the same as local hash $rawHash");
print("Getting data from remote");
rawData = await getLatestData();
if (rawData == "") {
return {};
}
// print(rawData);
returnData = jsonDecode(rawData);
// fileLink.writeAsStringSync(rawData, mode: FileMode.write);
prefs.setString(storageKey, rawData);
}
returnData = jsonDecode(menuData.data);
return returnData;
}

View File

@@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}

View File

@@ -36,13 +36,13 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.5
# crypto: ^3.0.3
crypto: ^3.0.3
intl: ^0.18.1
# http: ^1.1.0
http: ^1.1.0
# dio: ^5.3.2
path_provider:
# localstorage: ^4.0.1+4
# shared_preferences: ^2.2.0
shared_preferences: ^2.2.0
# sembast:
dev_dependencies: