From 07cb9dfebdcba21bffd776ca195e4737472bd6bd Mon Sep 17 00:00:00 2001 From: Fabio Dela Antonio Date: Mon, 20 Apr 2020 21:01:04 +0100 Subject: [PATCH] [WIP] Adding support for beatsaver:// links --- BeatmapAR/AppDelegate.swift | 35 ++++++++++++++++++++++++++--------- project.yml | 5 +++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/BeatmapAR/AppDelegate.swift b/BeatmapAR/AppDelegate.swift index d8aff4b..90fea21 100644 --- a/BeatmapAR/AppDelegate.swift +++ b/BeatmapAR/AppDelegate.swift @@ -4,6 +4,7 @@ import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let backgroundDownloadQueue = DispatchQueue(label: "DownloadQueue", qos: .background) func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let window = UIWindow(frame: UIScreen.main.bounds) @@ -18,20 +19,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate { open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:] ) -> Bool { - let manager = FileManager.default - guard url.isFileURL, - let documentsURL = manager.urls(for: .documentDirectory, in: .userDomainMask).first - else { + guard let documentsURL = manager.urls(for: .documentDirectory, in: .userDomainMask).first else { return false } - try? manager.moveItem( - at: url, - to: documentsURL.appendingPathComponent(url.lastPathComponent) - ) + if url.scheme == "beatsaver", + let beatSaverId = url.host, + let downloadURL = URL(string: "https://beatsaver.com/api/download/key/\(beatSaverId)") { - return true + // FIXME: Display a download screen instead... + backgroundDownloadQueue.async { + if let beatmapData = try? Data(contentsOf: downloadURL) { + try? beatmapData.write( + to: documentsURL.appendingPathComponent("\(beatSaverId).zip"), + options: .atomicWrite + ) + } + } + + return true + } else if url.isFileURL { + try? manager.moveItem( + at: url, + to: documentsURL.appendingPathComponent(url.lastPathComponent) + ) + + return true + } else { + return false + } } func applicationWillResignActive(_ application: UIApplication) { diff --git a/project.yml b/project.yml index e3e2c4a..ee3a52e 100644 --- a/project.yml +++ b/project.yml @@ -56,6 +56,11 @@ targets: UTTypeTagSpecification: public.filename-extension: zip public.mime-type: application/zip + CFBundleURLTypes: + - CFBundleTypeRole: Editor + CFBundleURLName: beatsaver + CFBundleURLSchemes: + - beatsaver sources: - path: README.md buildPhase: none