Files
suixinkan_uikit/suixinkan/UI/CloudDrive/CloudDriveFormatter.swift

63 lines
1.7 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.

//
// 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)
}
}