checkpoint

This commit is contained in:
Alessandro Autiero
2023-02-24 15:24:24 +01:00
parent 013d15d7ff
commit 63c7cc5c5b
69 changed files with 1148 additions and 644 deletions

View File

@@ -4,43 +4,57 @@ import 'package:archive/archive_io.dart';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;
import 'package:reboot_launcher/src/model/reboot_download.dart';
import 'package:reboot_launcher/src/util/os.dart';
const _rebootUrl =
const String rebootDownloadUrl =
"https://nightly.link/Milxnor/Project-Reboot/workflows/msbuild/main/Release.zip";
Future<DateTime?> _getLastUpdate(int? lastUpdateMs) async {
return lastUpdateMs != null ? DateTime.fromMillisecondsSinceEpoch(lastUpdateMs) : null;
Future<RebootDownload> downloadRebootDll(String url, int? lastUpdateMs) async {
Directory? outputDir;
File? tempZip;
try {
var now = DateTime.now();
var oldRebootDll = await loadBinary("reboot.dll", true);
var lastUpdate = await _getLastUpdate(lastUpdateMs);
var exists = await oldRebootDll.exists();
if (lastUpdate != null &&
now.difference(lastUpdate).inHours <= 24 &&
await oldRebootDll.exists()) {
return RebootDownload(lastUpdateMs!);
}
var response = await http.get(Uri.parse(rebootDownloadUrl));
var tempZip = await loadBinary("reboot.zip", true);
await tempZip.writeAsBytes(response.bodyBytes);
var outputDir = await safeBinariesDirectory.createTemp("reboot_out");
await extractFileToDisk(tempZip.path, outputDir.path);
var rebootDll = File(outputDir
.listSync()
.firstWhere((element) => path.extension(element.path) == ".dll")
.path);
if (!exists ||
sha1.convert(await oldRebootDll.readAsBytes()) !=
sha1.convert(await rebootDll.readAsBytes())) {
await oldRebootDll.writeAsBytes(await rebootDll.readAsBytes());
}
return RebootDownload(now.millisecondsSinceEpoch);
} catch (error, stackTrace) {
return RebootDownload(-1, error, stackTrace);
} finally {
try {
outputDir?.delete(recursive: true);
tempZip?.delete();
} catch (_) {}
}
}
Future<int> downloadRebootDll(int? lastUpdateMs) async {
var now = DateTime.now();
var oldRebootDll = await loadBinary("reboot.dll", true);
var lastUpdate = await _getLastUpdate(lastUpdateMs);
var exists = await oldRebootDll.exists();
if(lastUpdate != null && now.difference(lastUpdate).inHours <= 24 && await oldRebootDll.exists()){
return lastUpdateMs!;
}
var response = await http.get(Uri.parse(_rebootUrl));
var tempZip = File("${tempDirectory.path}/reboot.zip");
await tempZip.writeAsBytes(response.bodyBytes);
var outputDir = await tempDirectory.createTemp("reboot");
await extractFileToDisk(tempZip.path, outputDir.path);
var rebootDll = File(
outputDir.listSync()
.firstWhere((element) => path.extension(element.path) == ".dll")
.path
);
if (exists && sha1.convert(await oldRebootDll.readAsBytes()) == sha1.convert(await rebootDll.readAsBytes())) {
outputDir.delete(recursive: true);
return now.millisecondsSinceEpoch;
}
await oldRebootDll.writeAsBytes(await rebootDll.readAsBytes());
outputDir.delete(recursive: true);
return now.millisecondsSinceEpoch;
}
Future<DateTime?> _getLastUpdate(int? lastUpdateMs) async {
return lastUpdateMs != null
? DateTime.fromMillisecondsSinceEpoch(lastUpdateMs)
: null;
}