Enabling downloading option for voicemails

This commit is contained in:
Sumanth Nadigadda 2021-05-21 16:37:23 +05:30
parent 5e52c3c44a
commit 6c51d3ee65

View File

@ -61,7 +61,7 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior {
} else {
// Download binary
downloadAudioFile { url in
downloadAudioFile(information: information) { url in
// TODO: Actually pause/play
Self.activeAudioPlayerDelegate?.audioFileURL = url
@ -84,18 +84,110 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior {
// MARK: - Download Audio File
//--------------------------------------------------
func downloadAudioFile(_ completion: @escaping (URL) -> ()) {
func downloadAudioFile(information:[AnyHashable: Any]?,_ completion: @escaping (URL) -> ()) {
guard let extraparams = information?["extraParameters"] as? [AnyHashable:Any] else {return}
guard let requestParams = MVMCoreRequestParameters.init(pageType: "getVoiceMailMessage", extraParameters: extraparams) else { return }
// TODO: Check cache. Return if available
// let audioData = MVMCoreCache.shared()?.getCachedData(withName: PagePlayAudioBehavior.activeAudioPlayerDelegate!.messageID!)
guard let soundFileURL = Bundle.main.url(forResource: "sampleAudio", withExtension: "wav") else { return }
// TODO: Store audio file in cache using some key (messageID???) to store it.
// MVMCoreCache.shared()?.addData(toCache: soundFileURL.dataRepresentation, withName: PagePlayAudioBehavior.activeAudioPlayerDelegate!.messageID!)
completion(soundFileURL)
// return soundFileURL
if let voicemailID = extraparams["messageID"] as? String, checkIfFileExists(voicemailID: voicemailID) {
let vmURL = self.constructVoicemailFilePathWithID(voicemailID: voicemailID)
completion(vmURL)
} else {
MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParams, locationForError: "PlayAudioRequest", requestFinished: { response, error in
guard let voicemailJSON = response as? [String:AnyHashable] else { return }
guard let voicemail = voicemailJSON["bin"] as? String, let voicemailID = voicemailJSON["vmMessageId"] as? String else { return }
let audioURL = self.saveVoicemailToDocumentDirectory(voicemail: voicemail, voicemailID: voicemailID)
completion(audioURL)
})
}
}
func checkIfFileExists(voicemailID:String) -> Bool {
var cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
cachePath?.appendPathComponent("FiOS.FDV.Voicemails", isDirectory: false)
let fileName = cachePath?.appendingPathComponent("VM_\(voicemailID).wmv")
return FileManager.default.fileExists(atPath: fileName?.path ?? "")
}
func constructVoicemailFilePathWithID(voicemailID:String) -> URL {
let voicemailUrl = URL(string: "fileNotFound")!
///Setting Voicemail Path
var cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
cachePath?.appendPathComponent("FiOS.FDV.Voicemails", isDirectory: false)
if let path = cachePath?.path, !FileManager.default.fileExists(atPath: path) {
do{
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
}
}
let fileName = cachePath?.appendingPathComponent("VM_\(voicemailID).wmv")
return fileName ?? voicemailUrl
}
func saveVoicemailToDocumentDirectory(voicemail:String, voicemailID:String) -> URL {
let voicemailUrl = URL(string: "fileNotFound")!
let fileName = self.constructVoicemailFilePathWithID(voicemailID: voicemailID)
let voicemailData = Data(base64Encoded: voicemail)
do{
try voicemailData?.write(to: fileName, options: .completeFileProtection)
return fileName
} catch let error as NSError {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
}
return voicemailUrl
}
// func saveVoicemailToDocumentDirectory(voicemail:String) -> URL {
// let voicemailUrl = URL(string: "fileNotFound")!
// ///Setting Voicemail Path
//
// let cacheDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first as NSString?
//
// if let cachePath = cacheDirectory?.appendingPathComponent("fdv.voicemails"), !FileManager.default.fileExists(atPath: cachePath) {
// do{
// try FileManager.default.createDirectory(atPath: cachePath, withIntermediateDirectories: false, attributes: nil)
// } catch let error as NSError {
// MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
// }
// }
// let fileName = cacheDirectory?.appendingPathComponent("fdv.voicemails").appending("/VM_1.wmv")
//#warning("Pending task to check if we can protect the voicemail data")
//
// let voicemailData = Data(base64Encoded: voicemail)
// do{
// if let _fileName = fileName, let fileUrl = URL(string: _fileName) {
// try voicemailData?.write(to: fileUrl, options: .completeFileProtection)
// return fileUrl
// }
// } catch let error as NSError {
// MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
// }
// return voicemailUrl
// }
// NSDictionary *completeProtection = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
// [[NSFileManager defaultManager] setAttributes:completeProtection ofItemAtPath:appFile error:nil];
// return ([data writeToFile:appFile atomically:YES]);
// func downloadVoicemail(url: URL?) {
//
// let downloadTask = URLSession.shared.downloadTask(with: url!) { url, response, error in
// guard error == nil, let fileURL = url else { return }
// do {
// let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
// let savedURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent)
// try FileManager.default.moveItem(at: fileURL, to: savedURL)
//
// } catch let error as NSError {
// MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
// }
// }
// downloadTask.resume()
// }
/*
// https://oneconfluence.verizon.com/pages/viewpage.action?spaceKey=EIM&title=FDV+API