|
@@ -0,0 +1,238 @@
|
|
|
+//
|
|
|
+// PathURLManager.swift
|
|
|
+// RainbowPlanet
|
|
|
+//
|
|
|
+// Created by 南鑫林 on 2019/10/25.
|
|
|
+// Copyright © 2019 RainbowPlanet. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import Foundation
|
|
|
+
|
|
|
+class PathURLManager: NSObject {
|
|
|
+
|
|
|
+ /// 获取程序的Home目录 ./
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取程序的Home目录
|
|
|
+ private class func homePath() -> URL? {
|
|
|
+ return URL(fileURLWithPath: NSHomeDirectory())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取Documnets目录 ./Documents
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Documnets目录
|
|
|
+ private class func documentPath() -> URL? {
|
|
|
+ return FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取Library目录 ./Library
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Library目录
|
|
|
+ private class func libraryPath() -> URL? {
|
|
|
+ return FileManager.default.urls(for: .libraryDirectory,in: .userDomainMask).first
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取Caches目录 ./Library/Caches
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Caches目录
|
|
|
+ private class func cachesPath() -> URL? {
|
|
|
+ return FileManager.default.urls(for: .cachesDirectory,in: .userDomainMask).first
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取tmp目录 ./tmp
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取tmp目录
|
|
|
+ class func tmpPath() -> URL? {
|
|
|
+ return URL(fileURLWithPath: NSTemporaryDirectory())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 创建项目目录路径
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 获取Documnets目录 ./Documents/UpToYo
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Documnets目录
|
|
|
+ class func projectDocumentPath() -> URL? {
|
|
|
+ return documentPath()?.appendingPathComponent("UpToYo")
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取Library目录 ./Library/UpToYo
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Library目录
|
|
|
+ class func projectLibraryPath() -> URL? {
|
|
|
+ let uptoyo = libraryPath()?.appendingPathComponent("UpToYo")
|
|
|
+ return createDirectory(pathURL: uptoyo)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取Caches目录 ./Library/Caches/UpToYo
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取Caches目录
|
|
|
+ class func projectCachesPath() -> URL? {
|
|
|
+ let uptoyo = cachesPath()?.appendingPathComponent("UpToYo")
|
|
|
+ return createDirectory(pathURL: uptoyo)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 获取tmp目录 ./tmp/UpToYo
|
|
|
+ ///
|
|
|
+ /// - Returns: 获取tmp目录
|
|
|
+ class func projectTmpPath() -> URL? {
|
|
|
+ let uptoyo = tmpPath()?.appendingPathComponent("UpToYo")
|
|
|
+ return createDirectory(pathURL: uptoyo)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 创建项目目录文件夹
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 下载文件
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectDownloadsPath(parentURL:URL?) -> URL? {
|
|
|
+ let downloads = parentURL?.appendingPathComponent("Downloads")
|
|
|
+ return createDirectory(pathURL: downloads)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 创建资源目录
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 资源文件
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectResourcesPath(parentURL:URL?) -> URL? {
|
|
|
+ let resources = parentURL?.appendingPathComponent("Resources")
|
|
|
+ return createDirectory(pathURL: resources)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 创建资源下的目录
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 图片文件夹
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectPicturesPath(parentURL:URL?) -> URL? {
|
|
|
+ let pictures = parentURL?.appendingPathComponent("Pictures")
|
|
|
+ return createDirectory(pathURL: pictures)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 视频文件夹
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectVideosPath(parentURL:URL?) -> URL? {
|
|
|
+ let videos = parentURL?.appendingPathComponent("Videos")
|
|
|
+ return createDirectory(pathURL: videos)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 音频文件夹
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectAudiosPath(parentURL:URL?) -> URL? {
|
|
|
+ let audios = parentURL?.appendingPathComponent("Audios")
|
|
|
+ return createDirectory(pathURL: audios)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 时间文件夹
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 日期文件
|
|
|
+ ///
|
|
|
+ /// - Parameter parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectDatePath(parentURL:URL?) -> URL? {
|
|
|
+ let dateString = parentURL?.appendingPathComponent(PathURLManager.dateString())
|
|
|
+ return createDirectory(pathURL: dateString)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 自定义格式生成日期文件
|
|
|
+ ///
|
|
|
+ /// - Parameters:
|
|
|
+ /// - dateFormat: 日期格式
|
|
|
+ /// - parentURL: 上一级的URL
|
|
|
+ /// - Returns: 下载文件下载文件
|
|
|
+ class func projectDateFormatPath(dateFormat:String,parentURL:URL?) -> URL? {
|
|
|
+ let dateString = parentURL?.appendingPathComponent(PathURLManager.dateString(dateFormat: dateFormat))
|
|
|
+ return createDirectory(pathURL: dateString)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 文件名
|
|
|
+extension PathURLManager {
|
|
|
+ ///
|
|
|
+ /// - Parameters:
|
|
|
+ /// - fileName: 文件名
|
|
|
+ /// - parentURL: 上一级的URL
|
|
|
+ /// - Returns: 文件路径
|
|
|
+ class func projectFilePath(fileName:String,parentURL:URL?) -> URL? {
|
|
|
+ let fileUrlPath = parentURL?.appendingPathComponent(fileName)
|
|
|
+ return fileUrlPath
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// MARK: - 创建路径
|
|
|
+extension PathURLManager {
|
|
|
+ /// 创建路径
|
|
|
+ ///
|
|
|
+ /// - Parameter pathURL: 路径
|
|
|
+ /// - Returns: 创建好的路径
|
|
|
+ class func createDirectory(pathURL:URL?) -> URL? {
|
|
|
+ //判断是否有路径
|
|
|
+ if FileManager.default.fileExists(atPath: pathURL?.path ?? "") {
|
|
|
+ return pathURL
|
|
|
+ }else {
|
|
|
+ do {
|
|
|
+ try FileManager.default.createDirectory(atPath: pathURL?.path ?? "", withIntermediateDirectories: true, attributes: nil)
|
|
|
+ return pathURL
|
|
|
+ } catch {
|
|
|
+ NXLLog("路径错误")
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 生成日期
|
|
|
+extension PathURLManager {
|
|
|
+
|
|
|
+ /// 生成日期
|
|
|
+ ///
|
|
|
+ /// - Returns: 日期字符串
|
|
|
+ class func dateString() -> String {
|
|
|
+ let dateStr = Date.dateToString(Date(), dateFormat: "yyyyMMdd")
|
|
|
+ return dateStr
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 自定义格式生成日期
|
|
|
+ ///
|
|
|
+ /// - Returns: 日期字符串
|
|
|
+ class func dateString(dateFormat:String) -> String {
|
|
|
+ let dateStr = Date.dateToString(Date(), dateFormat: dateFormat)
|
|
|
+ return dateStr
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// MARK: - 生成随机字符串
|
|
|
+extension PathURLManager {
|
|
|
+ /// 生成随机字符串
|
|
|
+ ///
|
|
|
+ /// - Returns: 随机字符串
|
|
|
+ class func randomString() -> String {
|
|
|
+ let puuid = CFUUIDCreate(nil)
|
|
|
+ let uuidString = CFUUIDCreateString(nil, puuid)
|
|
|
+ let result = CFStringCreateCopy(nil, uuidString)
|
|
|
+ return result! as String
|
|
|
+ }
|
|
|
+}
|