Skip to content

Commit

Permalink
httpCookieStorage (#80)
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jul 19, 2024
1 parent 5fa0dd8 commit 4cf36e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 61 deletions.
62 changes: 31 additions & 31 deletions Sources/NextcloudKit/NKCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,26 @@ public class NKCommon: NSObject {
internal lazy var sessionConfiguration: URLSessionConfiguration = {
let configuration = URLSessionConfiguration.af.default
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
if let groupIdentifier {
let cookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: groupIdentifier)
configuration.httpCookieStorage = cookieStorage
} else {
configuration.httpCookieStorage = nil
}
return configuration
}()
internal var rootQueue: DispatchQueue = DispatchQueue(label: "com.nextcloud.nextcloudkit.sessionManagerData.rootQueue")
internal var requestQueue: DispatchQueue?
internal var serializationQueue: DispatchQueue?

internal var internalUser = ""
internal var internalUserId = ""
internal var internalPassword = ""
internal var internalAccount = ""
internal var internalUrlBase = ""
internal var internalUserAgent: String?
internal var internalNextcloudVersion: Int = 0
internal var _user = ""

Check failure on line 112 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_user' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _userId = ""

Check failure on line 113 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_userId' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _password = ""

Check failure on line 114 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_password' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _account = ""

Check failure on line 115 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_account' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _urlBase = ""

Check failure on line 116 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_urlBase' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _userAgent: String?

Check failure on line 117 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_userAgent' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _nextcloudVersion: Int = 0

Check failure on line 118 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_nextcloudVersion' should only contain alphanumeric and other allowed characters (identifier_name)
internal var _groupIdentifier: String?

Check failure on line 119 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Identifier Name Violation: Variable name '_groupIdentifier' should only contain alphanumeric and other allowed characters (identifier_name)

internal var internalTypeIdentifiers: [UTTypeConformsToServer] = []
internal var utiCache = NSCache<NSString, CFString>()
Expand All @@ -126,31 +133,35 @@ public class NKCommon: NSObject {
private let queueLog = DispatchQueue(label: "com.nextcloud.nextcloudkit.queuelog", attributes: .concurrent )

public var user: String {
return internalUser
return _user
}

public var userId: String {
return internalUserId
return _userId
}

public var password: String {
return internalPassword
return _password
}

public var account: String {
return internalAccount
return _account
}

public var urlBase: String {
return internalUrlBase
return _urlBase
}

public var userAgent: String? {
return internalUserAgent
return _userAgent
}

public var nextcloudVersion: Int {
return internalNextcloudVersion
return _nextcloudVersion
}

public var groupIdentifier: String? {
return _groupIdentifier
}

public let backgroundQueue = DispatchQueue(label: "com.nextcloud.nextcloudkit.backgroundqueue", qos: .background, attributes: .concurrent)
Expand Down Expand Up @@ -225,15 +236,13 @@ public class NKCommon: NSObject {
// MARK: - Type Identifier

public func getInternalTypeIdentifier(typeIdentifier: String) -> [UTTypeConformsToServer] {

var results: [UTTypeConformsToServer] = []

for internalTypeIdentifier in internalTypeIdentifiers {
if internalTypeIdentifier.typeIdentifier == typeIdentifier {
results.append(internalTypeIdentifier)
}
}

return results
}

Expand Down Expand Up @@ -305,7 +314,6 @@ public class NKCommon: NSObject {
iconName = fileProperties.iconName
}
}

return(mimeType: mimeType, classFile: classFile, iconName: iconName, typeIdentifier: typeIdentifier, fileNameWithoutExt: fileNameWithoutExt, ext: ext)
}

Expand Down Expand Up @@ -367,17 +375,19 @@ public class NKCommon: NSObject {
}
}
}

return fileProperty
}

// MARK: - Chunked File

public func chunkedFile(inputDirectory: String, outputDirectory: String, fileName: String, chunkSize: Int, filesChunk: [(fileName: String, size: Int64)],
public func chunkedFile(inputDirectory: String,

Check warning on line 383 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
outputDirectory: String,
fileName: String,
chunkSize: Int,
filesChunk: [(fileName: String, size: Int64)],
numChunks: @escaping (_ num: Int) -> Void = { _ in },
counterChunk: @escaping (_ counter: Int) -> Void = { _ in },
completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)]) -> Void = { _ in }) {

// Check if filesChunk is empty
if !filesChunk.isEmpty { return completion(filesChunk) }

Expand Down Expand Up @@ -422,13 +432,10 @@ public class NKCommon: NSObject {
}

repeat {

if stop {
return completion([])
}

if autoreleasepool(invoking: { () -> Int in

if chunk >= chunkSize {
writer?.closeFile()
writer = nil
Expand Down Expand Up @@ -461,9 +468,7 @@ public class NKCommon: NSObject {
}
filesChunk = []
return 0

}) == 0 { break }

} while true

writer?.closeFile()
Expand All @@ -476,7 +481,6 @@ public class NKCommon: NSObject {
filesChunk[counter].size = incrementalSize
counter += 1
}

return completion(filesChunk)
}

Expand Down Expand Up @@ -510,18 +514,16 @@ public class NKCommon: NSObject {
headers.update(name: "Accept", value: "application/json")
}
headers.update(name: "OCS-APIRequest", value: "true")

for (key, value) in appendHeaders ?? [:] {
headers.update(name: key, value: value)
}

return headers
}

