Skip to content

Commit 8b28433

Browse files
authored
Merge pull request #30 from sukov/version-2.0.2
Version 2.0.2
2 parents bbb647b + a1fc6ef commit 8b28433

25 files changed

+118
-36
lines changed

CachingPlayerItem.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'CachingPlayerItem'
3-
s.version = '2.0.1'
3+
s.version = '2.0.2'
44
s.summary = 'Cache & Play audio and video files'
55

66
s.homepage = 'https://github.com/sukov/CachingPlayerItem'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Once you have your Swift package set up, adding `CachingPlayerItem` as a depende
5555

5656
```swift
5757
dependencies: [
58-
.package(url: "https://github.com/sukov/CachingPlayerItem.git", from: "1.0.5")
58+
.package(url: "https://github.com/sukov/CachingPlayerItem.git", from: "2.0.2")
5959
]
6060
```
6161

Source/CachingPlayerItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public final class CachingPlayerItem: AVPlayerItem {
217217
guard initialScheme != nil else { return }
218218

219219
// Otherwise the ResourceLoaderDelegate wont deallocate and will keep downloading.
220-
cancelDownload()
220+
resourceLoaderDelegate.invalidateAndCancelSession(shouldResetData: false)
221221
}
222222

223223
// MARK: Public methods

Source/CachingPlayerItemConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public enum CachingPlayerItemConfiguration {
2121
/// If set greater than 0, the set value with be compared with the downloaded media size. If the size of the downloaded media is lower, an error will be thrown. Useful when `expectedContentLength` is unavailable.
2222
/// Default value is `0`.
2323
public static var minimumExpectedFileSize: Int = 0
24+
25+
/// Flag for deciding whether an `NSFileWriteOutOfSpaceError` should be thrown when there is not enough available disk space left for caching the entire media file. Defaults to `true`.
26+
public static var shouldCheckAvailableDiskSpaceBeforeCaching: Bool = true
2427
}
2528

