Skip to content

Commit

Permalink
feat: Delete old downloaded videos data on device to optimize storage (
Browse files Browse the repository at this point in the history
…openedx#452)

* feat: Delete old downloaded videos data on device to optimize storage

* chore: resolved failed test cases

* chore: address review feedback
  • Loading branch information
shafqat-muneer authored Jun 12, 2024
1 parent c39b087 commit 3ab245b
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Authorization/AuthorizationTests/AuthorizationMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,12 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
return __value
}

open func removeAppSupportDirectoryUnusedContent() {
addInvocation(.m_removeAppSupportDirectoryUnusedContent)
let perform = methodPerformValue(.m_removeAppSupportDirectoryUnusedContent) as? () -> Void
perform?()
}


fileprivate enum MethodType {
case m_publisher
Expand All @@ -2421,6 +2427,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case m_fileUrl__for_blockId(Parameter<String>)
case m_resumeDownloading
case m_isLargeVideosSize__blocks_blocks(Parameter<[CourseBlock]>)
case m_removeAppSupportDirectoryUnusedContent
case p_currentDownloadTask_get

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
Expand Down Expand Up @@ -2477,6 +2484,8 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsBlocks, rhs: rhsBlocks, with: matcher), lhsBlocks, rhsBlocks, "blocks"))
return Matcher.ComparisonResult(results)

case (.m_removeAppSupportDirectoryUnusedContent, .m_removeAppSupportDirectoryUnusedContent): return .match
case (.p_currentDownloadTask_get,.p_currentDownloadTask_get): return Matcher.ComparisonResult.match
default: return .none
}
Expand All @@ -2498,6 +2507,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case let .m_fileUrl__for_blockId(p0): return p0.intValue
case .m_resumeDownloading: return 0
case let .m_isLargeVideosSize__blocks_blocks(p0): return p0.intValue
case .m_removeAppSupportDirectoryUnusedContent: return 0
case .p_currentDownloadTask_get: return 0
}
}
Expand All @@ -2517,6 +2527,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case .m_fileUrl__for_blockId: return ".fileUrl(for:)"
case .m_resumeDownloading: return ".resumeDownloading()"
case .m_isLargeVideosSize__blocks_blocks: return ".isLargeVideosSize(blocks:)"
case .m_removeAppSupportDirectoryUnusedContent: return ".removeAppSupportDirectoryUnusedContent()"
case .p_currentDownloadTask_get: return "[get] .currentDownloadTask"
}
}
Expand Down Expand Up @@ -2673,6 +2684,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func fileUrl(for blockId: Parameter<String>) -> Verify { return Verify(method: .m_fileUrl__for_blockId(`blockId`))}
public static func resumeDownloading() -> Verify { return Verify(method: .m_resumeDownloading)}
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>) -> Verify { return Verify(method: .m_isLargeVideosSize__blocks_blocks(`blocks`))}
public static func removeAppSupportDirectoryUnusedContent() -> Verify { return Verify(method: .m_removeAppSupportDirectoryUnusedContent)}
public static var currentDownloadTask: Verify { return Verify(method: .p_currentDownloadTask_get) }
}

Expand Down Expand Up @@ -2722,6 +2734,9 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>, perform: @escaping ([CourseBlock]) -> Void) -> Perform {
return Perform(method: .m_isLargeVideosSize__blocks_blocks(`blocks`), performs: perform)
}
public static func removeAppSupportDirectoryUnusedContent(perform: @escaping () -> Void) -> Perform {
return Perform(method: .m_removeAppSupportDirectoryUnusedContent, performs: perform)
}
}

public func given(_ method: Given) {
Expand Down
2 changes: 2 additions & 0 deletions Core/Core/Data/CoreStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public protocol CoreStorage {
var lastReviewDate: Date? {get set}
var user: DataLayer.User? {get set}
var userSettings: UserSettings? {get set}
var resetAppSupportDirectoryUserData: Bool? {get set}
func clear()
}

Expand All @@ -31,6 +32,7 @@ public class CoreStorageMock: CoreStorage {
public var lastReviewDate: Date?
public var user: DataLayer.User?
public var userSettings: UserSettings?
public var resetAppSupportDirectoryUserData: Bool?
public func clear() {}

public init() {}
Expand Down
59 changes: 59 additions & 0 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public protocol DownloadManagerProtocol {
func resumeDownloading() throws
func fileUrl(for blockId: String) -> URL?
func isLargeVideosSize(blocks: [CourseBlock]) -> Bool

func removeAppSupportDirectoryUnusedContent()
}

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

public func removeAppSupportDirectoryUnusedContent() {
deleteMD5HashedFolders()
}

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 {
debugPrint("Error getting Application Support Directory: \(error)")
return nil
}
}

private func isMD5Hash(_ folderName: String) -> Bool {
let md5Regex = "^[a-fA-F0-9]{32}$"
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)
debugPrint("Deleted folder: \(folderName)")
} catch {
debugPrint("Error deleting folder \(folderName): \(error)")
}
}
}
} catch {
debugPrint("Error reading contents of Application Support directory: \(error)")
}
}
}

