Use http by default on ConnectToServerView (#971)

This commit is contained in:
alasclar 2024-04-17 17:42:10 +01:00 committed by GitHub
parent ec9bfaa2fe
commit 272799d9c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 4 deletions

View File

@ -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 {

View File

@ -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>(),

View File

@ -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

View File

@ -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