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))
|
||||
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 {
|
||||
|
|
|
@ -48,6 +48,7 @@ final class ConnectToServerViewModel: ViewModel {
|
|||
|
||||
let formattedURL = url.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
.trimmingCharacters(in: .objectReplacement)
|
||||
.prepending("http://", if: !url.contains("://"))
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
// in case of redirects, we must process the new URL
|
||||
|
||||
let connectionURL = processConnectionURL(initial: url, response: response.response.url)
|
||||
|
||||
let newServerState = ServerState(
|
||||
urls: [url],
|
||||
currentURL: url,
|
||||
urls: [connectionURL],
|
||||
currentURL: connectionURL,
|
||||
name: name,
|
||||
id: id,
|
||||
os: os,
|
||||
|
@ -79,6 +84,27 @@ final class ConnectToServerViewModel: ViewModel {
|
|||
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 {
|
||||
if let _ = try? SwiftfinStore.dataStack.fetchOne(
|
||||
From<SwiftfinStore.Models.StoredServer>(),
|
||||
|
|
|
@ -33,7 +33,7 @@ struct ConnectToServerView: View {
|
|||
@State
|
||||
private var isPresentingError: Bool = false
|
||||
@State
|
||||
private var url = "http://"
|
||||
private var url = ""
|
||||
|
||||
private func connectToServer(at url: String) {
|
||||
let task = Task {
|
||||
|
@ -87,6 +87,9 @@ struct ConnectToServerView: View {
|
|||
.buttonStyle(.card)
|
||||
} else {
|
||||
Button {
|
||||
if !url.contains("://") {
|
||||
url = "http://" + url
|
||||
}
|
||||
connectToServer(at: url)
|
||||
} label: {
|
||||
L10n.connect.text
|
||||
|
|
|
@ -33,7 +33,7 @@ struct ConnectToServerView: View {
|
|||
@State
|
||||
private var isPresentingError: Bool = false
|
||||
@State
|
||||
private var url = "http://"
|
||||
private var url = ""
|
||||
|
||||
private func connectToServer() {
|
||||
let task = Task {
|
||||
|
@ -78,6 +78,9 @@ struct ConnectToServerView: View {
|
|||
}
|
||||
} else {
|
||||
Button {
|
||||
if !url.contains("://") {
|
||||
url = "http://" + url
|
||||
}
|
||||
connectToServer()
|
||||
} label: {
|
||||
L10n.connect.text
|
||||
|
|
Loading…
Reference in New Issue