@capgo/capacitor-downloader 8.1.29 → 8.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,7 +23,7 @@ import java.util.Map;
23
23
  @CapacitorPlugin(name = "CapacitorDownloader")
24
24
  public class CapacitorDownloaderPlugin extends Plugin {
25
25
 
26
- private final String pluginVersion = "8.1.29";
26
+ private final String pluginVersion = "8.1.31";
27
27
 
28
28
  private DownloadManager downloadManager;
29
29
  private final Map<String, Long> downloads = new HashMap<>();
@@ -8,7 +8,7 @@ import Capacitor
8
8
 
9
9
  @objc(CapacitorDownloaderPlugin)
10
10
  public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
11
- private let pluginVersion: String = "8.1.29"
11
+ private let pluginVersion: String = "8.1.31"
12
12
  public let identifier = "CapacitorDownloaderPlugin"
13
13
  public let jsName = "CapacitorDownloader"
14
14
  public let pluginMethods: [CAPPluginMethod] = [
@@ -22,12 +22,40 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
22
22
  ]
23
23
 
24
24
  private var tasks: [String: URLSessionDownloadTask] = [:]
25
+ private let tasksLock = NSLock()
25
26
  private lazy var session: URLSession = {
26
27
  let config = URLSessionConfiguration.background(withIdentifier: "CapacitorDownloader")
27
28
  config.sessionSendsLaunchEvents = true
28
29
  return URLSession(configuration: config, delegate: self, delegateQueue: nil)
29
30
  }()
30
31
 
32
+ private func setTask(_ task: URLSessionDownloadTask, for id: String) {
33
+ tasksLock.lock()
34
+ defer { tasksLock.unlock() }
35
+ tasks[id] = task
36
+ }
37
+
38
+ private func getTask(for id: String) -> URLSessionDownloadTask? {
39
+ tasksLock.lock()
40
+ defer { tasksLock.unlock() }
41
+ return tasks[id]
42
+ }
43
+
44
+ private func removeTask(for id: String) {
45
+ tasksLock.lock()
46
+ defer { tasksLock.unlock() }
47
+ tasks.removeValue(forKey: id)
48
+ }
49
+
50
+ private func idForTask(_ task: URLSessionDownloadTask) -> String? {
51
+ if let id = task.taskDescription, !id.isEmpty {
52
+ return id
53
+ }
54
+ tasksLock.lock()
55
+ defer { tasksLock.unlock() }
56
+ return tasks.first(where: { $0.value == task })?.key
57
+ }
58
+
31
59
  @objc func download(_ call: CAPPluginCall) {
32
60
  guard let id = call.getString("id"),
33
61
  let urlString = call.getString("url"),
@@ -45,7 +73,8 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
45
73
  }
46
74
 
47
75
  let task = session.downloadTask(with: request)
48
- tasks[id] = task
76
+ task.taskDescription = id
77
+ setTask(task, for: id)
49
78
  task.resume()
50
79
 
51
80
  call.resolve([
@@ -57,7 +86,7 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
57
86
 
58
87
  @objc func pause(_ call: CAPPluginCall) {
59
88
  guard let id = call.getString("id"),
60
- let task = tasks[id] else {
89
+ let task = getTask(for: id) else {
61
90
  call.reject("Task not found")
62
91
  return
63
92
  }
@@ -68,7 +97,7 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
68
97
 
69
98
  @objc func resume(_ call: CAPPluginCall) {
70
99
  guard let id = call.getString("id"),
71
- let task = tasks[id] else {
100
+ let task = getTask(for: id) else {
72
101
  call.reject("Task not found")
73
102
  return
74
103
  }
@@ -79,19 +108,19 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
79
108
 
80
109
  @objc func stop(_ call: CAPPluginCall) {
81
110
  guard let id = call.getString("id"),
82
- let task = tasks[id] else {
111
+ let task = getTask(for: id) else {
83
112
  call.reject("Task not found")
84
113
  return
85
114
  }
86
115
 
87
116
  task.cancel()
88
- tasks.removeValue(forKey: id)
117
+ removeTask(for: id)
89
118
  call.resolve()
90
119
  }
91
120
 
92
121
  @objc func checkStatus(_ call: CAPPluginCall) {
93
122
  guard let id = call.getString("id"),
94
- let task = tasks[id] else {
123
+ let task = getTask(for: id) else {
95
124
  call.reject("Task not found")
96
125
  return
97
126
  }
@@ -141,7 +170,7 @@ public class CapacitorDownloaderPlugin: CAPPlugin, CAPBridgedPlugin {
141
170
 
142
171
  extension CapacitorDownloaderPlugin: URLSessionDownloadDelegate {
143
172
  public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
144
- guard let id = tasks.first(where: { $0.value == downloadTask })?.key,
173
+ guard let id = idForTask(downloadTask),
145
174
  let destinationURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(id) else {
146
175
  return
147
176
  }
@@ -156,11 +185,11 @@ extension CapacitorDownloaderPlugin: URLSessionDownloadDelegate {
156
185
 
157
186
  public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
158
187
  guard let downloadTask = task as? URLSessionDownloadTask,
159
- let id = tasks.first(where: { $0.value == downloadTask })?.key else {
188
+ let id = idForTask(downloadTask) else {
160
189
  return
161
190
  }
162
191
 
163
- tasks.removeValue(forKey: id)
192
+ removeTask(for: id)
164
193
 
165
194
  if let error = error {
166
195
  notifyListeners("downloadFailed", data: ["id": id, "error": error.localizedDescription])
@@ -168,7 +197,7 @@ extension CapacitorDownloaderPlugin: URLSessionDownloadDelegate {
168
197
  }
169
198
 
170
199
  public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
171
- guard let id = tasks.first(where: { $0.value == downloadTask })?.key else {
200
+ guard let id = idForTask(downloadTask) else {
172
201
  return
173
202
  }
174
203
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-downloader",
3
- "version": "8.1.29",
3
+ "version": "8.1.31",
4
4
  "description": "Download file in background or foreground",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -52,7 +52,8 @@
52
52
  "prepublishOnly": "bun run build",
53
53
  "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs",
54
54
  "example:install": "cd example-app && bun install --frozen-lockfile",
55
- "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build"
55
+ "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build",
56
+ "changelog:ai": "node scripts/generate-ai-changelog.mjs"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@capacitor/android": "^8.0.0",