@available(iOSApplicationExtension, unavailable)
Expand Down Expand Up @@ -638,6 +694,9 @@ public class DownloadManagerMock: DownloadManagerProtocol {
false
}

public func removeAppSupportDirectoryUnusedContent() {

}
}
#endif
// swiftlint:enable file_length
15 changes: 15 additions & 0 deletions Course/CourseTests/CourseMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,12 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
return __value
}

open func removeAppSupportDirectoryUnusedContent() {
addInvocation(.m_removeAppSupportDirectoryUnusedContent)
let perform = methodPerformValue(.m_removeAppSupportDirectoryUnusedContent) as? () -> Void
perform?()
}


fileprivate enum MethodType {
case m_publisher
Expand All @@ -2847,6 +2853,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case m_fileUrl__for_blockId(Parameter<String>)
case m_resumeDownloading
case m_isLargeVideosSize__blocks_blocks(Parameter<[CourseBlock]>)
case m_removeAppSupportDirectoryUnusedContent
case p_currentDownloadTask_get

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
Expand Down Expand Up @@ -2903,6 +2910,8 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsBlocks, rhs: rhsBlocks, with: matcher), lhsBlocks, rhsBlocks, "blocks"))
return Matcher.ComparisonResult(results)

case (.m_removeAppSupportDirectoryUnusedContent, .m_removeAppSupportDirectoryUnusedContent): return .match
case (.p_currentDownloadTask_get,.p_currentDownloadTask_get): return Matcher.ComparisonResult.match
default: return .none
}
Expand All @@ -2924,6 +2933,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case let .m_fileUrl__for_blockId(p0): return p0.intValue
case .m_resumeDownloading: return 0
case let .m_isLargeVideosSize__blocks_blocks(p0): return p0.intValue
case .m_removeAppSupportDirectoryUnusedContent: return 0
case .p_currentDownloadTask_get: return 0
}
}
Expand All @@ -2943,6 +2953,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case .m_fileUrl__for_blockId: return ".fileUrl(for:)"
case .m_resumeDownloading: return ".resumeDownloading()"
case .m_isLargeVideosSize__blocks_blocks: return ".isLargeVideosSize(blocks:)"
case .m_removeAppSupportDirectoryUnusedContent: return ".removeAppSupportDirectoryUnusedContent()"
case .p_currentDownloadTask_get: return "[get] .currentDownloadTask"
}
}
Expand Down Expand Up @@ -3099,6 +3110,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func fileUrl(for blockId: Parameter<String>) -> Verify { return Verify(method: .m_fileUrl__for_blockId(`blockId`))}
public static func resumeDownloading() -> Verify { return Verify(method: .m_resumeDownloading)}
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>) -> Verify { return Verify(method: .m_isLargeVideosSize__blocks_blocks(`blocks`))}
public static func removeAppSupportDirectoryUnusedContent() -> Verify { return Verify(method: .m_removeAppSupportDirectoryUnusedContent)}
public static var currentDownloadTask: Verify { return Verify(method: .p_currentDownloadTask_get) }
}

Expand Down Expand Up @@ -3148,6 +3160,9 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>, perform: @escaping ([CourseBlock]) -> Void) -> Perform {
return Perform(method: .m_isLargeVideosSize__blocks_blocks(`blocks`), performs: perform)
}
public static func removeAppSupportDirectoryUnusedContent(perform: @escaping () -> Void) -> Perform {
return Perform(method: .m_removeAppSupportDirectoryUnusedContent, performs: perform)
}
}

public func given(_ method: Given) {
Expand Down
15 changes: 15 additions & 0 deletions Dashboard/DashboardTests/DashboardMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,12 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
return __value
}

open func removeAppSupportDirectoryUnusedContent() {
addInvocation(.m_removeAppSupportDirectoryUnusedContent)
let perform = methodPerformValue(.m_removeAppSupportDirectoryUnusedContent) as? () -> Void
perform?()
}


fileprivate enum MethodType {
case m_publisher
Expand All @@ -2160,6 +2166,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case m_fileUrl__for_blockId(Parameter<String>)
case m_resumeDownloading
case m_isLargeVideosSize__blocks_blocks(Parameter<[CourseBlock]>)
case m_removeAppSupportDirectoryUnusedContent
case p_currentDownloadTask_get

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
Expand Down Expand Up @@ -2216,6 +2223,8 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsBlocks, rhs: rhsBlocks, with: matcher), lhsBlocks, rhsBlocks, "blocks"))
return Matcher.ComparisonResult(results)

