@@ -41,8 +41,28 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
4141 return [ : ]
4242 }
4343
44+ var cache_hostname : String = " "
45+ func hostname( ) async -> String {
46+ if cache_hostname != " " {
47+ return cache_hostname
48+ }
49+ if let name = storageName {
50+ if let host = await getKeyChain ( key: " \( name) _hostname " ) {
51+ cache_hostname = host
52+ }
53+ return cache_hostname
54+ }
55+ else {
56+ return " api.pcloud.com "
57+ }
58+ }
59+
4460 override func signIn( _ successURL: URL ) async throws -> Bool {
4561 let oauthToken = NSURLComponents ( string: ( successURL. absoluteString) ) ? . queryItems? . filter ( { $0. name == " code " } ) . first
62+ let hostname = NSURLComponents ( string: ( successURL. absoluteString) ) ? . queryItems? . filter ( { $0. name == " hostname " } ) . first
63+ if let hostnameString = hostname? . value {
64+ _ = await setKeyChain ( key: " \( storageName!) _hostname " , value: hostnameString)
65+ }
4666
4767 if let oauthTokenString = oauthToken? . value {
4868 return await getToken ( oauthToken: oauthTokenString)
@@ -53,7 +73,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
5373 override func getToken( oauthToken: String ) async -> Bool {
5474 os_log ( " %{public}@ " , log: log, type: . debug, " getToken(pCloud: \( storageName ?? " " ) ) " )
5575
56- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /oauth2_token " ) !)
76+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /oauth2_token " ) !)
5777 request. httpMethod = " POST "
5878 request. setValue ( " application/x-www-form-urlencoded; charset=UTF-8 " , forHTTPHeaderField: " Content-Type " )
5979
@@ -105,14 +125,15 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
105125 public override func logout( ) async {
106126 if let name = storageName {
107127 let _ = await delKeyChain ( key: " \( name) _accountId " )
128+ let _ = await delKeyChain ( key: " \( name) _hostname " )
108129 }
109130 await super. logout ( )
110131 }
111132
112133 override func revokeToken( token: String ) async -> Bool {
113134 os_log ( " %{public}@ " , log: log, type: . debug, " revokeToken(pCloud: \( storageName ?? " " ) ) " )
114135
115- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /logout " ) !)
136+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /logout " ) !)
116137 request. httpMethod = " POST "
117138 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
118139
@@ -135,7 +156,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
135156 }
136157
137158 override func isAuthorized( ) async -> Bool {
138- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /userinfo " ) !)
159+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /userinfo " ) !)
139160 request. httpMethod = " GET "
140161 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
141162
@@ -160,7 +181,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
160181 return try await callWithRetry ( action: { [ self ] in
161182 os_log ( " %{public}@ " , log: log, type: . debug, " listFolder(pCloud: \( storageName ?? " " ) ) " )
162183
163- let url = " https://api.pcloud.com /listfolder?folderid= \( folderId) &timeformat=timestamp "
184+ let url = " https:// \( await hostname ( ) ) /listfolder?folderid= \( folderId) &timeformat=timestamp "
164185
165186 var request : URLRequest = URLRequest ( url: URL ( string: url) !)
166187 request. httpMethod = " GET "
@@ -307,7 +328,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
307328 return try await callWithRetry ( action: { [ self ] in
308329 os_log ( " %{public}@ " , log: log, type: . debug, " readFile(pCloud: \( storageName ?? " " ) \( fileId) \( start ?? - 1 ) \( length ?? - 1 ) \( ( start ?? 0 ) + ( length ?? 0 ) ) " )
309330
310- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /getfilelink?fileid= \( id) " ) !)
331+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /getfilelink?fileid= \( id) " ) !)
311332 request. httpMethod = " GET "
312333 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
313334
@@ -368,7 +389,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
368389 do {
369390 return try await callWithRetry ( action: { [ self ] in
370391 let body = " folderid= \( folderid) &name= \( name) &timeformat=timestamp "
371- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /createfolder " ) !)
392+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /createfolder " ) !)
372393 request. httpMethod = " POST "
373394 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
374395 request. httpBody = body. data ( using: . utf8)
@@ -421,7 +442,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
421442 do {
422443 return try await callWithRetry ( action: { [ self ] in
423444 let body = " folderid= \( folderId) "
424- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /deletefolderrecursive " ) !)
445+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /deletefolderrecursive " ) !)
425446 request. httpMethod = " POST "
426447 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
427448 request. httpBody = body. data ( using: . utf8)
@@ -449,7 +470,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
449470 do {
450471 return try await callWithRetry ( action: { [ self ] in
451472 let body = " fileid= \( fileId) "
452- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /deletefile " ) !)
473+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /deletefile " ) !)
453474 request. httpMethod = " POST "
454475 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
455476 request. httpBody = body. data ( using: . utf8)
@@ -537,7 +558,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
537558 if let toName = toName {
538559 rename += " &toname= \( toName) "
539560 }
540- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /renamefile " ) !)
561+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /renamefile " ) !)
541562 request. httpMethod = " POST "
542563 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
543564 request. httpBody = rename. data ( using: . utf8)
@@ -578,7 +599,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
578599 if let toName = toName {
579600 rename += " &toname= \( toName) "
580601 }
581- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /renamefolder " ) !)
602+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /renamefolder " ) !)
582603 request. httpMethod = " POST "
583604 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
584605 request. httpBody = rename. data ( using: . utf8)
@@ -710,7 +731,7 @@ public class pCloudStorage: NetworkStorage, URLSessionDataDelegate {
710731 }
711732 let tmpurl = URL ( fileURLWithPath: NSTemporaryDirectory ( ) ) . appendingPathComponent ( " \( Int . random ( in: 0 ... 0xffffffff ) ) " )
712733
713- var request : URLRequest = URLRequest ( url: URL ( string: " https://api.pcloud.com /uploadfile " ) !)
734+ var request : URLRequest = URLRequest ( url: URL ( string: " https:// \( await hostname ( ) ) /uploadfile " ) !)
714735 request. httpMethod = " POST "
715736 request. setValue ( " Bearer \( await accessToken ( ) ) " , forHTTPHeaderField: " Authorization " )
716737 let boundary = " Boundary- \( UUID ( ) . uuidString) "
0 commit comments