Skip to content

Commit

Permalink
fix: restore old update method for windows version
Browse files Browse the repository at this point in the history
  • Loading branch information
nini22P committed Jan 21, 2025
1 parent 3086aa7 commit d250767
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 118 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v1.1.1

### Changelog
* Restore old update method for windows version (Double-click the `iris-updater.bat` in the same directory as the executable file to upgrade if you have problems updating.)

### 更新日志
* windows 版本恢复为旧的更新方式(更新出问题的可双击打开可执行文件同级目录下的 `iris-updater.bat` 升级)


## v1.1.0

### Breaking Changes
Expand Down
132 changes: 15 additions & 117 deletions lib/pages/dialog/show_release_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:iris/utils/file_size_convert.dart';
import 'package:iris/utils/is_desktop.dart';
import 'package:iris/utils/path.dart';
import 'package:path/path.dart' as p;
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
Expand All @@ -29,102 +27,25 @@ class ReleaseDialog extends HookWidget {
@override
Widget build(BuildContext context) {
final t = getLocalizations(context);
final progress = useState(0.0);
final isError = useState(false);
final isDownload = useState(false);
final downloadingTask = useState<StreamSubscription?>(null);
const downloadFileName = 'Iris.zip';

void update() async {
if (Platform.isWindows) {
final String executableDirPath = await getExecutableDirPath();
final String tempPath = await getTempPath();
final String filePath = p.join(tempPath, downloadFileName);
final String executablePath = p.join(executableDirPath, 'iris.exe');

if (!File(filePath).existsSync()) {
return;
}

final commands = [
'title Iris',
'timeout /t 2 /nobreak',
'powerShell Expand-Archive -Path "$filePath" -DestinationPath "$tempPath" -Force',
'xcopy $tempPath\\Iris\\* $executableDirPath /s /E /I /Y',
'rd /s /q $tempPath',
'start $executablePath',
];

String resolvedExecutablePath = Platform.resolvedExecutable;
String path = p.dirname(resolvedExecutablePath);
String batFilePath = p.join(path, 'iris-updater.bat');
// 执行 bat 文件
await Process.start(
'cmd',
['/c', commands.join(' && ')],
'cmd.exe',
['/c', batFilePath],
mode: ProcessStartMode.detached,
runInShell: true,
workingDirectory: executableDirPath,
);

// 退出应用
exit(0);
}
}

Future<StreamSubscription> downloadFile(String url, String filePath) async {
final client = http.Client();
final request = http.Request('GET', Uri.parse(url));
final response = await client.send(request);
if (response.statusCode == 200) {
final totalBytes = response.contentLength ?? 0;

var file = File(filePath);
var sink = file.openWrite();

final streamSubscription = response.stream.listen(
(List<int> chunk) {
sink.add(chunk);
progress.value += chunk.length / totalBytes;
},
onDone: () async {
await sink.close();
client.close();
},
onError: (error) {
sink.close();
client.close();
isError.value = true;
isDownload.value = false;
},
cancelOnError: true,
);
return streamSubscription;
} else {
throw Exception('Download failed with status ${response.statusCode}');
}
}

Future<void> download() async {
if (isDesktop && !isDownload.value) {
final String tempPath = await getTempPath();
final String filePath = p.join(tempPath, downloadFileName);

try {
isDownload.value = true;
isError.value = false;
final sub = await downloadFile(release.url, filePath);
downloadingTask.value = sub;
} catch (e) {
isError.value = true;
}
} else {
launchURL(release.url);
Navigator.pop(context, 'OK');
}
}

Future<void> cancel() async {
if (!(progress.value == 0) && downloadingTask.value != null) {
await downloadingTask.value!.cancel();
progress.value = 0.0;
}

if (context.mounted) {
Navigator.pop(context, 'Cancel');
}
Expand All @@ -133,23 +54,7 @@ class ReleaseDialog extends HookWidget {
return AlertDialog(
title: Text('${t.checked_new_version}: ${release.version}'),
content: SingleChildScrollView(
child: isError.value
? SizedBox(
height: 100,
width: 100,
child: Center(
child: Text(t.download_error),
),
)
: isDownload.value
? SizedBox(
height: 100,
width: 100,
child: Center(
child: CircularProgressIndicator(value: progress.value),
),
)
: MarkdownBody(data: release.changeLog, shrinkWrap: true),
child: MarkdownBody(data: release.changeLog, shrinkWrap: true),
),
actions: <Widget>[
TextButton(
Expand All @@ -163,26 +68,19 @@ class ReleaseDialog extends HookWidget {
Visibility(
visible: !isDesktop,
child: TextButton(
onPressed: download,
onPressed: () {
launchURL(release.url);
Navigator.pop(context, 'OK');
},
child: Text(t.download),
),
),
Visibility(
visible: isDesktop,
child: TextButton(
onPressed: progress.value >= 1
? update
: isDownload.value
? null
: download,
child: isError.value
? Text(t.retry)
: progress.value >= 1
? Text(t.confirmUpdate)
: isDownload.value
? Text('${(progress.value * 100).toStringAsFixed(2)} %')
: Text(
'${t.download} (${fileSizeConvert(release.size)} MB)'),
onPressed: update,
child: Text(
'${t.download_and_update} (${fileSizeConvert(release.size)} MB)'),
),
),
],
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.0+2
version: 1.1.1+2

environment:
sdk: ^3.5.4
Expand Down

0 comments on commit d250767

Please sign in to comment.