Skip to content
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

Merged

Conversation

shafqat-muneer
Copy link
Contributor

@shafqat-muneer shafqat-muneer commented Jun 10, 2024

LEARNER-9966: Delete old downloaded videos data on device to optimize storage

We decided not to migrate downloaded content but have to look into deleting the old downloaded videos so the app does not create useless dummy data in the learners' devices.

Technical Details:
The old application saves users' downloaded videos in the Application Support directory within folders named users' MD5 hashes. When multiple users log into the same device, separate MD5 hashed folders are created for each user, each containing that user's downloaded data.

After upgrading the application, we can use the MD5 hash algorithm to delete only the data of the currently logged-in user, while the data for other users will remain intact. Because MD5 hash folder name will be generated for current logged in username. To prevent this scenario, we need to create a regex that identifies and deletes all MD5 hashed folders upon launching the upgraded application.

We can consider removing this code once we are confident that most or all users have transitioned to the new application.

Before Update Size After Update Size
Before Update Size After Update Size
Before Update After Update
Before.Update.mov
After.Update.mov

@@ -124,6 +124,8 @@ public protocol DownloadManagerProtocol {
func resumeDownloading() throws
func fileUrl(for blockId: String) -> URL?
func isLargeVideosSize(blocks: [CourseBlock]) -> Bool

func removeAppSupportDirectoryDeprecatedContent()
Copy link
Contributor

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?

@@ -470,6 +472,60 @@ public class DownloadManager: DownloadManagerProtocol {
debugLog("SaveFile Error", error.localizedDescription)
}
}

public func removeAppSupportDirectoryDeprecatedContent() {
deleteMD5HashedFolders()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doing the work directly here that you are doing in deleteMD5HashedFolders function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For our specific scenario, the suggested name removeAppSupportDirectoryUnusedContent seems fitting. However, the name deleteMD5HashedFolders explicitly describes the deletion of MD5 hashed folders (not any other content). If we integrate the implementation of deleteMD5HashedFolders() directly into removeAppSupportDirectoryUnusedContent(), it might affect the clarity of the abstraction level. Do you have any suggestions or thoughts on finding a more suitable name?

)
return appSupportDirectory
} catch {
print("Error getting Application Support Directory: \(error)")
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Comment on lines 111 to 120
private func resetAppSupportDirectoryUserData() {
if var upgradationValue = Container.shared.resolve(CoreStorage.self) {
if upgradationValue.resetAppSupportDirectoryUserData == false {
Task {
Container.shared.resolve(DownloadManagerProtocol.self)?.removeAppSupportDirectoryDeprecatedContent()
upgradationValue.resetAppSupportDirectoryUserData = true
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about updating this method to guard like below thoughts?

private func resetAppSupportDirectoryUserData() {
        guard var upgradationValue = Container.shared.resolve(CoreStorage.self),
              let downloadManager = Container.shared.resolve(DownloadManagerProtocol.self),
              upgradationValue.resetAppSupportDirectoryUserData == false
        else {return}
        
        Task {
            downloadManager.removeAppSupportDirectoryDeprecatedContent()
            upgradationValue.resetAppSupportDirectoryUserData = true
        }
    }

rnr
rnr previously approved these changes Jun 11, 2024
Copy link
Contributor

@rnr rnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me (just check Saeed's comments)

}

private func isMD5Hash(_ folderName: String) -> Bool {
let md5Regex = "^[a-fA-F0-9]{32}$"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can md5 contain uppercase letters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@shafqat-muneer
Copy link
Contributor Author

shafqat-muneer commented Jun 11, 2024

@saeedbashir @rnr Addressed suggestions. Ready for another round of review 🎉

Copy link
Contributor

@saeedbashir saeedbashir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@shafqat-muneer
Copy link
Contributor Author

@volodymyr-chekyrta Awaiting your review. Thanks

@volodymyr-chekyrta volodymyr-chekyrta merged commit 3ab245b into openedx:develop Jun 12, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants