Music_Player3/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_CacheManager.swift

191 lines
6.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MP_CacheManager.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/23.
//
import Foundation
////
class MP_CacheAndArchiverManager {
// 访
static let shared = MP_CacheAndArchiverManager()
//
let fileManager = FileManager.default
///
// private var archiverDic:[String: MP_PlayerRequestModel]!
///
private var isCompleteds:[String:Bool] = [:]
private init() {
}
///
func createCachePath() -> String? {
// CacheMP_PlayerCache
let cacheDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
let path = cacheDirectory+"/"+"MP_PlayerCache"
//
guard fileManager.fileExists(atPath: path) == false else {
//
return path
}
//
do {
//
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
return path
} catch {
//
print("创建播放器缓存文件夹失败,失败原因:\(error)")
return nil
}
}
///
func createTempFile(_ videoID:String) -> Bool {
//
let path = tempPath(videoID)
if fileManager.fileExists(atPath: path) {
//
do{
try fileManager.removeItem(atPath: path)
}catch {
print("删除临时文件失败error: \(error)")
}
}
//
return fileManager.createFile(atPath: path, contents: nil, attributes: nil)
}
///
func writeDataToAudioFileTempPathWithData(_ data:Data, videoId:String) {
guard let handle = FileHandle(forWritingAtPath: tempPath(videoId)) else {return}
if #available(iOS 13.4, *) {
do{
try handle.seekToEnd()
}catch {
print("Seek到末尾失败失败原因:\(error)")
}
} else {
handle.seekToEndOfFile()
}
//
if #available(iOS 13.4, *) {
do{
try handle.write(contentsOf: data)
}catch{
print("写入数据失败,失败原因:\(error)")
}
} else {
handle.write(data)
}
do {
if #available(iOS 13.4, *) {
try? handle.close() //
} else {
handle.closeFile()
}
}
}
///
func readTempFileDataWithOffset(_ offset:UInt64, length:Int, videoId:String) -> Data? {
var allhandle:FileHandle!
//
if isCompleteds[videoId] == true {
//
let path = audioCachedPath() ?? ""
let audioName = "/"+videoId+".mp4"
let cachePath = path+audioName
guard let handle = FileHandle(forReadingAtPath: cachePath) else {return nil}
allhandle = handle
}else {
let path = tempPath(videoId)
guard let handle = FileHandle(forReadingAtPath: path) else {return nil}
allhandle = handle
}
if #available(iOS 13.0, *) {
do{
try allhandle.seek(toOffset: offset)
}catch{
print("Seek到指定位置失败失败原因:\(error)")
try? allhandle.close()
}
} else {
allhandle.seek(toFileOffset: offset)
}
if #available(iOS 13.4, *) {
do{
let data = try allhandle.read(upToCount: length)
try allhandle.close()
return data
}catch{
print("读取到指定位置失败,失败原因:\(error)")
return nil
}
} else {
let data = allhandle.readData(ofLength: length)
allhandle.closeFile()
return data
}
}
///
/// - Parameter videoId: 使videoId
func moveAudioFileFromTempPathToCachePath(_ videoId: String) -> Bool {
guard let path = audioCachedPath() else {return false }
// 1.
if !(fileManager.fileExists(atPath: path)) {
//
do{
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
}catch {
print("创建缓存夹失败,失败原因:\(error)")
return false
}
}
let audioName = "/"+videoId+".mp4"
let cachePath = path+audioName
//
if fileManager.fileExists(atPath: cachePath) {
// do {
// print("")
// try fileManager.removeItem(atPath: cachePath)
// } catch {
// print(": \(error)")
// return false
// }
return true
}
// 2.
let tempFilePath = tempPath(videoId)
guard fileManager.fileExists(atPath: tempFilePath) else {
// print(": \(tempFilePath)")
return false
}
// 3.
do {
try fileManager.moveItem(atPath: tempFilePath, toPath: cachePath)
// print("\(videoId):\(cachePath)")
//
isCompleteds[videoId] = true
return true
} catch {
print("移动 \(videoId) 缓存文件失败,失败原因: \(error)")
// deleteKeyValueIfHaveArchivedWithVideoId(videoId) //
return false
}
}
///
private func audioCachedPath() -> String? {
return (createCachePath() ?? "")
}
///
private func tempPath(_ videoID:String) -> String {
return (NSTemporaryDirectory())+"/"+"\(videoID).mp4"
}
///
private func archiverPath() -> String {
return ((createCachePath() ?? ""))+"/"+"MP_Player.archiver"
}
}