功能优化:跨平台文件大小计算支持

- 新增跨平台文件大小计算工具,支持Windows/macOS/Linux
- Windows使用1024进制,macOS/Linux使用1000进制
- 优化光盘类型配置,自动适配不同操作系统
- 更新文件大小格式化函数,确保跨平台一致性
- 添加测试工具和详细文档说明
This commit is contained in:
24kycj
2025-10-22 12:41:04 +08:00
parent ea93a5dd94
commit 3f20a6b16a
7 changed files with 333 additions and 94 deletions
+125
View File
@@ -0,0 +1,125 @@
/**
* 跨平台文件大小计算工具
* 处理不同操作系统的文件大小差异
*/
// 检测操作系统
export function getOS() {
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: 1000, // macOS 使用 1000 进制 (十进制)
linux: 1000 // Linux 使用 1000 进制 (十进制,遵循 SI 标准)
}
return baseSizes[os] || 1000
}
// 计算光盘类型大小
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: 1000, // macOS 使用 1000 进制 (十进制)
linux: 1000 // Linux 使用 1000 进制 (十进制,遵循 SI 标准)
}
return baseSizes[os] || 1000
}
// 光盘类型配置
export function getDiscTypes(os = null) {
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
return [
{
value: 1,
label: 'CD 700MB',
size: 700 * Math.pow(baseSize, 2)
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * Math.pow(baseSize, 3)
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * Math.pow(baseSize, 3)
},
{
value: 4,
label: 'BD 25GB',
size: 25 * Math.pow(baseSize, 3)
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * Math.pow(baseSize, 3)
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * Math.pow(baseSize, 3)
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * Math.pow(baseSize, 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)
}))
}
}