From 0424436114307b7bbac9af3d0f6d6a67777bc0e1 Mon Sep 17 00:00:00 2001 From: PangMo5 Date: Fri, 28 May 2021 02:44:25 +0900 Subject: [PATCH] JellyfinAPI authHeader trim issue clear --- JellyfinPlayer.xcodeproj/project.pbxproj | 12 ++++++++++++ JellyfinPlayer/APIs/JellyfinAPI.swift | 24 ++++++++++++++++-------- JellyfinPlayer/ContentView.swift | 8 +++++++- JellyfinPlayer/Extensions/String++.swift | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 JellyfinPlayer/Extensions/String++.swift diff --git a/JellyfinPlayer.xcodeproj/project.pbxproj b/JellyfinPlayer.xcodeproj/project.pbxproj index 591aea64..8e575c32 100644 --- a/JellyfinPlayer.xcodeproj/project.pbxproj +++ b/JellyfinPlayer.xcodeproj/project.pbxproj @@ -43,6 +43,7 @@ 53FF7F2A263CF3F500585C35 /* LatestMediaView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FF7F29263CF3F500585C35 /* LatestMediaView.swift */; }; 6213388E265F777C00A81A2A /* LibraryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6213388D265F777C00A81A2A /* LibraryViewModel.swift */; }; 62133890265F83A900A81A2A /* LibraryListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6213388F265F83A900A81A2A /* LibraryListView.swift */; }; + 621338932660107500A81A2A /* String++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 621338922660107500A81A2A /* String++.swift */; }; 6273DD43265F4195009C1D0B /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = 6273DD42265F4195009C1D0B /* Moya */; }; 6273DD45265F4195009C1D0B /* CombineMoya in Frameworks */ = {isa = PBXBuildFile; productRef = 6273DD44265F4195009C1D0B /* CombineMoya */; }; 6273DD48265F41B3009C1D0B /* JellyfinAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6273DD47265F41B3009C1D0B /* JellyfinAPI.swift */; }; @@ -108,6 +109,7 @@ 53FF7F29263CF3F500585C35 /* LatestMediaView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatestMediaView.swift; sourceTree = ""; }; 6213388D265F777C00A81A2A /* LibraryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryViewModel.swift; sourceTree = ""; }; 6213388F265F83A900A81A2A /* LibraryListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryListView.swift; sourceTree = ""; }; + 621338922660107500A81A2A /* String++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String++.swift"; sourceTree = ""; }; 6273DD47265F41B3009C1D0B /* JellyfinAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JellyfinAPI.swift; sourceTree = ""; }; 6273DD4D265F47B2009C1D0B /* LibrarySearchViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibrarySearchViewModel.swift; sourceTree = ""; }; AE8C3153265D60BF008AA076 /* SettingsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsModel.swift; sourceTree = ""; }; @@ -156,6 +158,7 @@ 5377CBF3263B596A003A4E83 /* JellyfinPlayer */ = { isa = PBXGroup; children = ( + 621338912660106C00A81A2A /* Extensions */, 6273DD4A265F4794009C1D0B /* Domains */, 6273DD46265F419B009C1D0B /* APIs */, AE8C3157265D6F5E008AA076 /* Resources */, @@ -223,6 +226,14 @@ path = ViewModels; sourceTree = ""; }; + 621338912660106C00A81A2A /* Extensions */ = { + isa = PBXGroup; + children = ( + 621338922660107500A81A2A /* String++.swift */, + ); + path = Extensions; + sourceTree = ""; + }; 6273DD46265F419B009C1D0B /* APIs */ = { isa = PBXGroup; children = ( @@ -385,6 +396,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 621338932660107500A81A2A /* String++.swift in Sources */, 53FF7F2A263CF3F500585C35 /* LatestMediaView.swift in Sources */, 5377CBFE263B596B003A4E83 /* PersistenceController.swift in Sources */, 5389276E263C25100035E14B /* ContinueWatchingView.swift in Sources */, diff --git a/JellyfinPlayer/APIs/JellyfinAPI.swift b/JellyfinPlayer/APIs/JellyfinAPI.swift index 300038cd..cebe8c4f 100644 --- a/JellyfinPlayer/APIs/JellyfinAPI.swift +++ b/JellyfinPlayer/APIs/JellyfinAPI.swift @@ -67,7 +67,7 @@ extension Filter { parameters["ImageTypeLimit"] = imageTypeLimit parameters["IncludeItemTypes"] = itemTypes.map(\.rawValue).joined(separator: ",") parameters["ParentId"] = parentID - parameters["Recursive"] = recursive.description + parameters["Recursive"] = recursive parameters["SortBy"] = sort?.rawValue parameters["SortOrder"] = asc?.rawValue parameters["Genres"] = genres.joined(separator: ",") @@ -83,6 +83,7 @@ enum JellyfinAPI { } extension JellyfinAPI: TargetType { + var baseURL: URL { switch self { case let .items(global, _, _), @@ -117,12 +118,12 @@ extension JellyfinAPI: TargetType { parameters["searchTerm"] = searchQuery parameters["StartIndex"] = (page - 1) * 100 parameters["Limit"] = 100 - return .requestParameters(parameters: parameters, encoding: URLEncoding.default) + return .requestParameters(parameters: parameters, encoding: URLEncoding.jellyfin) case let .items(_, filter, page): var parameters = filter.toParamters parameters["StartIndex"] = (page - 1) * 100 parameters["Limit"] = 100 - return .requestParameters(parameters: parameters, encoding: URLEncoding.default) + return .requestParameters(parameters: parameters, encoding: URLEncoding.jellyfin) } } @@ -130,11 +131,18 @@ extension JellyfinAPI: TargetType { switch self { case let .items(global, _, _), let .search(global, _, _, _): - return [ - "X-Emby-Authorization": global.authHeader, - "Content-Type": "application/json", - "Accept": "application/json" - ] + var headers = [String: String]() + headers["Content-Type"] = "application/json" + headers["Accept"] = "application/json" + headers["X-Emby-Authorization"] = global.authHeader + return headers } } } + +extension URLEncoding { + + static var jellyfin: URLEncoding { + URLEncoding(destination: .methodDependent, arrayEncoding: .noBrackets, boolEncoding: .literal) + } +} diff --git a/JellyfinPlayer/ContentView.swift b/JellyfinPlayer/ContentView.swift index 5a5d20f8..d2554751 100644 --- a/JellyfinPlayer/ContentView.swift +++ b/JellyfinPlayer/ContentView.swift @@ -80,7 +80,13 @@ struct ContentView: View { } let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String; - globalData.authHeader = "MediaBrowser Client=\"SwiftFin\", Device=\"\(UIDevice.current.name)\", DeviceId=\"\(globalData.user?.device_uuid ?? "")\", Version=\"\(appVersion ?? "0.0.1")\", Token=\"\(globalData.authToken)\""; + var header = "MediaBrowser " + header.append("Client=\"SwiftFin\",") + header.append("Device=\"\(UIDevice.current.name.removeRegexMatches(pattern: "[^\\w\\s]"))\",") + header.append("DeviceId=\"\(globalData.user?.device_uuid ?? "")\",") + header.append("Version=\"\(appVersion ?? "0.0.1")\",") + header.append("Token=\"\(globalData.authToken)\"") + globalData.authHeader = header let request = RestRequest(method: .get, url: (globalData.server?.baseURI ?? "") + "/Users/Me") request.headerParameters["X-Emby-Authorization"] = globalData.authHeader diff --git a/JellyfinPlayer/Extensions/String++.swift b/JellyfinPlayer/Extensions/String++.swift new file mode 100644 index 00000000..dfbddfe7 --- /dev/null +++ b/JellyfinPlayer/Extensions/String++.swift @@ -0,0 +1,18 @@ +// +// String++.swift +// JellyfinPlayer +// +// Created by PangMo5 on 2021/05/28. +// + +import Foundation + +extension String { + func removeRegexMatches(pattern: String, replaceWith: String = "") -> String { + do { + let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive) + let range = NSRange(location: 0, length: count) + return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith) + } catch { return self } + } +}