public func createStandardUrl(serverUrl: String, endpoint: String) -> URLConvertible? {
guard var serverUrl = serverUrl.urlEncoded else { return nil }
if serverUrl.last != "/" { serverUrl = serverUrl + "/" }

if serverUrl.last != "/" { serverUrl = serverUrl + "/" }
serverUrl = serverUrl + endpoint
return serverUrl.asUrl
}
Expand All @@ -532,7 +534,6 @@ public class NKCommon: NSObject {

dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = format

guard let date = dateFormatter.date(from: dateString) else { return nil }
return date
}
Expand All @@ -542,7 +543,6 @@ public class NKCommon: NSObject {

dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = format

return dateFormatter.string(from: date)
}

Expand Down
43 changes: 13 additions & 30 deletions Sources/NextcloudKit/NextcloudKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ open class NextcloudKit: SessionDelegate {

// MARK: - Setup

public func setup(account: String? = nil, user: String, userId: String, password: String, urlBase: String, userAgent: String, nextcloudVersion: Int, delegate: NKCommonDelegate?) {
self.setup(account: account, user: user, userId: userId, password: password, urlBase: urlBase)
public func setup(account: String? = nil, user: String, userId: String, password: String, urlBase: String, userAgent: String, nextcloudVersion: Int, groupIdentifier: String? = nil, delegate: NKCommonDelegate?) {
self.setup(account: account, user: user, userId: userId, password: password, urlBase: urlBase, groupIdentifier: groupIdentifier)
self.setup(userAgent: userAgent)
self.setup(nextcloudVersion: nextcloudVersion)
self.setup(delegate: delegate)
}

public func setup(account: String? = nil, user: String, userId: String, password: String, urlBase: String) {
public func setup(account: String? = nil, user: String, userId: String, password: String, urlBase: String, groupIdentifier: String? = nil) {
self.nkCommonInstance._groupIdentifier = groupIdentifier
if (self.nkCommonInstance.account != account) || (self.nkCommonInstance.urlBase != urlBase && self.nkCommonInstance.user != user) {
if let cookieStore = sessionManager.session.configuration.httpCookieStorage {
for cookie in cookieStore.cookies ?? [] {
Expand All @@ -88,45 +89,27 @@ open class NextcloudKit: SessionDelegate {
self.nkCommonInstance.internalTypeIdentifiers = []
}

if let account = account {
self.nkCommonInstance.internalAccount = account
if let account {
self.nkCommonInstance._account = account
} else {
self.nkCommonInstance.internalAccount = ""
self.nkCommonInstance._account = ""
}
self.nkCommonInstance.internalUser = user
self.nkCommonInstance.internalUserId = userId
self.nkCommonInstance.internalPassword = password
self.nkCommonInstance.internalUrlBase = urlBase
self.nkCommonInstance._user = user
self.nkCommonInstance._userId = userId
self.nkCommonInstance._password = password
self.nkCommonInstance._urlBase = urlBase
}

public func setup(delegate: NKCommonDelegate?) {
self.nkCommonInstance.delegate = delegate
}

public func setup(userAgent: String) {
self.nkCommonInstance.internalUserAgent = userAgent
self.nkCommonInstance._userAgent = userAgent
}

public func setup(nextcloudVersion: Int) {
self.nkCommonInstance.internalNextcloudVersion = nextcloudVersion
}

public func setupSessionManager(sessionConfiguration: URLSessionConfiguration?,
rootQueue: DispatchQueue?,
requestQueue: DispatchQueue?,
serializationQueue: DispatchQueue?) {
if let sessionConfiguration = sessionConfiguration {
self.nkCommonInstance.sessionConfiguration = sessionConfiguration
}
if let rootQueue = rootQueue {
self.nkCommonInstance.rootQueue = rootQueue
}
if let requestQueue = requestQueue {
self.nkCommonInstance.requestQueue = requestQueue
}
if let serializationQueue = serializationQueue {
self.nkCommonInstance.serializationQueue = serializationQueue
}
self.nkCommonInstance._nextcloudVersion = nextcloudVersion
}

/*
Expand Down

0 comments on commit 4cf36e5

Please sign in to comment.