-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for url rewriting. (#6)
- Loading branch information
Showing
14 changed files
with
191 additions
and
118 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Logging.swift | ||
// URLHelperApp | ||
// | ||
// Created by Grigory Entin on 02/12/2023. | ||
// | ||
|
||
import Foundation | ||
import os.log | ||
|
||
private let logSubsystem = Bundle.main.bundleIdentifier! | ||
|
||
extension Logger { | ||
init(category: String) { | ||
self.init(subsystem: logSubsystem, category: category) | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// SampleURLResolver.swift | ||
// URLHelperApp | ||
// | ||
// Created by Grigory Entin on 06/10/2018. | ||
// Copyright © 2018 Grigory Entin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URL { | ||
|
||
fileprivate func matchingURLResolution() -> URLResolution { | ||
switch self { | ||
case _ where absoluteString.hasPrefix("https://stackoverflow.com/"): | ||
return URLResolution(finalURL: self, appBundleIdentifier: "org.epichrome.app.Coding") | ||
default: | ||
return URLResolution(finalURL: self, appBundleIdentifier: "com.google.Chrome") | ||
} | ||
} | ||
} | ||
|
||
class SampleURLResolver : URLResolver { | ||
|
||
func resolveURL(_ url: URL) async throws -> URLResolution? { | ||
url.matchingURLResolution() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// ScriptBasedURLResolver.swift | ||
// URLHelperApp | ||
// | ||
// Created by Grigory Entin on 05/10/2018. | ||
// Copyright © 2018 Grigory Entin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class ScriptBasedURLResolver : URLResolver { | ||
|
||
let fileManager = FileManager() | ||
let resolverScriptName = "AppBundleIdentifierAndURLForURL" | ||
|
||
var defaultResolverURL: URL { | ||
let scriptsDirectoryURL: URL = try! fileManager.url(for: .applicationScriptsDirectory, in: .userDomainMask, appropriateFor: nil, create: true) | ||
let resolverURL = scriptsDirectoryURL.appendingPathComponent(resolverScriptName) | ||
return resolverURL | ||
} | ||
|
||
func preprocessResolverURL() async throws -> URL? { | ||
try await makeSureResolverScriptExists(resolverURL: defaultResolverURL) | ||
} | ||
|
||
func makeSureResolverScriptExists(resolverURL: URL) async throws -> URL? { | ||
do { | ||
try fileManager.copyItem(at: bundledResolverURL, to: resolverURL) | ||
return resolverURL | ||
} catch { | ||
switch error { | ||
case CocoaError.fileWriteFileExists: | ||
return resolverURL | ||
case CocoaError.fileWriteNoPermission: | ||
guard let updatedResolverURL = try await facilitateWriteAccessForURLResolverScript(at: resolverURL) else { | ||
return nil | ||
} | ||
return try await makeSureResolverScriptExists(resolverURL: updatedResolverURL) | ||
default: | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
var bundledResolverURL: URL { | ||
let bundle = Bundle(for: type(of: self)) | ||
return bundle.url(forResource: resolverScriptName, withExtension: "")! | ||
} | ||
|
||
func resolveURL(_ url: URL) async throws -> URLResolution? { | ||
guard let resolverURL = try await preprocessResolverURL() else { | ||
return nil | ||
} | ||
return try await resolveURLWithoutPreprocessing(url, resolverURL: resolverURL) | ||
} | ||
|
||
private func resolveURLWithoutPreprocessing(_ url: URL, resolverURL: URL) async throws -> URLResolution { | ||
let data = try await outputFromLaunching(executableURL: resolverURL, arguments: [url.absoluteString]) | ||
let resolution = try JSONDecoder().decode(URLResolution.self, from: data) | ||
return resolution | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// URLResolver.swift | ||
// URLHelperApp | ||
// | ||
// Created by Grigory Entin on 04/10/2018. | ||
// Copyright © 2018 Grigory Entin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct URLResolution: Decodable { | ||
let finalURL: URL | ||
let appBundleIdentifier: String | ||
} | ||
|
||
protocol URLResolver { | ||
func resolveURL(_ url: URL) async throws -> URLResolution? | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// WriteAccessFacilitator.swift | ||
// URLHelperApp | ||
// | ||
// Created by Grigory Entin on 01/12/2023. | ||
// | ||
|
||
import AppKit | ||
import Foundation | ||
|
||
private let fileManager = FileManager() | ||
|
||
private var appName: String { | ||
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? fileManager.displayName(atPath: Bundle.main.bundlePath) | ||
} | ||
|
||
func facilitateWriteAccessForURLResolverScript(at url: URL) async throws -> URL? { | ||
try await facilitateWriteAccessViaUserInteraction(to: url, message: String(localized: "Select the location for the resolver script for \(appName)")) | ||
} | ||
|
||
@MainActor | ||
func facilitateWriteAccessViaUserInteraction(to url: URL, message: String) async throws -> URL? { | ||
let panel = { | ||
$0.message = message | ||
$0.directoryURL = url.deletingLastPathComponent() | ||
$0.nameFieldStringValue = url.lastPathComponent | ||
$0.prompt = String(localized: "Select") | ||
return $0 | ||
}(NSSavePanel()) | ||
|
||
return panel.url | ||
} |
This file contains 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