case (.m_removeAppSupportDirectoryUnusedContent, .m_removeAppSupportDirectoryUnusedContent): return .match
case (.p_currentDownloadTask_get,.p_currentDownloadTask_get): return Matcher.ComparisonResult.match
default: return .none
}
Expand All @@ -2237,6 +2246,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case let .m_fileUrl__for_blockId(p0): return p0.intValue
case .m_resumeDownloading: return 0
case let .m_isLargeVideosSize__blocks_blocks(p0): return p0.intValue
case .m_removeAppSupportDirectoryUnusedContent: return 0
case .p_currentDownloadTask_get: return 0
}
}
Expand All @@ -2256,6 +2266,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case .m_fileUrl__for_blockId: return ".fileUrl(for:)"
case .m_resumeDownloading: return ".resumeDownloading()"
case .m_isLargeVideosSize__blocks_blocks: return ".isLargeVideosSize(blocks:)"
case .m_removeAppSupportDirectoryUnusedContent: return ".removeAppSupportDirectoryUnusedContent()"
case .p_currentDownloadTask_get: return "[get] .currentDownloadTask"
}
}
Expand Down Expand Up @@ -2412,6 +2423,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func fileUrl(for blockId: Parameter<String>) -> Verify { return Verify(method: .m_fileUrl__for_blockId(`blockId`))}
public static func resumeDownloading() -> Verify { return Verify(method: .m_resumeDownloading)}
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>) -> Verify { return Verify(method: .m_isLargeVideosSize__blocks_blocks(`blocks`))}
public static func removeAppSupportDirectoryUnusedContent() -> Verify { return Verify(method: .m_removeAppSupportDirectoryUnusedContent)}
public static var currentDownloadTask: Verify { return Verify(method: .p_currentDownloadTask_get) }
}

Expand Down Expand Up @@ -2461,6 +2473,9 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>, perform: @escaping ([CourseBlock]) -> Void) -> Perform {
return Perform(method: .m_isLargeVideosSize__blocks_blocks(`blocks`), performs: perform)
}
public static func removeAppSupportDirectoryUnusedContent(perform: @escaping () -> Void) -> Perform {
return Perform(method: .m_removeAppSupportDirectoryUnusedContent, performs: perform)
}
}

public func given(_ method: Given) {
Expand Down
15 changes: 15 additions & 0 deletions Discovery/DiscoveryTests/DiscoveryMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,12 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
return __value
}

open func removeAppSupportDirectoryUnusedContent() {
addInvocation(.m_removeAppSupportDirectoryUnusedContent)
let perform = methodPerformValue(.m_removeAppSupportDirectoryUnusedContent) as? () -> Void
perform?()
}


fileprivate enum MethodType {
case m_publisher
Expand All @@ -2354,6 +2360,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case m_fileUrl__for_blockId(Parameter<String>)
case m_resumeDownloading
case m_isLargeVideosSize__blocks_blocks(Parameter<[CourseBlock]>)
case m_removeAppSupportDirectoryUnusedContent
case p_currentDownloadTask_get

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
Expand Down Expand Up @@ -2410,6 +2417,8 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsBlocks, rhs: rhsBlocks, with: matcher), lhsBlocks, rhsBlocks, "blocks"))
return Matcher.ComparisonResult(results)

case (.m_removeAppSupportDirectoryUnusedContent, .m_removeAppSupportDirectoryUnusedContent): return .match
case (.p_currentDownloadTask_get,.p_currentDownloadTask_get): return Matcher.ComparisonResult.match
default: return .none
}
Expand All @@ -2431,6 +2440,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case let .m_fileUrl__for_blockId(p0): return p0.intValue
case .m_resumeDownloading: return 0
case let .m_isLargeVideosSize__blocks_blocks(p0): return p0.intValue
case .m_removeAppSupportDirectoryUnusedContent: return 0
case .p_currentDownloadTask_get: return 0
}
}
Expand All @@ -2450,6 +2460,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
case .m_fileUrl__for_blockId: return ".fileUrl(for:)"
case .m_resumeDownloading: return ".resumeDownloading()"
case .m_isLargeVideosSize__blocks_blocks: return ".isLargeVideosSize(blocks:)"
case .m_removeAppSupportDirectoryUnusedContent: return ".removeAppSupportDirectoryUnusedContent()"
case .p_currentDownloadTask_get: return "[get] .currentDownloadTask"
}
}
Expand Down Expand Up @@ -2606,6 +2617,7 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func fileUrl(for blockId: Parameter<String>) -> Verify { return Verify(method: .m_fileUrl__for_blockId(`blockId`))}
public static func resumeDownloading() -> Verify { return Verify(method: .m_resumeDownloading)}
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>) -> Verify { return Verify(method: .m_isLargeVideosSize__blocks_blocks(`blocks`))}
public static func removeAppSupportDirectoryUnusedContent() -> Verify { return Verify(method: .m_removeAppSupportDirectoryUnusedContent)}
public static var currentDownloadTask: Verify { return Verify(method: .p_currentDownloadTask_get) }
}

Expand Down Expand Up @@ -2655,6 +2667,9 @@ open class DownloadManagerProtocolMock: DownloadManagerProtocol, Mock {
public static func isLargeVideosSize(blocks: Parameter<[CourseBlock]>, perform: @escaping ([CourseBlock]) -> Void) -> Perform {
return Perform(method: .m_isLargeVideosSize__blocks_blocks(`blocks`), performs: perform)
}
public static func removeAppSupportDirectoryUnusedContent(perform: @escaping () -> Void) -> Perform {
return Perform(method: .m_removeAppSupportDirectoryUnusedContent, performs: perform)
}
}

public func given(_ method: Given) {
Expand Down
Loading

0 comments on commit 3ab245b

Please sign in to comment.