-
Notifications
You must be signed in to change notification settings - Fork 860
fix: 安装模组未进行文件名存在检查 #5879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix: 安装模组未进行文件名存在检查 #5879
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,13 +54,18 @@ | |||||
| import org.jackhuang.hmcl.util.io.FileUtils; | ||||||
| import org.jetbrains.annotations.Nullable; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | ||||||
| import java.util.Locale; | ||||||
| import java.util.Set; | ||||||
| import java.util.concurrent.CancellationException; | ||||||
| import java.util.function.Supplier; | ||||||
| import java.util.stream.Collectors; | ||||||
|
|
||||||
| import static org.jackhuang.hmcl.ui.FXUtils.runInFX; | ||||||
| import static org.jackhuang.hmcl.util.i18n.I18n.i18n; | ||||||
| import static org.jackhuang.hmcl.util.logging.Logger.LOG; | ||||||
|
|
||||||
| public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage { | ||||||
| private final ReadOnlyObjectWrapper<DecoratorPage.State> state = new ReadOnlyObjectWrapper<>(DecoratorPage.State.fromTitle(i18n("download"), -1)); | ||||||
|
|
@@ -134,6 +139,19 @@ public static void download(DownloadProvider downloadProvider, Profile profile, | |||||
|
|
||||||
| Path runDirectory = profile.getRepository().hasVersion(version) ? profile.getRepository().getRunDirectory(version) : profile.getRepository().getBaseDirectory(); | ||||||
|
|
||||||
| Set<String> existingFiles; | ||||||
|
|
||||||
| try (var list = Files.list(runDirectory.resolve(subdirectoryName))) { | ||||||
| existingFiles = list.map(Path::getFileName) | ||||||
| .map(Path::toString) | ||||||
| .collect(Collectors.toSet()); | ||||||
| } catch (IOException e) { | ||||||
| LOG.warning("Failed to list files in " + runDirectory.resolve(subdirectoryName), e); | ||||||
| existingFiles = Set.of(); | ||||||
| } | ||||||
|
|
||||||
| Set<String> finalExistingFiles = existingFiles; | ||||||
|
|
||||||
| Controllers.prompt(i18n("archive.file.name"), (result, handler) -> { | ||||||
| Path dest = runDirectory.resolve(subdirectoryName).resolve(result); | ||||||
|
|
||||||
|
|
@@ -153,7 +171,7 @@ public static void download(DownloadProvider downloadProvider, Profile profile, | |||||
| } | ||||||
| }), i18n("message.downloading"), TaskCancellationAction.NORMAL); | ||||||
| handler.resolve(); | ||||||
| }, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid)); | ||||||
| }, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid), new Validator(i18n("profile.already_exists"), (it) -> !finalExistingFiles.contains(it))); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 由于
Suggested change
|
||||||
|
|
||||||
| } | ||||||
|
|
||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议在调用
Files.list之前先检查目录是否存在。如果子目录(如mods)尚未创建,Files.list会抛出NoSuchFileException,导致 catch 块记录不必要的警告日志。此外,预先解析路径可以提高代码可读性并避免重复解析。同时,为了支持跨平台的文件名冲突检查(如 Windows/macOS 默认不区分大小写),建议将文件名统一转换为小写存储。