2629
fileprivate extension Int {

Source/MediaFileHandle.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ final class MediaFileHandle {
3737
// MARK: Internal methods
3838

3939
extension MediaFileHandle {
40+
var freeDiskSpace: Int64? {
41+
let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String)
42+
let freeSpace = (systemAttributes?[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value
43+
return freeSpace
44+
}
45+
4046
var attributes: [FileAttributeKey : Any]? {
4147
do {
4248
return try FileManager.default.attributesOfItem(atPath: filePath)

Source/ResourceLoaderDelegate.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ final class ResourceLoaderDelegate: NSObject, AVAssetResourceLoaderDelegate, URL
113113
guard let self else { return }
114114

115115
let taskId = task.taskIdentifier
116+
116117
if let error {
117118
guard (error as? URLError)?.code != .cancelled else { return }
118119

@@ -129,6 +130,12 @@ final class ResourceLoaderDelegate: NSObject, AVAssetResourceLoaderDelegate, URL
129130
}
130131

131132
if let response = task.response, pendingContentInfoRequest?.id == taskId {
133+
let insufficientDiskSpaceError = checkAvailableDiskSpaceIfNeeded(response: response)
134+
guard insufficientDiskSpaceError == nil else {
135+
downloadFailed(with: insufficientDiskSpaceError!)
136+
return
137+
}
138+
132139
pendingContentInfoRequest?.fillInContentInformationRequest(with: response)
133140
finishLoadingPendingRequest(withId: taskId)
134141
contentInfoResponse = response
@@ -248,6 +255,20 @@ final class ResourceLoaderDelegate: NSObject, AVAssetResourceLoaderDelegate, URL
248255
return error
249256
}
250257

258+
private func checkAvailableDiskSpaceIfNeeded(response: URLResponse) -> NSError? {
259+
guard
260+
CachingPlayerItemConfiguration.shouldCheckAvailableDiskSpaceBeforeCaching,
261+
let response = response as? HTTPURLResponse,
262+
let freeDiskSpace = fileHandle.freeDiskSpace
263+
else { return nil }
264+
265+
if freeDiskSpace < response.processedInfoData.expectedContentLength {
266+
return NSError(domain: "Failed downloading asset. Reason: insufficient disk space available.", code: NSFileWriteOutOfSpaceError, userInfo: nil)
267+
}
268+
269+
return nil
270+
}
271+
251272
private func downloadFailed(with error: Error) {
252273
invalidateAndCancelSession()
253274

docs/Classes.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -133,7 +133,7 @@ <h4>Declaration</h4>
133133
</article>
134134
</div>
135135
<section class="footer">
136-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
136+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
137137
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
138138
</section>
139139
</body>

docs/Classes/CachingPlayerItem.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -798,7 +798,7 @@ <h4>Declaration</h4>
798798
</article>
799799
</div>
800800
<section class="footer">
801-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
801+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
802802
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
803803
</section>
804804
</body>

docs/Enums.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -132,7 +132,7 @@ <h4>Declaration</h4>
132132
</article>
133133
</div>
134134
<section class="footer">
135-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
135+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
136136
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
137137
</section>
138138
</body>

docs/Enums/CachingPlayerItemConfiguration.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -212,6 +212,33 @@ <h4>Declaration</h4>
212212
</section>
213213
</div>
214214
</li>
215+
<li class="item">
216+
<div>
217+
<code>
218+
<a name="/s:17CachingPlayerItem0abC13ConfigurationO035shouldCheckAvailableDiskSpaceBeforeA0SbvpZ"></a>
219+
<a name="//apple_ref/swift/Variable/shouldCheckAvailableDiskSpaceBeforeCaching" class="dashAnchor"></a>
220+
<a class="token" href="#/s:17CachingPlayerItem0abC13ConfigurationO035shouldCheckAvailableDiskSpaceBeforeA0SbvpZ">shouldCheckAvailableDiskSpaceBeforeCaching</a>
221+
</code>
222+
</div>
223+
<div class="height-container">
224+
<div class="pointer-container"></div>
225+
<section class="section">
226+
<div class="pointer"></div>
227+
<div class="abstract">
228+
<p>Flag for deciding whether an <code>NSFileWriteOutOfSpaceError</code> should be thrown when there is not enough available disk space left for caching the entire media file. Defaults to <code>true</code>.</p>
229+
230+
</div>
231+
<div class="declaration">
232+
<h4>Declaration</h4>
233+
<div class="language">
234+
<p class="aside-title">Swift</p>
235+
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">static</span> <span class="k">var</span> <span class="nv">shouldCheckAvailableDiskSpaceBeforeCaching</span><span class="p">:</span> <span class="kt">Bool</span></code></pre>
236+
237+
</div>
238+
</div>
239+
</section>
240+
</div>
241+
</li>
215242
</ul>
216243
</div>
217244
</div>
@@ -220,7 +247,7 @@ <h4>Declaration</h4>
220247
</article>
221248
</div>
222249
<section class="footer">
223-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
250+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
224251
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
225252
</section>
226253
</body>

docs/Protocols.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -133,7 +133,7 @@ <h4>Declaration</h4>
133133
</article>
134134
</div>
135135
<section class="footer">
136-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
136+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
137137
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
138138
</section>
139139
</body>

docs/Protocols/CachingPlayerItemDelegate.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -302,7 +302,7 @@ <h4>Declaration</h4>
302302
</article>
303303
</div>
304304
<section class="footer">
305-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
305+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
306306
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
307307
</section>
308308
</body>

docs/docsets/CachingPlayerItem.docset/Contents/Resources/Documents/Classes.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -133,7 +133,7 @@ <h4>Declaration</h4>
133133
</article>
134134
</div>
135135
<section class="footer">
136-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
136+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
137137
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
138138
</section>
139139
</body>

docs/docsets/CachingPlayerItem.docset/Contents/Resources/Documents/Classes/CachingPlayerItem.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -798,7 +798,7 @@ <h4>Declaration</h4>
798798
</article>
799799
</div>
800800
<section class="footer">
801-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
801+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
802802
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
803803
</section>
804804
</body>

docs/docsets/CachingPlayerItem.docset/Contents/Resources/Documents/Enums.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -132,7 +132,7 @@ <h4>Declaration</h4>
132132
</article>
133133
</div>
134134
<section class="footer">
135-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
135+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
136136
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
137137
</section>
138138
</body>

docs/docsets/CachingPlayerItem.docset/Contents/Resources/Documents/Enums/CachingPlayerItemConfiguration.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -212,6 +212,33 @@ <h4>Declaration</h4>
212212
</section>
213213
</div>
214214
</li>
215+
<li class="item">
216+
<div>
217+
<code>
218+
<a name="/s:17CachingPlayerItem0abC13ConfigurationO035shouldCheckAvailableDiskSpaceBeforeA0SbvpZ"></a>
219+
<a name="//apple_ref/swift/Variable/shouldCheckAvailableDiskSpaceBeforeCaching" class="dashAnchor"></a>
220+
<a class="token" href="#/s:17CachingPlayerItem0abC13ConfigurationO035shouldCheckAvailableDiskSpaceBeforeA0SbvpZ">shouldCheckAvailableDiskSpaceBeforeCaching</a>
221+
</code>
222+
</div>
223+
<div class="height-container">
224+
<div class="pointer-container"></div>
225+
<section class="section">
226+
<div class="pointer"></div>
227+
<div class="abstract">
228+
<p>Flag for deciding whether an <code>NSFileWriteOutOfSpaceError</code> should be thrown when there is not enough available disk space left for caching the entire media file. Defaults to <code>true</code>.</p>
229+
230+
</div>
231+
<div class="declaration">
232+
<h4>Declaration</h4>
233+
<div class="language">
234+
<p class="aside-title">Swift</p>
235+
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">static</span> <span class="k">var</span> <span class="nv">shouldCheckAvailableDiskSpaceBeforeCaching</span><span class="p">:</span> <span class="kt">Bool</span></code></pre>
236+
237+
</div>
238+
</div>
239+
</section>
240+
</div>
241+
</li>
215242
</ul>
216243
</div>
217244
</div>
@@ -220,7 +247,7 @@ <h4>Declaration</h4>
220247
</article>
221248
</div>
222249
<section class="footer">
223-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
250+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
224251
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
225252
</section>
226253
</body>

docs/docsets/CachingPlayerItem.docset/Contents/Resources/Documents/Protocols.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="index.html">
24-
CachingPlayerItem 2.0.0 Docs
24+
CachingPlayerItem 2.0.2 Docs
2525
</a>
2626
(92% documented)
2727
</p>
@@ -133,7 +133,7 @@ <h4>Declaration</h4>
133133
</article>
134134
</div>
135135
<section class="footer">
136-
<p>&copy; 2024 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2024-11-28)</p>
136+
<p>&copy; 2025 <a class="link" href="https://github.com/sukov/CachingPlayerItem" target="_blank" rel="external noopener">Gorjan Shukov</a>. All rights reserved. (Last updated: 2025-03-12)</p>
137137
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
138138
</section>
139139
</body>

0 commit comments

Comments
 (0)