Use http by default on ConnectToServerView (#971)
This commit is contained in:
parent
ec9bfaa2fe
commit
272799d9c5
|
@ -99,6 +99,21 @@ extension String {
|
||||||
let characters = Self.alphanumeric.randomSample(count: Int.random(in: range))
|
let characters = Self.alphanumeric.randomSample(count: Int.random(in: range))
|
||||||
return String(characters)
|
return String(characters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func trimmingSuffix(_ suffix: String) -> String {
|
||||||
|
|
||||||
|
guard suffix.count <= count else { return self }
|
||||||
|
|
||||||
|
var s = self
|
||||||
|
var suffix = suffix
|
||||||
|
|
||||||
|
while s.last == suffix.last {
|
||||||
|
s.removeLast()
|
||||||
|
suffix.removeLast()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension CharacterSet {
|
extension CharacterSet {
|
||||||
|
|
|
@ -48,6 +48,7 @@ final class ConnectToServerViewModel: ViewModel {
|
||||||
|
|
||||||
let formattedURL = url.trimmingCharacters(in: .whitespacesAndNewlines)
|
let formattedURL = url.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
.trimmingCharacters(in: .objectReplacement)
|
.trimmingCharacters(in: .objectReplacement)
|
||||||
|
.prepending("http://", if: !url.contains("://"))
|
||||||
|
|
||||||
guard let url = URL(string: formattedURL) else { throw JellyfinAPIError("Invalid URL") }
|
guard let url = URL(string: formattedURL) else { throw JellyfinAPIError("Invalid URL") }
|
||||||
|
|
||||||
|
@ -66,9 +67,13 @@ final class ConnectToServerViewModel: ViewModel {
|
||||||
throw JellyfinAPIError("Missing server data from network call")
|
throw JellyfinAPIError("Missing server data from network call")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// in case of redirects, we must process the new URL
|
||||||
|
|
||||||
|
let connectionURL = processConnectionURL(initial: url, response: response.response.url)
|
||||||
|
|
||||||
let newServerState = ServerState(
|
let newServerState = ServerState(
|
||||||
urls: [url],
|
urls: [connectionURL],
|
||||||
currentURL: url,
|
currentURL: connectionURL,
|
||||||
name: name,
|
name: name,
|
||||||
id: id,
|
id: id,
|
||||||
os: os,
|
os: os,
|
||||||
|
@ -79,6 +84,27 @@ final class ConnectToServerViewModel: ViewModel {
|
||||||
return (newServerState, url)
|
return (newServerState, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this probably isn't the best way to properly handle this, fix if necessary
|
||||||
|
private func processConnectionURL(initial url: URL, response: URL?) -> URL {
|
||||||
|
|
||||||
|
guard let response else { return url }
|
||||||
|
|
||||||
|
if url.scheme != response.scheme ||
|
||||||
|
url.host != response.host
|
||||||
|
{
|
||||||
|
var newURL = response.absoluteString.trimmingSuffix(Paths.getPublicSystemInfo.url?.absoluteString ?? "")
|
||||||
|
|
||||||
|
// if ended in a "/"
|
||||||
|
if url.absoluteString.last == "/" {
|
||||||
|
newURL.append("/")
|
||||||
|
}
|
||||||
|
|
||||||
|
return URL(string: newURL) ?? url
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
func isDuplicate(server: ServerState) -> Bool {
|
func isDuplicate(server: ServerState) -> Bool {
|
||||||
if let _ = try? SwiftfinStore.dataStack.fetchOne(
|
if let _ = try? SwiftfinStore.dataStack.fetchOne(
|
||||||
From<SwiftfinStore.Models.StoredServer>(),
|
From<SwiftfinStore.Models.StoredServer>(),
|
||||||
|
|
|
@ -33,7 +33,7 @@ struct ConnectToServerView: View {
|
||||||
@State
|
@State
|
||||||
private var isPresentingError: Bool = false
|
private var isPresentingError: Bool = false
|
||||||
@State
|
@State
|
||||||
private var url = "http://"
|
private var url = ""
|
||||||
|
|
||||||
private func connectToServer(at url: String) {
|
private func connectToServer(at url: String) {
|
||||||
let task = Task {
|
let task = Task {
|
||||||
|
@ -87,6 +87,9 @@ struct ConnectToServerView: View {
|
||||||
.buttonStyle(.card)
|
.buttonStyle(.card)
|
||||||
} else {
|
} else {
|
||||||
Button {
|
Button {
|
||||||
|
if !url.contains("://") {
|
||||||
|
url = "http://" + url
|
||||||
|
}
|
||||||
connectToServer(at: url)
|
connectToServer(at: url)
|
||||||
} label: {
|
} label: {
|
||||||
L10n.connect.text
|
L10n.connect.text
|
||||||
|
|
|
@ -33,7 +33,7 @@ struct ConnectToServerView: View {
|
||||||
@State
|
@State
|
||||||
private var isPresentingError: Bool = false
|
private var isPresentingError: Bool = false
|
||||||
@State
|
@State
|
||||||
private var url = "http://"
|
private var url = ""
|
||||||
|
|
||||||
private func connectToServer() {
|
private func connectToServer() {
|
||||||
let task = Task {
|
let task = Task {
|
||||||
|
@ -78,6 +78,9 @@ struct ConnectToServerView: View {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Button {
|
Button {
|
||||||
|
if !url.contains("://") {
|
||||||
|
url = "http://" + url
|
||||||
|
}
|
||||||
connectToServer()
|
connectToServer()
|
||||||
} label: {
|
} label: {
|
||||||
L10n.connect.text
|
L10n.connect.text
|
||||||
|
|
Loading…
Reference in New Issue