63 lines
1.7 KiB
Swift
63 lines
1.7 KiB
Swift
//
|
||
// CloudDriveFormatter.swift
|
||
// suixinkan
|
||
//
|
||
|
||
import Foundation
|
||
import UIKit
|
||
|
||
/// 云盘展示格式化工具。
|
||
enum CloudDriveFormatter {
|
||
/// 文件类型名称。
|
||
static func typeName(_ type: Int) -> String {
|
||
switch type {
|
||
case 1:
|
||
"视频"
|
||
case 2:
|
||
"图片"
|
||
case 99:
|
||
"文件夹"
|
||
default:
|
||
"文件"
|
||
}
|
||
}
|
||
|
||
/// 文件描述。
|
||
static func fileDescription(_ file: CloudFile) -> String {
|
||
if file.isFolder {
|
||
return "\(file.childNum) 个项目"
|
||
}
|
||
return "\(typeName(file.type)) \(fileSize(file.fileSize))"
|
||
}
|
||
|
||
/// 文件大小展示。
|
||
static func fileSize(_ bytes: Int64) -> String {
|
||
guard bytes > 0 else { return "0B" }
|
||
let units = ["B", "KB", "MB", "GB", "TB"]
|
||
var value = Double(bytes)
|
||
var index = 0
|
||
while value >= 1024, index < units.count - 1 {
|
||
value /= 1024
|
||
index += 1
|
||
}
|
||
if index == 0 {
|
||
return "\(Int(value))\(units[index])"
|
||
}
|
||
return String(format: "%.1f%@", value, units[index])
|
||
}
|
||
|
||
/// URL 或文件名的后缀。
|
||
static func fileExtension(_ value: String) -> String {
|
||
let ext = URL(string: value)?.pathExtension ?? URL(fileURLWithPath: value).pathExtension
|
||
return ext.isEmpty ? typeName(0) : ext.uppercased()
|
||
}
|
||
}
|
||
|
||
/// 云盘 Android 同名图片资源访问工具。
|
||
enum CloudDriveAsset {
|
||
/// 返回云盘图片资源,缺失时使用系统图标兜底。
|
||
static func image(named name: String, fallbackSystemName: String) -> UIImage? {
|
||
UIImage(named: name)?.withRenderingMode(.alwaysOriginal) ?? UIImage(systemName: fallbackSystemName)
|
||
}
|
||
}
|