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

80 lines
2.5 KiB
Swift
Raw Permalink 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 {
let createdDate = dateOnly(file.createdAt)
if file.isFolder {
return "\(createdDate) | \(file.childNum)个项目"
}
return "\(createdDate) | \(fileSize(file.fileSize))"
}
///
static func fileSize(_ bytes: Int64) -> String {
guard bytes > 0 else { return "0 B" }
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
}
return String(format: "%.2f %@", value, units[index])
}
/// Android 使 yyyy-MM-dd
static func dateOnly(_ value: String) -> String {
guard !value.isEmpty else { return "" }
if value.count >= 10 {
let endIndex = value.index(value.startIndex, offsetBy: 10)
return String(value[..<endIndex])
}
return value
}
/// 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)
}
///
static func resizedImage(named name: String, fallbackSystemName: String, size: CGSize) -> UIImage? {
guard let source = image(named: name, fallbackSystemName: fallbackSystemName) else { return nil }
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { _ in
source.draw(in: CGRect(origin: .zero, size: size))
}.withRenderingMode(.alwaysOriginal)
}
}