Skip to content

Commit

Permalink
Add log to missing content, fix percent coding
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH authored and kelson42 committed Jun 9, 2024
1 parent fd97c37 commit 1cb7325
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
17 changes: 12 additions & 5 deletions Model/Utilities/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ extension URL {
/// It makes sure that trailing slash is preserved,
/// and leading slash is removed.
var contentPath: String {
[scheme, "://", host, "/"]
.compactMap { $0 }
.reduce(absoluteString) { partialResult, prefix in
partialResult.removingPrefix(prefix)
}
if #available(macOS 13.0, iOS 16.0, *) {
return path(percentEncoded: false).removingPrefix("/")
} else {
var path: String = [scheme, "://", host, "/"]
.compactMap { $0 }
.reduce(absoluteString) { partialResult, prefix in
partialResult.removingPrefix(prefix)
}
path = path.removingPercentEncoding ?? path
path = (try? path.replacingRegex(matching: "#.*", with: "")) ?? path
return path
}
}

// swiftlint:disable:next force_try
Expand Down
6 changes: 4 additions & 2 deletions Tests/URLContentPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ final class URLContentPathTests: XCTestCase {

private let testURLs = [
URL(string: "kiwix://6E4F3D4A-2F8A-789A-3B88-212219F4FB27/irp.fas.org/doddir/milmed/index.html")!,
URL(string: "kiwix://861C031F-DAFB-9688-4DB4-8F1199FE2926/mesquartierschinois.wordpress.com/")!
URL(string: "kiwix://861C031F-DAFB-9688-4DB4-8F1199FE2926/mesquartierschinois.wordpress.com/")!,
URL(string: "kiwix://861C031F-DAFB-9688-4DB4-8F1199FE2926/widgets.wp.com/likes/master.html%3Fver%3D20240530#ver=20240530&lang=fr&lang_ver=1713167421&origin=https://mesquartierschinois.wordpress.com")!
]

func test_no_leading_slash() {
Expand All @@ -37,7 +38,8 @@ final class URLContentPathTests: XCTestCase {
func test_value() {
XCTAssertEqual(testURLs.map { $0.contentPath }, [
"irp.fas.org/doddir/milmed/index.html",
"mesquartierschinois.wordpress.com/"
"mesquartierschinois.wordpress.com/",
"widgets.wp.com/likes/master.html?ver=20240530"
])
}

Expand Down
20 changes: 16 additions & 4 deletions ViewModel/BrowserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import CoreData
import CoreLocation
import WebKit
import Defaults
import os

import OrderedCollections
import CoreKiwix
Expand Down Expand Up @@ -281,11 +282,22 @@ final class BrowserViewModel: NSObject, ObservableObject,
decisionHandler(.cancel)
} else if url.isKiwixURL {
guard ZimFileService.shared.getContentSize(url: url) != nil else {
// content is missing
decisionHandler(.cancel)
NotificationCenter.default.post(
name: .alert, object: nil, userInfo: ["rawValue": ActiveAlert.articleFailedToLoad.rawValue]
os_log(
"Missing content at url: %@ => %@",
log: Log.URLSchemeHandler,
type: .error,
url.absoluteString,
url.contentPath
)
decisionHandler(.cancel)
if navigationAction.request.mainDocumentURL == url {
// only show alerts for missing main document
NotificationCenter.default.post(
name: .alert,
object: nil,
userInfo: ["rawValue": ActiveAlert.articleFailedToLoad.rawValue]
)
}
return
}
decisionHandler(.allow)
Expand Down

0 comments on commit 1cb7325

Please sign in to comment.