-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Delete old downloaded videos data on device to optimize storage #452
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,8 @@ public protocol DownloadManagerProtocol { | |
func resumeDownloading() throws | ||
func fileUrl(for blockId: String) -> URL? | ||
func isLargeVideosSize(blocks: [CourseBlock]) -> Bool | ||
|
||
func removeAppSupportDirectoryDeprecatedContent() | ||
} | ||
|
||
public enum DownloadManagerEvent { | ||
|
@@ -470,6 +472,60 @@ public class DownloadManager: DownloadManagerProtocol { | |
debugLog("SaveFile Error", error.localizedDescription) | ||
} | ||
} | ||
|
||
public func removeAppSupportDirectoryDeprecatedContent() { | ||
deleteMD5HashedFolders() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about doing the work directly here that you are doing in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For our specific scenario, the suggested name |
||
} | ||
|
||
private func getApplicationSupportDirectory() -> URL? { | ||
let fileManager = FileManager.default | ||
do { | ||
let appSupportDirectory = try fileManager.url( | ||
for: .applicationSupportDirectory, | ||
in: .userDomainMask, | ||
appropriateFor: nil, | ||
create: true | ||
) | ||
return appSupportDirectory | ||
} catch { | ||
print("Error getting Application Support Directory: \(error)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use debugPrint here and in other places where applicable in code that you wrote. |
||
return nil | ||
} | ||
} | ||
|
||
private func isMD5Hash(_ folderName: String) -> Bool { | ||
let md5Regex = "^[a-fA-F0-9]{32}$" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can md5 contain uppercase letters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, an MD5 hash will not contain uppercase letters. However, to be safe, if there is any customization that introduces uppercase letters, it will also delete that folder. |
||
let predicate = NSPredicate(format: "SELF MATCHES %@", md5Regex) | ||
return predicate.evaluate(with: folderName) | ||
} | ||
|
||
private func deleteMD5HashedFolders() { | ||
guard let appSupportDirectory = getApplicationSupportDirectory() else { | ||
return | ||
} | ||
|
||
let fileManager = FileManager.default | ||
do { | ||
let folderContents = try fileManager.contentsOfDirectory( | ||
at: appSupportDirectory, | ||
includingPropertiesForKeys: nil, | ||
options: [] | ||
) | ||
for folderURL in folderContents { | ||
let folderName = folderURL.lastPathComponent | ||
if isMD5Hash(folderName) { | ||
do { | ||
try fileManager.removeItem(at: folderURL) | ||
print("Deleted folder: \(folderName)") | ||
} catch { | ||
print("Error deleting folder \(folderName): \(error)") | ||
} | ||
} | ||
} | ||
} catch { | ||
print("Error reading contents of Application Support directory: \(error)") | ||
} | ||
} | ||
} | ||
|
||
@available(iOSApplicationExtension, unavailable) | ||
|
@@ -638,6 +694,9 @@ public class DownloadManagerMock: DownloadManagerProtocol { | |
false | ||
} | ||
|
||
public func removeAppSupportDirectoryDeprecatedContent() { | ||
|
||
} | ||
} | ||
#endif | ||
// swiftlint:enable file_length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about renaming it to
removeAppSupportDirectoryUnusedContent
thoughts?