UNPKG

@xuanqikai/one-click-upload

Version:

A CLI tool for one-click file upload to cloud storage services (OSS, TOS)

176 lines 5.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getFileInfo = getFileInfo; exports.scanDirectory = scanDirectory; exports.pathExists = pathExists; exports.formatFileSize = formatFileSize; exports.formatDuration = formatDuration; exports.normalizePath = normalizePath; exports.normalizeRemotePath = normalizeRemotePath; exports.generateRemoteFilePath = generateRemoteFilePath; exports.isHiddenFile = isHiddenFile; exports.getFileExtension = getFileExtension; exports.ensureDir = ensureDir; const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); /** * 获取文件信息 */ async function getFileInfo(filePath, basePath) { const stats = await fs.stat(filePath); const relativePath = basePath ? path.relative(basePath, filePath) : path.basename(filePath); return { path: filePath, name: path.basename(filePath), size: stats.size, isDirectory: stats.isDirectory(), // 标准化相对路径,确保在所有平台上使用正斜杠 relativePath: normalizePath(relativePath) }; } /** * 递归扫描目录,获取所有文件 */ async function scanDirectory(dirPath, basePath) { const files = []; const base = basePath || dirPath; async function scan(currentPath) { const items = await fs.readdir(currentPath); for (const item of items) { const itemPath = path.join(currentPath, item); const stats = await fs.stat(itemPath); if (stats.isDirectory()) { await scan(itemPath); } else { const fileInfo = await getFileInfo(itemPath, base); files.push(fileInfo); } } } await scan(dirPath); return files; } /** * 检查路径是否存在 */ async function pathExists(filePath) { try { await fs.access(filePath); return true; } catch { return false; } } /** * 格式化文件大小 */ function formatFileSize(bytes) { const units = ['B', 'KB', 'MB', 'GB', 'TB']; let size = bytes; let unitIndex = 0; while (size >= 1024 && unitIndex < units.length - 1) { size /= 1024; unitIndex++; } return `${size.toFixed(2)} ${units[unitIndex]}`; } /** * 格式化持续时间 */ function formatDuration(milliseconds) { const seconds = Math.floor(milliseconds / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); if (hours > 0) { return `${hours}h ${minutes % 60}m ${seconds % 60}s`; } else if (minutes > 0) { return `${minutes}m ${seconds % 60}s`; } else { return `${seconds}s`; } } /** * 标准化路径分隔符(统一使用正斜杠) */ function normalizePath(filePath) { return filePath.replace(/\\/g, '/'); } /** * 确保远程路径格式正确 */ function normalizeRemotePath(remotePath) { // 移除开头的斜杠,确保不以斜杠开头 let normalized = remotePath.replace(/^\/+/, ''); // 标准化路径分隔符 normalized = normalizePath(normalized); // 确保目录路径以斜杠结尾 return normalized; } /** * 生成远程文件路径 */ function generateRemoteFilePath(remotePath, fileName) { const normalizedRemotePath = normalizeRemotePath(remotePath); if (normalizedRemotePath === '') { return fileName; } // 如果远程路径不以斜杠结尾,添加斜杠 const separator = normalizedRemotePath.endsWith('/') ? '' : '/'; return `${normalizedRemotePath}${separator}${fileName}`; } /** * 检查文件是否为隐藏文件 */ function isHiddenFile(fileName) { return fileName.startsWith('.'); } /** * 获取文件扩展名 */ function getFileExtension(fileName) { return path.extname(fileName).toLowerCase(); } /** * 创建目录(如果不存在) */ async function ensureDir(dirPath) { await fs.ensureDir(dirPath); } //# sourceMappingURL=fileUtils.js.map