#!/usr/bin/env node /** * Google Drive 文件上传示例脚本 * 演示如何使用 /.system/service/gdrive 中的脚本上传文件 */ const path = require('path'); const fs = require('fs'); // 设置环境变量指向 gdrive 服务目录 process.env.GDRIVE_SERVICE_PATH = '/.system/service/gdrive'; // 导入 gdrive-hf 模块 (支持 HuggingFace Space 环境变量) const gdriveModule = require(path.join(process.env.GDRIVE_SERVICE_PATH, 'gdrive-hf.js')); /** * 创建示例文件 */ function createSampleFile(filePath) { const content = `这是一个示例文件,用于测试 Google Drive 上传功能。 创建时间: ${new Date().toLocaleString('zh-CN')} 文件路径: ${filePath} 测试内容: Google Drive API 上传示例 此文件由 /.system/script/upload-example.js 自动生成。 `; fs.writeFileSync(filePath, content, 'utf8'); console.log(`示例文件已创建: ${filePath}`); } /** * 上传示例文件到 Google Drive */ async function uploadSampleFile() { try { // 示例文件路径 const sampleFileName = `example_${Date.now()}.txt`; const sampleFilePath = path.join(process.cwd(), sampleFileName); // 创建示例文件 createSampleFile(sampleFilePath); console.log('开始上传到 Google Drive...'); // 调用 gdrive 模块的 uploadFile 函数 const result = await gdriveModule.uploadFile(sampleFilePath); if (result) { console.log('\n=== 上传成功 ==='); console.log('文件 ID:', result.id); console.log('文件名:', result.name); console.log('文件大小:', result.size, '字节'); console.log('查看链接:', result.webViewLink); // 清理本地示例文件 fs.unlinkSync(sampleFilePath); console.log(`\n本地示例文件已删除: ${sampleFilePath}`); } } catch (error) { console.error('上传失败:', error.message); // 提供错误解决建议 if (error.message.includes('环境变量') || error.message.includes('GOOGLE_')) { console.log('\n建议解决方案:'); console.log('1. 在 HuggingFace Space 的 Settings > Environment Variables 中设置:'); console.log(' - GOOGLE_CLIENT_ID: Google OAuth 客户端 ID'); console.log(' - GOOGLE_CLIENT_SECRET: Google OAuth 客户端密钥'); console.log(' - GOOGLE_REFRESH_TOKEN: Google OAuth 刷新令牌'); console.log('2. 运行检查配置: cd /.system/service/gdrive && node gdrive-hf.js check'); } } } /** * 上传指定文件到 Google Drive * @param {string} filePath - 要上传的文件路径 * @param {string} folderId - 目标文件夹 ID (可选) */ async function uploadSpecificFile(filePath, folderId = null) { try { if (!fs.existsSync(filePath)) { console.error(`文件不存在: ${filePath}`); return; } console.log(`上传文件: ${filePath}`); // 调用 gdrive 模块的 uploadFile 函数 const result = await gdriveModule.uploadFile(filePath, folderId); if (result) { console.log('\n=== 上传成功 ==='); console.log('文件 ID:', result.id); console.log('文件名:', result.name); console.log('文件大小:', result.size, '字节'); console.log('查看链接:', result.webViewLink); } } catch (error) { console.error('上传失败:', error.message); } } /** * 显示使用说明 */ function showUsage() { console.log('使用方法:'); console.log(' node upload-example.js - 上传示例文件'); console.log(' node upload-example.js <文件路径> - 上传指定文件'); console.log(' node upload-example.js <文件路径> <文件夹ID> - 上传到指定文件夹'); console.log(''); console.log('示例:'); console.log(' node upload-example.js'); console.log(' node upload-example.js ./test.txt'); console.log(' node upload-example.js ./document.pdf 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'); console.log(''); console.log('注意:'); console.log('1. 需要在 HuggingFace Space 环境变量中设置 Google Drive 凭据'); console.log('2. 检查配置: cd /.system/service/gdrive && node gdrive-hf.js check'); console.log('3. 环境变量: GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN'); } // 主函数 async function main() { const args = process.argv.slice(2); if (args.length === 0) { // 没有参数,上传示例文件 await uploadSampleFile(); } else if (args.length === 1) { // 上传指定文件 await uploadSpecificFile(args[0]); } else if (args.length === 2) { // 上传到指定文件夹 await uploadSpecificFile(args[0], args[1]); } else { console.error('参数错误'); showUsage(); } } // 如果直接运行此脚本 if (require.main === module) { main().catch(error => { console.error('程序执行失败:', error.message); process.exit(1); }); } module.exports = { uploadSampleFile, uploadSpecificFile, createSampleFile };