-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Cache.swift
827 lines (693 loc) · 31.4 KB
/
Cache.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//
// Cache.swift
// Aerial
//
// Created by Guillaume Louel on 06/06/2020.
// Copyright © 2020 Guillaume Louel. All rights reserved.
//
import Cocoa
import CoreWLAN
import AVKit
/**
Aerial's new Cache management
Everything Cache related is moved here.
- Note: Where is our cache ?
Starting with 2.0, Aerial is putting its files in two locations :
- `~/Library/Application Support/Aerial/` : Contains manifests files and strings bundles for each source, in their own directory
- `~/Library/Application Support/Aerial/Cache/` : Contains (only) the cached videos
Users of version 1.x.x will automatically see their video files migrated to the correct location.
In Catalina, those paths live inside a user's container :
`~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Application Support/`
- Attention: Shared by multiple users writable locations are no longer possible, because sandboxing is awesome !
*/
// swiftlint:disable:next type_body_length
struct Cache {
/**
Returns the SSID of the Wi-Fi network the user is currently connected to.
- Note: Returns an empty string if not connected to Wi-Fi
*/
static var ssid: String {
return CWWiFiClient.shared().interface(withName: nil)?.ssid() ?? ""
}
static var processedSupportPath = ""
/**
Returns Aerial's Application Support path.
+ On macOS 10.14 and earlier : `~/Library/Application Support/Aerial/`
+ Starting with 10.15 : `~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Application Support/Aerial/`
- Note: Returns `/` on failure.
In some rare instances those system folders may not exist in the container, in this case Aerial can't work.
*/
static var supportPath: String {
// Dont' redo the thing all the time
if processedSupportPath != "" {
return processedSupportPath
}
var appPath = ""
if PrefsCache.overrideCache {
debugLog("Cache Override")
if !Aerial.helper.underCompanion, #available(macOS 12, *) {
if let bookmarkData = PrefsCache.supportBookmarkData {
do {
var isStale = false
let bookmarkUrl = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
//debugLog("Bookmark is stale : \(isStale)")
appPath = bookmarkUrl.path
do {
let url = try NSURL.init(resolvingBookmarkData: bookmarkData, options: .withoutUI, relativeTo: nil, bookmarkDataIsStale: nil)
url.startAccessingSecurityScopedResource()
} catch let error as NSError {
errorLog("Bookmark Access Failed: \(error.description)")
}
} catch let error {
errorLog("Can't process bookmark \(error)")
}
} else {
errorLog("Can't find supportBookmarkData on macOS 12")
}
} else {
if let customPath = PrefsCache.supportPath {
debugLog("Trying \(customPath)")
if FileManager.default.fileExists(atPath: customPath) {
appPath = customPath
} else {
errorLog("Could not find your custom Caches path, reverting to default settings")
}
} else {
errorLog("Empty path, reverting to default settings")
}
}
}
// This is the normal(ish) path
if appPath == "" {
// This is the normal path via screensaver
if !Aerial.helper.underCompanion {
// Grab an array of Application Support paths
let appSupportPaths = NSSearchPathForDirectoriesInDomains(
.applicationSupportDirectory,
.userDomainMask,
true)
if appSupportPaths.isEmpty {
errorLog("FATAL : app support does not exist!")
return "/"
}
appPath = appSupportPaths[0]
} else {
// If we are underCompanion, we need to add the container on 10.15+
// Grab an array of Application Support paths
if #available(OSX 10.15, *) {
let libPaths = NSSearchPathForDirectoriesInDomains(
.libraryDirectory,
.userDomainMask,
true)
appPath = libPaths.first! + "/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Application Support/"
} else {
let appSupportPaths = NSSearchPathForDirectoriesInDomains(
.applicationSupportDirectory,
.userDomainMask,
true)
if appSupportPaths.isEmpty {
errorLog("FATAL : app support does not exist!")
return "/"
}
appPath = appSupportPaths[0]
}
}
}
let appSupportDirectory = appPath as NSString
if aerialFolderExists(at: appSupportDirectory) {
processedSupportPath = appSupportDirectory.appendingPathComponent("Aerial")
return processedSupportPath
} else {
debugLog("Creating app support directory...")
let asPath = appSupportDirectory.appendingPathComponent("Aerial")
let fileManager = FileManager.default
do {
try fileManager.createDirectory(atPath: asPath,
withIntermediateDirectories: true, attributes: nil)
processedSupportPath = asPath
return asPath
} catch let error {
errorLog("FATAL : Couldn't create app support directory in User directory: \(error)")
return "/"
}
}
}
/**
Returns Aerial's Caches path.
+ On macOS 10.14 and earlier : `~/Library/Application Support/Aerial/Cache/`
+ Starting with 10.15 : `~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Application Support/Aerial/Cache/`
- Note: Returns `/` on failure.
In some rare instances those system folders may not exist in the container, in this case Aerial can't work.
Also note that the shared `Caches` folder, `/Library/Caches/Aerial/`, is no longer user writable in Catalina and will be ignored.
*/
static var path: String = {
var path = ""
/*if PrefsCache.overrideCache {
if #available(macOS 12, *) {
if let bookmarkData = PrefsCache.cacheBookmarkData {
do {
var isStale = false
let bookmarkUrl = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
debugLog("Bookmark is stale : \(isStale)")
debugLog("\(bookmarkUrl)")
path = bookmarkUrl.path
debugLog("\(path)")
} catch {
errorLog("Can't process bookmark")
}
} else {
errorLog("Can't find cacheBookmarkData on macOS 12")
}
} else {
if let customPath = Preferences.sharedInstance.customCacheDirectory {
debugLog("Trying \(customPath)")
if FileManager.default.fileExists(atPath: customPath) {
path = customPath
} else {
errorLog("Could not find your custom Caches path, reverting to default settings")
}
} else {
errorLog("Empty path, reverting to default settings")
}
}
if path == "" {
PrefsCache.overrideCache = false
path = Cache.supportPath.appending("/Cache")
}
} else {*/
path = Cache.supportPath.appending("/Cache")
// }
if FileManager.default.fileExists(atPath: path as String) {
return path
} else {
do {
try FileManager.default.createDirectory(atPath: path,
withIntermediateDirectories: true, attributes: nil)
return path
} catch let error {
errorLog("FATAL : Couldn't create Cache directory in Aerial's AppSupport directory: \(error)")
return "/"
}
}
}()
static var pathUrl: URL = {
if #available(macOS 12, *) {
if PrefsCache.overrideCache {
if let bookmarkData = PrefsCache.cacheBookmarkData {
do {
var isStale = false
let bookmarkUrl = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
debugLog("Bookmark is stale : \(isStale)")
debugLog("\(bookmarkUrl)")
return bookmarkUrl
} catch {
errorLog("Can't process bookmark")
}
}
}
}
return URL(fileURLWithPath: path)
}()
/**
Returns Aerial's thumbnail cache path, creating it if needed.
+ On macOS 10.14 and earlier : `~/Library/Application Support/Aerial/Thumbnails/`
+ Starting with 10.15 : `~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Application Support/Aerial/Thumbnails/`
- Note: Returns `/` on failure.
*/
static var thumbnailsPath: String = {
let path = Cache.supportPath.appending("/Thumbnails")
if FileManager.default.fileExists(atPath: path as String) {
return path
} else {
do {
try FileManager.default.createDirectory(atPath: path,
withIntermediateDirectories: true, attributes: nil)
return path
} catch let error {
errorLog("FATAL : Couldn't create Thumbnails directory in Aerial's AppSupport directory: \(error)")
return "/"
}
}
}()
/**
Returns Aerial's former cache path, if it exists.
+ On macOS 10.14 and earlier : `~/Library/Caches/Aerial/`
+ Starting with 10.15 : `~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver/Data/Library/Caches/Aerial/`
- Note: Returns `nil` on failure.
*/
static private var formerCachePath: String? = {
// Grab an array of Cache paths
let cacheSupportPaths = NSSearchPathForDirectoriesInDomains(
.cachesDirectory,
.userDomainMask,
true)
if cacheSupportPaths.isEmpty {
errorLog("Couldn't find Caches paths!")
return nil
}
let cacheSupportDirectory = cacheSupportPaths[0] as NSString
if aerialFolderExists(at: cacheSupportDirectory) {
return cacheSupportDirectory.appendingPathComponent("Aerial")
} else {
do {
debugLog("trying to create \(cacheSupportDirectory.appendingPathComponent("Aerial"))")
try FileManager.default.createDirectory(atPath: cacheSupportDirectory.appendingPathComponent("Aerial"),
withIntermediateDirectories: true, attributes: nil)
return path
} catch {
errorLog("Could not create Aerial's Caches path")
}
return nil
}
}()
// MARK: - Migration from Aerial 1.x.x to 2.x.x
/**
Migrate files from previous versions of Aerial to the 2.x.x structure.
- Moves the video files from Application Support to the `Application Support/Aerial/Cache` sub directory.
- Moves the video files from Caches to the `Application Support/Aerial/Cache` sub directory
*/
static func migrate() {
if !PrefsCache.overrideCache {
migrateAppSupport()
migrateOldCache()
}
}
/**
Migrate video that may be at the root of /Application Support/Aerial/
*/
static private func migrateAppSupport() {
let supportURL = URL(fileURLWithPath: supportPath as String)
do {
let directoryContent = try FileManager.default.contentsOfDirectory(at: supportURL, includingPropertiesForKeys: nil)
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
if !videoURLs.isEmpty {
debugLog("Starting migration of your video files from Application Support to the /Cache subfolder")
for videoURL in videoURLs {
debugLog("moving \(videoURL.lastPathComponent)")
let newURL = URL(fileURLWithPath: path.appending("/\(videoURL.lastPathComponent)"))
try FileManager.default.moveItem(at: videoURL, to: newURL)
}
debugLog("Migration done.")
}
} catch {
errorLog("Error during migration, please report")
errorLog(error.localizedDescription)
}
}
/**
Migrate video that may be at the root of a user's /Caches/Aerial/
*/
static private func migrateOldCache() {
if let formerCachePath = formerCachePath {
do {
let formerCacheURL = URL(fileURLWithPath: formerCachePath as String)
let directoryContent = try FileManager.default.contentsOfDirectory(at: formerCacheURL, includingPropertiesForKeys: nil)
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
if !videoURLs.isEmpty {
debugLog("Starting migration of your video files from Caches to the /Cache subfolder of Application Support")
for videoURL in videoURLs {
debugLog("moving \(videoURL.lastPathComponent)")
let newURL = URL(fileURLWithPath: path.appending("/\(videoURL.lastPathComponent)"))
try FileManager.default.moveItem(at: videoURL, to: newURL)
}
debugLog("Migration done.")
}
} catch {
errorLog("Error during migration, please report")
errorLog(error.localizedDescription)
}
}
}
// Remove files in bad format or outdated
static func removeCruft() {
// TODO: kind of a temporary safety
if VideoList.instance.videos.count > 90 {
// First let's look at the cache
// let pathURL = URL(fileURLWithPath: path)
do {
guard pathUrl.startAccessingSecurityScopedResource() else {
errorLog("removeCruft couldn't access scoped resouce")
return
}
let directoryContent = try FileManager.default.contentsOfDirectory(at: pathUrl, includingPropertiesForKeys: nil)
debugLog("count : \(directoryContent.count)")
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
for video in videoURLs {
let filename = video.lastPathComponent
debugLog("\(filename)")
var found = false
// swiftlint:disable for_where
for candidate in VideoList.instance.videos {
if candidate.url.lastPathComponent == filename {
found = true
}
}
if !found {
debugLog("This file is not in the correct format or outdated, removing : \(video)")
try? FileManager.default.removeItem(at: video)
}
}
pathUrl.stopAccessingSecurityScopedResource()
} catch {
errorLog("Error during removing of videos in wrong format, please report")
errorLog(error.localizedDescription)
}
// Also remove uncached cruft
removeUncachedCruft()
}
}
static func removeUncachedCruft() {
for source in SourceList.foundSources where !source.isCachable && source.type != .local {
debugLog("Checking cruft in \(source.name)")
let pathURL = URL(fileURLWithPath: supportPath.appending("/" + source.name))
let unprocessed = source.getUnprocessedVideos()
debugLog(pathURL.absoluteString)
do {
let directoryContent = try FileManager.default.contentsOfDirectory(at: pathURL, includingPropertiesForKeys: nil)
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
for video in videoURLs {
let filename = video.lastPathComponent
var found = false
// swiftlint:disable for_where
for candidate in unprocessed {
if candidate.url.lastPathComponent == filename {
found = true
}
}
if !found {
debugLog("This file is not in the correct format or outdated, removing : \(video)")
try? FileManager.default.removeItem(at: video)
}
}
} catch {
errorLog("Error during removal of videos in wrong format, please report")
errorLog(error.localizedDescription)
}
}
}
/// This clears the whole cache. User beware !
static func clearCache() {
let pathURL = URL(fileURLWithPath: path)
do {
let directoryContent = try FileManager.default.contentsOfDirectory(at: pathURL, includingPropertiesForKeys: nil)
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
for video in videoURLs {
try? FileManager.default.removeItem(at: video)
}
} catch {
errorLog("Error during removal of videos in wrong format, please report")
errorLog(error.localizedDescription)
}
}
static func clearNonCacheableSources() {
// Then we need to look at individual online sources
// let onlineVideos = VideoList.instance.videos.filter({ !$0.source.isCachable })
for source in SourceList.foundSources.filter({!$0.isCachable}) {
let pathSource = URL(fileURLWithPath: supportPath).appendingPathComponent(source.name)
if FileManager.default.fileExists(atPath: pathSource.path) {
do {
let directoryContent = try FileManager.default.contentsOfDirectory(at: pathSource, includingPropertiesForKeys: nil)
let videoURLs = directoryContent.filter { $0.pathExtension == "mov" }
for video in videoURLs {
debugLog("Removing file : \(video)")
try? FileManager.default.removeItem(at: video)
}
} catch {
errorLog("Error during removing of videos in wrong format, please report")
errorLog(error.localizedDescription)
}
}
}
}
// MARK: - About the cache
/**
Is our cache full ?
*/
static func isFull() -> Bool {
return size() > PrefsCache.cacheLimit
}
/**
Do we still have a bit of free space (0.5 GB)
*/
static func hasSomeFreeSpace() -> Bool {
return size() < PrefsCache.cacheLimit - 0.5
}
/**
Returns the cache size in GB as a string (eg. 5.1 GB)
*/
static func sizeString() -> String {
let pathURL = Foundation.URL(fileURLWithPath: path)
// check if the url is a directory
if (try? pathURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
var folderSize = 0
(FileManager.default.enumerator(at: pathURL, includingPropertiesForKeys: nil)?.allObjects as? [URL])?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = .useGB
byteCountFormatter.countStyle = .file
let sizeToDisplay = byteCountFormatter.string(for: folderSize) ?? ""
return sizeToDisplay
}
// In case it fails somehow
return "No cache found"
}
// MARK: - Helpers
/**
Does an `/Aerial/` subfolder exist inside the given path
- parameter at: Source path
- returns: Path existance as a Bool.
*/
private static func aerialFolderExists(at: NSString) -> Bool {
let aerialFolder = at.appendingPathComponent("Aerial")
return FileManager.default.fileExists(atPath: aerialFolder as String)
}
/**
Returns cache size in GB
*/
static func size() -> Double {
let pathURL = URL(fileURLWithPath: path)
// check if the url is a directory
if (try? pathURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
var folderSize = 0
(FileManager.default.enumerator(at: pathURL, includingPropertiesForKeys: nil)?.allObjects as? [URL])?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
return Double(folderSize) / 1000000000
}
return 0
}
static func getDirectorySize(directory: String) -> Double {
if FileManager.default.fileExists(atPath: directory) {
let pathURL = URL(fileURLWithPath: directory)
// check if the url is a directory
if (try? pathURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
var folderSize = 0
(FileManager.default.enumerator(at: pathURL, includingPropertiesForKeys: nil)?.allObjects as? [URL])?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
return Double(folderSize) / 1000000000
}
return 0
} else {
return 0
}
}
static func packsSize() -> Double {
var totalSize: Double = 0
for source in SourceList.foundSources where !source.isCachable {
let sourcePath = supportPath.appending("/" + source.name)
totalSize += getDirectorySize(directory: sourcePath)
}
return totalSize
}
// swiftlint:disable line_length
// MARK: Networking restrictions for cache
/**
Can we download a file ?
Depending on user's settings, the cache may be full or the user may not be on a trusted network.
- Note: If a user disabled cache management (full manual mode), this will always be true.
- parameter action: A closure with the action to be accomplished should the conditions be met.
*/
static func ensureDownload(action: @escaping () -> Void) {
// Do we manage the cache or not ?
if PrefsCache.enableManagement {
// Check network first
if !canNetwork() {
if !Aerial.helper.showAlert(question: "You are on a restricted WiFi network",
text: "Your current settings restrict downloads when not connected to a trusted network. Do you wish to proceed?\n\nReminder: You can change this setting in the Cache tab.",
button1: "Download Anyway",
button2: "Cancel") {
return
}
}
// Then cache status
if isFull() {
let formatter = NumberFormatter()
formatter.locale = Locale.current // USA: Locale(identifier: "en_US")
formatter.numberStyle = .decimal
let result = formatter.string(from: NSNumber(value: PrefsCache.cacheLimit.rounded(toPlaces: 1)))!
//print(result) // -> US$9,999.99
if !Aerial.helper.showAlert(question: "Your cache is full",
text: "Your cache limit is currently set to \(result) GB, and currently contains \(Cache.sizeString()) of files.\n\n Do you want to proceed with the download anyway?\n\nYou can manually increase or decrease your cache size in Settings > Cache.",
button1: "Download Anyway",
button2: "Cancel") {
return
}
}
}
// If all is fine then proceed
action()
}
/**
Can we safely use network ?
Depending on user's settings, they may not be on a trusted network.
- Note: If a user disabled cache management (full manual mode), this will always be true.
*/
static func canNetwork() -> Bool {
if !PrefsCache.enableManagement {
return true
}
if PrefsCache.restrictOnWiFi {
// If we are not connected to WiFi we allow
if Cache.ssid == "" || PrefsCache.allowedNetworks.contains(ssid) {
return true
} else {
return false
}
} else {
return true
}
}
static func outdatedVideos() -> [AerialVideo] {
guard PrefsCache.enableManagement else {
return []
}
var cutoffDate = Date()
switch PrefsCache.cachePeriodicity {
case .daily:
cutoffDate = Calendar.current.date(byAdding: .day, value: -1, to: cutoffDate)!
case .weekly:
cutoffDate = Calendar.current.date(byAdding: .day, value: -7, to: cutoffDate)!
case .monthly:
cutoffDate = Calendar.current.date(byAdding: .month, value: -1, to: cutoffDate)!
case .never:
return []
}
// Get a list of cached videos that are not favorites, and are from a cacheable source (not a pack)
// Yes this is getting a bit complicated
var evictable: [Date: AerialVideo] = [:]
let currentlyCached = VideoList.instance.videos.filter({ $0.isAvailableOffline && $0.source.isCachable && !PrefsVideos.favorites.contains($0.id)})
for video in currentlyCached {
let path = VideoCache.cachePath(forVideo: video)!
// swiftlint:disable:next force_try
let attributes = try! FileManager.default.attributesOfItem(atPath: path)
let creationDate = attributes[.creationDate] as! Date
if creationDate < cutoffDate {
evictable[creationDate] = video
}
}
return evictable.sorted { $0.key < $1.key }.map({ $0.value })
}
// swiftlint:disable:next cyclomatic_complexity
static func freeCache() {
guard PrefsCache.enableManagement else {
return
}
// Step 1 : Delete hidden videos
debugLog("Looking for hidden videos to delete...")
for video in VideoList.instance.videos.filter({ PrefsVideos.hidden.contains($0.id) && $0.isAvailableOffline }) {
debugLog("Deleting hidden video \(video.secondaryName)")
do {
let path = VideoList.instance.localPathFor(video: video)
try FileManager.default.removeItem(atPath: path)
} catch {
errorLog("Could not delete video : \(video.secondaryName)")
}
}
// We may be good ?
if hasSomeFreeSpace() {
return
}
// Step 2 : Delete videos that are out of rotation
let evictables = outdatedVideos()
if evictables.isEmpty {
debugLog("No outdated videos, we won't delete anything")
return
}
debugLog("Looking for outdated videos that aren't in rotation (candidates : \(evictables.count)")
outerLoop: for video in evictables {
if VideoList.instance.currentRotation().contains(video) {
// Outdated but in rotation, so keep it !
// debugLog("outdated but in rotation \(video.secondaryName)")
} else {
debugLog("Removing outdated video not in rotation \(video.secondaryName)")
do {
try FileManager.default.removeItem(atPath: VideoCache.cachePath(forVideo: video)!)
} catch {
errorLog("Could not delete video : \(video.secondaryName)")
}
if hasSomeFreeSpace() {
// Removed enough
break outerLoop
}
}
}
// Are we there yeeeet ?
if hasSomeFreeSpace() {
return
}
debugLog("Looking for outdated videos that may still be in rotation (candidates : \(evictables.count)")
var currentVideos = [AerialVideo]()
for view in AerialView.instanciatedViews {
if let video = view.currentVideo {
currentVideos.append(video)
}
}
outerLoop2: for video in evictables {
if currentVideos.contains(video) {
debugLog("\(video.secondaryName) is currently playing, trying another")
} else {
debugLog("Removing outdated video that was in rotation \(video.secondaryName)")
do {
try FileManager.default.removeItem(atPath: VideoCache.cachePath(forVideo: video)!)
} catch {
errorLog("Could not delete video : \(video.secondaryName)")
}
if hasSomeFreeSpace() {
// Removed enough
break outerLoop2
}
}
}
// At this point we can't do more
}
static func fillOrRollCache() {
guard PrefsCache.enableManagement && canNetwork() else {
return
}
// Grab a *shuffled* list of uncached in rotation videos
let rotation = VideoList.instance.currentRotation().filter { !$0.isAvailableOffline }.shuffled()
if rotation.isEmpty {
debugLog("> Current playlist is already fully cached, no download/rotation needed")
return
}
debugLog("> Fill or roll cache")
// Do we have some space to download at least a video (by default .5 GB) ?
if !hasSomeFreeSpace() {
freeCache()
if !hasSomeFreeSpace() {
debugLog("No free space to reclaim currently.")
return
}
}
debugLog("Uncached videos in rotation : \(rotation.count)")
// We may be satisfied already
if rotation.isEmpty {
return
}
// Queue the first video on the list
debugLog("Queuing video : \(rotation.first!.secondaryName)")
VideoManager.sharedInstance.queueDownload(rotation.first!)
}
}