Moved converted voice messages to their own folder. Cleaning up all temporary files on reload and logout.

This commit is contained in:
Stefan Ceriu
2021-08-30 17:24:16 +03:00
committed by Stefan Ceriu
parent 66d98ac999
commit d2908eec5c
3 changed files with 57 additions and 5 deletions
@@ -26,6 +26,7 @@ enum VoiceMessageAttachmentCacheManagerError: Error {
case durationError(Error?)
case invalidNumberOfSamples
case samplingError
case cancelled
}
/**
@@ -51,6 +52,12 @@ struct VoiceMessageAttachmentCacheManagerLoadResult {
let samples: [Float]
}
@objc class VoiceMessageAttachmentCacheManagerBridge: NSObject {
@objc static func clearCache() {
VoiceMessageAttachmentCacheManager.sharedManager.clearCache()
}
}
class VoiceMessageAttachmentCacheManager {
private struct Constants {
@@ -67,6 +74,10 @@ class VoiceMessageAttachmentCacheManager {
private let workQueue: DispatchQueue
private let operationQueue: OperationQueue
private var temporaryFilesFolderURL: URL {
return URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("VoiceMessages")
}
private init() {
workQueue = DispatchQueue(label: "io.element.VoiceMessageAttachmentCacheManager.queue", qos: .userInitiated)
operationQueue = OperationQueue()
@@ -76,16 +87,27 @@ class VoiceMessageAttachmentCacheManager {
func loadAttachment(_ attachment: MXKAttachment, numberOfSamples: Int, completion: @escaping (Result<VoiceMessageAttachmentCacheManagerLoadResult, Error>) -> Void) {
guard attachment.type == MXKAttachmentTypeVoiceMessage else {
completion(Result.failure(VoiceMessageAttachmentCacheManagerError.invalidAttachmentType))
MXLog.error("[VoiceMessageAttachmentCacheManager] Invalid attachment type, ignoring request.")
return
}
guard let identifier = attachment.eventId else {
completion(Result.failure(VoiceMessageAttachmentCacheManagerError.invalidEventId))
MXLog.error("[VoiceMessageAttachmentCacheManager] Invalid event id, ignoring request.")
return
}
guard numberOfSamples > 0 else {
completion(Result.failure(VoiceMessageAttachmentCacheManagerError.invalidNumberOfSamples))
MXLog.error("[VoiceMessageAttachmentCacheManager] Invalid number of samples, ignoring request.")
return
}
do {
try setupTemporaryFilesFolder()
} catch {
completion(Result.failure(VoiceMessageAttachmentCacheManagerError.preparationError(error)))
MXLog.error("[VoiceMessageAttachmentCacheManager] Failed creating temporary files folder with error: \(error)")
return
}
@@ -105,6 +127,23 @@ class VoiceMessageAttachmentCacheManager {
}
}
func clearCache() {
for key in completionCallbacks.keys {
invokeFailureCallbacksForIdentifier(key.eventIdentifier, requiredNumberOfSamples: key.requiredNumberOfSamples, error: VoiceMessageAttachmentCacheManagerError.cancelled)
}
operationQueue.cancelAllOperations()
samples.removeAll()
durations.removeAll()
finalURLs.removeAll()
do {
try FileManager.default.removeItem(at: temporaryFilesFolderURL)
} catch {
MXLog.error("[VoiceMessageAttachmentCacheManager] Failed clearing cached disk files with error: \(error)")
}
}
private func enqueueLoadAttachment(_ attachment: MXKAttachment, identifier: String, numberOfSamples: Int, completion: @escaping (Result<VoiceMessageAttachmentCacheManagerLoadResult, Error>) -> Void) {
let callbackKey = CompletionCallbackKey(eventIdentifier: identifier, requiredNumberOfSamples: numberOfSamples)
@@ -169,8 +208,7 @@ class VoiceMessageAttachmentCacheManager {
return
}
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let newURL = temporaryDirectoryURL.appendingPathComponent(ProcessInfo().globallyUniqueString).appendingPathExtension("m4a")
let newURL = temporaryFilesFolderURL.appendingPathComponent(ProcessInfo().globallyUniqueString).appendingPathExtension("m4a")
VoiceMessageAudioConverter.convertToMPEG4AAC(sourceURL: URL(fileURLWithPath: filePath), destinationURL: newURL) { result in
self.workQueue.async {
@@ -275,4 +313,9 @@ class VoiceMessageAttachmentCacheManager {
MXLog.debug("[VoiceMessageAttachmentCacheManager] Failed task with error: \(error)")
}
private func setupTemporaryFilesFolder() throws {
let url = temporaryFilesFolderURL
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}
}