OpenCode Deployer commited on
Commit
04d62ee
·
1 Parent(s): 618270f
Files changed (2) hide show
  1. script/upload-example.js +159 -0
  2. service/start-services.sh +15 -0
script/upload-example.js ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Google Drive 文件上传示例脚本
5
+ * 演示如何使用 /.system/service/gdrive 中的脚本上传文件
6
+ */
7
+
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+
11
+ // 设置环境变量指向 gdrive 服务目录
12
+ process.env.GDRIVE_SERVICE_PATH = '/.system/service/gdrive';
13
+
14
+ // 导入 gdrive-hf 模块 (支持 HuggingFace Space 环境变量)
15
+ const gdriveModule = require(path.join(process.env.GDRIVE_SERVICE_PATH, 'gdrive-hf.js'));
16
+
17
+ /**
18
+ * 创建示例文件
19
+ */
20
+ function createSampleFile(filePath) {
21
+ const content = `这是一个示例文件,用于测试 Google Drive 上传功能。
22
+
23
+ 创建时间: ${new Date().toLocaleString('zh-CN')}
24
+ 文件路径: ${filePath}
25
+ 测试内容: Google Drive API 上传示例
26
+
27
+ 此文件由 /.system/script/upload-example.js 自动生成。
28
+ `;
29
+
30
+ fs.writeFileSync(filePath, content, 'utf8');
31
+ console.log(`示例文件已创建: ${filePath}`);
32
+ }
33
+
34
+ /**
35
+ * 上传示例文件到 Google Drive
36
+ */
37
+ async function uploadSampleFile() {
38
+ try {
39
+ // 示例文件路径
40
+ const sampleFileName = `example_${Date.now()}.txt`;
41
+ const sampleFilePath = path.join(process.cwd(), sampleFileName);
42
+
43
+ // 创建示例文件
44
+ createSampleFile(sampleFilePath);
45
+
46
+ console.log('开始上传到 Google Drive...');
47
+
48
+ // 调用 gdrive 模块的 uploadFile 函数
49
+ const result = await gdriveModule.uploadFile(sampleFilePath);
50
+
51
+ if (result) {
52
+ console.log('\n=== 上传成功 ===');
53
+ console.log('文件 ID:', result.id);
54
+ console.log('文件名:', result.name);
55
+ console.log('文件大小:', result.size, '字节');
56
+ console.log('查看链接:', result.webViewLink);
57
+
58
+ // 清理本地示例文件
59
+ fs.unlinkSync(sampleFilePath);
60
+ console.log(`\n本地示例文件已删除: ${sampleFilePath}`);
61
+ }
62
+
63
+ } catch (error) {
64
+ console.error('上传失败:', error.message);
65
+
66
+ // 提供错误解决建议
67
+ if (error.message.includes('环境变量') || error.message.includes('GOOGLE_')) {
68
+ console.log('\n建议解决方案:');
69
+ console.log('1. 在 HuggingFace Space 的 Settings > Environment Variables 中设置:');
70
+ console.log(' - GOOGLE_CLIENT_ID: Google OAuth 客户端 ID');
71
+ console.log(' - GOOGLE_CLIENT_SECRET: Google OAuth 客户端密钥');
72
+ console.log(' - GOOGLE_REFRESH_TOKEN: Google OAuth 刷新令牌');
73
+ console.log('2. 运行检查配置: cd /.system/service/gdrive && node gdrive-hf.js check');
74
+ }
75
+ }
76
+ }
77
+
78
+ /**
79
+ * 上传指定文件到 Google Drive
80
+ * @param {string} filePath - 要上传的文件路径
81
+ * @param {string} folderId - 目标文件夹 ID (可选)
82
+ */
83
+ async function uploadSpecificFile(filePath, folderId = null) {
84
+ try {
85
+ if (!fs.existsSync(filePath)) {
86
+ console.error(`文件不存在: ${filePath}`);
87
+ return;
88
+ }
89
+
90
+ console.log(`上传文件: ${filePath}`);
91
+
92
+ // 调用 gdrive 模块的 uploadFile 函数
93
+ const result = await gdriveModule.uploadFile(filePath, folderId);
94
+
95
+ if (result) {
96
+ console.log('\n=== 上传成功 ===');
97
+ console.log('文件 ID:', result.id);
98
+ console.log('文件名:', result.name);
99
+ console.log('文件大小:', result.size, '字节');
100
+ console.log('查看链接:', result.webViewLink);
101
+ }
102
+
103
+ } catch (error) {
104
+ console.error('上传失败:', error.message);
105
+ }
106
+ }
107
+
108
+ /**
109
+ * 显示使用说明
110
+ */
111
+ function showUsage() {
112
+ console.log('使用方法:');
113
+ console.log(' node upload-example.js - 上传示例文件');
114
+ console.log(' node upload-example.js <文件路径> - 上传指定文件');
115
+ console.log(' node upload-example.js <文件路径> <文件夹ID> - 上传到指定文件夹');
116
+ console.log('');
117
+ console.log('示例:');
118
+ console.log(' node upload-example.js');
119
+ console.log(' node upload-example.js ./test.txt');
120
+ console.log(' node upload-example.js ./document.pdf 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms');
121
+ console.log('');
122
+ console.log('注意:');
123
+ console.log('1. 需要在 HuggingFace Space 环境变量中设置 Google Drive 凭据');
124
+ console.log('2. 检查配置: cd /.system/service/gdrive && node gdrive-hf.js check');
125
+ console.log('3. 环境变量: GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN');
126
+ }
127
+
128
+ // 主函数
129
+ async function main() {
130
+ const args = process.argv.slice(2);
131
+
132
+ if (args.length === 0) {
133
+ // 没有参数,上传示例文件
134
+ await uploadSampleFile();
135
+ } else if (args.length === 1) {
136
+ // 上传指定文件
137
+ await uploadSpecificFile(args[0]);
138
+ } else if (args.length === 2) {
139
+ // 上��到指定文件夹
140
+ await uploadSpecificFile(args[0], args[1]);
141
+ } else {
142
+ console.error('参数错误');
143
+ showUsage();
144
+ }
145
+ }
146
+
147
+ // 如果直接运行此脚本
148
+ if (require.main === module) {
149
+ main().catch(error => {
150
+ console.error('程序执行失败:', error.message);
151
+ process.exit(1);
152
+ });
153
+ }
154
+
155
+ module.exports = {
156
+ uploadSampleFile,
157
+ uploadSpecificFile,
158
+ createSampleFile
159
+ };
service/start-services.sh CHANGED
@@ -7,6 +7,21 @@ set -e
7
 
8
  /.system/script/restore.sh || true
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  echo "🚀 启动 OpenCode AI Web Interface 服务..."
11
 
12
  # 明确禁用服务器认证,确保公开访问
 
7
 
8
  /.system/script/restore.sh || true
9
 
10
+ echo ""
11
+ echo ""
12
+ echo "安装 gdrive 依赖..."
13
+ cd /.system/service/gdrive && npm install
14
+
15
+ echo ""
16
+ echo ""
17
+ echo "node /.system/service/gdrive/gdrive-hf.js check"
18
+ node /.system/service/gdrive/gdrive-hf.js check
19
+
20
+ echo ""
21
+ echo ""
22
+ echo "/.system/script/upload-example.js"
23
+ node /.system/script/upload-example.js
24
+
25
  echo "🚀 启动 OpenCode AI Web Interface 服务..."
26
 
27
  # 明确禁用服务器认证,确保公开访问