diff options
| author | Fuwn <[email protected]> | 2026-02-23 11:11:20 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-23 13:33:42 -0800 |
| commit | 878ab7f18b467580844d2971970297703d2679ff (patch) | |
| tree | a4c38e6a9d17051887fd9a7c9b3b1d54bfba4f0e /Sora | |
| parent | chore: refine english copy and localization labels (diff) | |
| download | sora-testing-878ab7f18b467580844d2971970297703d2679ff.tar.xz sora-testing-878ab7f18b467580844d2971970297703d2679ff.zip | |
fix: guard post detail url actions against invalid urls
Diffstat (limited to 'Sora')
| -rw-r--r-- | Sora/Views/Post/Details/PostDetailsImageView.swift | 38 | ||||
| -rw-r--r-- | Sora/Views/Post/Details/PostDetailsView.swift | 8 |
2 files changed, 35 insertions, 11 deletions
diff --git a/Sora/Views/Post/Details/PostDetailsImageView.swift b/Sora/Views/Post/Details/PostDetailsImageView.swift index dda1356..1e0da72 100644 --- a/Sora/Views/Post/Details/PostDetailsImageView.swift +++ b/Sora/Views/Post/Details/PostDetailsImageView.swift @@ -34,14 +34,17 @@ struct PostDetailsImageView<Placeholder: View>: View { #if os(iOS) if settings.enableShareShortcut { Button { + guard let shareURL = url else { return } + keyWindow?.rootViewController?.present( UIActivityViewController( - activityItems: [url ?? URL(string: "")!], applicationActivities: nil + activityItems: [shareURL], applicationActivities: nil ), animated: true ) } label: { Label("Share", systemImage: "square.and.arrow.up") } + .disabled(url == nil) } #endif @@ -96,14 +99,19 @@ struct PostDetailsImageView<Placeholder: View>: View { .keyboardShortcut("c", modifiers: [.command]) Button { - openURL(postURL(for: post?.id ?? "")) + guard let postURL = postURL(for: post?.id) else { return } + + openURL(postURL) } label: { Label("Open Post in Safari", systemImage: "safari") } + .disabled(postURL(for: post?.id) == nil) - if let source = post?.source { + if let source = post?.source, + let sourceURL = URL(string: source.trimmingCharacters(in: .whitespacesAndNewlines)) + { Button { - openURL(URL(string: source)!) + openURL(sourceURL) } label: { Label("Open Source Link in Safari", systemImage: "safari") } @@ -170,17 +178,31 @@ struct PostDetailsImageView<Placeholder: View>: View { self.post = post } - private func postURL(for id: String) -> URL { + private func postURL(for id: String?) -> URL? { + guard let id, !id.isEmpty else { return nil } + + var components = URLComponents() + + components.scheme = "https" + components.host = manager.domain + switch manager.flavor { case .moebooru: - return URL(string: "https://\(manager.domain)/post/show/\(id)")! + components.path = "/post/show/\(id)" case .gelbooru: - return URL(string: "https://\(manager.domain)/index.php?page=post&s=view&id=\(id)")! + components.path = "/index.php" + components.queryItems = [ + URLQueryItem(name: "page", value: "post"), + URLQueryItem(name: "s", value: "view"), + URLQueryItem(name: "id", value: id), + ] case .danbooru: - return URL(string: "https://\(manager.domain)/posts/\(id)")! + components.path = "/posts/\(id)" } + + return components.url } #if os(macOS) diff --git a/Sora/Views/Post/Details/PostDetailsView.swift b/Sora/Views/Post/Details/PostDetailsView.swift index 8dc95d4..9c55798 100644 --- a/Sora/Views/Post/Details/PostDetailsView.swift +++ b/Sora/Views/Post/Details/PostDetailsView.swift @@ -129,9 +129,11 @@ struct PostDetailsView: View { #if os(macOS) if settings.enableShareShortcut { - ToolbarItem { - ShareLink(item: imageURL!) { - Label("Share", systemImage: "square.and.arrow.up") + if let imageURL { + ToolbarItem { + ShareLink(item: imageURL) { + Label("Share", systemImage: "square.and.arrow.up") + } } } } |