Files
SoonWorkerD/src/renderer/utils/fileSize.js
T
2026-05-12 21:42:42 +08:00

128 lines
3.1 KiB
JavaScript
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.
/**
* 跨平台文件大小计算工具
* 处理不同操作系统的文件大小差异
*/
// 检测操作系统(网页端无 process.platform,返回 'web'
export function getOS() {
if (typeof process === 'undefined' || process.platform === undefined) return 'web'
const platform = process.platform
if (platform === 'win32') return 'windows'
if (platform === 'darwin') return 'mac'
if (platform === 'linux') return 'linux'
return 'unknown'
}
// 获取基础单位大小
export function getBaseSize() {
const os = getOS()
// 不同操作系统的基础单位
const baseSizes = {
windows: 1024, // Windows 使用 1024 进制 (二进制)
mac: 1024, // macOS 使用 1024 进制 (十进制)
linux: 1024, // Linux 使用 1024 进制 (十进制,遵循 SI 标准)
web: 1024 // 网页端固定 1024
}
return baseSizes[os] || 1024
}
// 计算光盘类型大小
export function calculateDiscSize(sizeInGB, os = null) {
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
return sizeInGB * Math.pow(baseSize, 3)
}
// 为特定操作系统获取基础大小
export function getBaseSizeForOS(os) {
const baseSizes = {
windows: 1024, // Windows 使用 1024 进制 (二进制)
mac: 1024, // macOS 使用 1024 进制 (十进制)
linux: 1024 // Linux 使用 1024 进制 (十进制,遵循 SI 标准)
}
return baseSizes[os] || 1024
}
// 光盘类型配置
export function getDiscTypes(os = null) {
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
return [
{
value: 1,
label: 'CD 700MB',
size: 700 * Math.pow(1000, 2)
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * Math.pow(1000, 3)
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * Math.pow(1000, 3)
},
{
value: 4,
label: 'BD 25GB',
size: 25 * Math.pow(1000, 3)
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * Math.pow(1000, 3)
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * Math.pow(1000, 3)
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * Math.pow(1000, 3)
}
]
}
// 格式化文件大小显示
export function formatFileSize(bytes, os = null) {
if (!bytes || bytes < 0) return '0 B'
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let size = bytes
let unitIndex = 0
while (size >= baseSize && unitIndex < units.length - 1) {
size /= baseSize
unitIndex++
}
return `${size.toFixed(2)} ${units[unitIndex]}`
}
// 获取当前系统的光盘类型配置
export function getCurrentOSDiscTypes() {
return getDiscTypes()
}
// 调试信息:显示当前系统配置
export function getDebugInfo() {
const os = getOS()
const baseSize = getBaseSize()
const discTypes = getDiscTypes()
return {
os,
baseSize,
discTypes: discTypes.map(type => ({
...type,
formattedSize: formatFileSize(type.size)
}))
}
}