Skip to content

Commit

Permalink
Merge pull request #75 from uploadcare/develop
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
makoni authored Dec 16, 2020
2 parents 65d2c94 + 4bfae72 commit 035fc3d
Show file tree
Hide file tree
Showing 5 changed files with 559 additions and 476 deletions.
41 changes: 37 additions & 4 deletions Demo/Demo/Tester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func sizeString(ofData data: Data) -> String {
class Tester {
private lazy var uploadcare: Uploadcare = {
// Define your Public Key here
let uploadcare = Uploadcare(withPublicKey: "", secretKey: "")
let uploadcare = Uploadcare(withPublicKey: "demopublickey", secretKey: "demopublickey")
// uploadcare.authScheme = .simple
// or
// uploadcare.authScheme = .signed
Expand Down Expand Up @@ -127,12 +127,20 @@ class Tester {
// queue.async {
// self.testDocumentConversion()
// }
queue.async {
self.testDocumentConversionStatus()
}
// queue.async {
// self.testDocumentConversionStatus()
// }
// queue.async {
// self.testVideoConversionStatus()
// }

queue.async {
self.testDirectUploadBug()
}
queue.async {
self.testDirectUploadBug()
}

}

func testUploadFileInfo() {
Expand Down Expand Up @@ -199,6 +207,31 @@ class Tester {
semaphore.wait()
}

func testDirectUploadBug() {
print("<------ testDirectUpload ------>")
guard let url = URL(string: "https://source.unsplash.com/random"), let data = try? Data(contentsOf: url) else { return }

print("size of file: \(sizeString(ofData: data))")

let semaphore = DispatchSemaphore(value: 0)

let api = Uploadcare(withPublicKey: "demopublickey", secretKey: "demopublickey")
api.uploadAPI.upload(files: ["random_file_name.jpg": data], store: .store, { (progress) in

}) { (resultDictionary, error) in
defer { semaphore.signal() }

if let error = error {
print(error)
return
}

print(resultDictionary ?? "nil")
}

semaphore.wait()
}

func testDirectUpload() {
print("<------ testDirectUpload ------>")
guard let url = URL(string: "https://source.unsplash.com/random"), let data = try? Data(contentsOf: url) else { return }
Expand Down
50 changes: 50 additions & 0 deletions Sources/Uploadcare/BackgroundSessionManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// BackgroundSessionManager.swift
//
//
// Created by Sergey Armodin on 03.12.2020.
// Copyright © 2020 Uploadcare, Inc. All rights reserved.
//

import Foundation

class BackgroundSessionManager: NSObject {
static let instance = BackgroundSessionManager()

lazy var session: URLSession = {
let bundle = Bundle.main.bundleIdentifier ?? ""
let config = URLSessionConfiguration.background(withIdentifier: "\(bundle).com.uploadcare.backgroundUrlSession")
config.isDiscretionary = false

// TODO: add a public settings for that
#if !os(macOS)
config.sessionSendsLaunchEvents = true
#endif

config.waitsForConnectivity = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
weak var sessionDelegate: URLSessionDelegate?
}

// MARK: - URLSessionTaskDelegate proxy
extension BackgroundSessionManager: URLSessionDataDelegate {
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
(sessionDelegate as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler)
}
}

// MARK: - URLSessionTaskDelegate proxy
extension BackgroundSessionManager: URLSessionTaskDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
(sessionDelegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didCompleteWithError: error)
}

public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
(sessionDelegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend)
}

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
(sessionDelegate as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: data)
}
}
24 changes: 7 additions & 17 deletions Sources/Uploadcare/UploadAPI.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// UploadAPI.swift
//
//
// Created by Sergey Armodin on 13.02.2020.
Expand Down Expand Up @@ -35,21 +35,6 @@ public class UploadAPI: NSObject {
/// Upload queue for multipart uploading
private var uploadQueue = DispatchQueue(label: "com.uploadcare.upload", qos: .utility, attributes: .concurrent)

/// URLSession for background uploads
private lazy var backgroundUrlSession: URLSession = {
let bundle = Bundle.main.bundleIdentifier ?? ""
let config = URLSessionConfiguration.background(withIdentifier: "\(bundle).com.uploadcare.backgroundUrlSession")
config.isDiscretionary = false

// TODO: add a public settings for that
#if !os(macOS)
config.sessionSendsLaunchEvents = true
#endif

config.waitsForConnectivity = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()

/// Running background tasks where key is URLSessionTask.taskIdentifier
private var backgroundTasks = [Int: BackgroundUploadTask]()

Expand All @@ -60,6 +45,10 @@ public class UploadAPI: NSObject {
self.publicKey = publicKey
self.secretKey = secretKey
self.manager = manager

super.init()

BackgroundSessionManager.instance.sessionDelegate = self
}
}

Expand Down Expand Up @@ -296,7 +285,7 @@ extension UploadAPI {
if let data = urlRequest.httpBody {
try? data.write(to: localURL)
}
let backgroundTask = backgroundUrlSession.uploadTask(with: urlRequest, fromFile: localURL)
let backgroundTask = BackgroundSessionManager.instance.session.uploadTask(with: urlRequest, fromFile: localURL)
backgroundTask.earliestBeginDate = Date()
backgroundTask.countOfBytesClientExpectsToSend = Int64(urlRequest.httpBody?.count ?? 0)

Expand Down Expand Up @@ -805,6 +794,7 @@ extension UploadAPI: URLSessionDataDelegate {
}
}

// MARK: - URLSessionTaskDelegate
extension UploadAPI: URLSessionTaskDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let backgroundTask = backgroundTasks[task.taskIdentifier] else { return }
Expand Down
2 changes: 1 addition & 1 deletion Uploadcare.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Uploadcare'
s.version = '0.3.0'
s.version = '0.3.1'
s.summary = 'Swift integration for Uploadcare'

# This description is used to generate tags and improve search results.
Expand Down
Loading

0 comments on commit 035fc3d

Please sign in to comment.