OpenCode Deployer commited on
Commit
9516b0c
·
1 Parent(s): 036fcb8
note.md CHANGED
@@ -55,3 +55,12 @@
55
  | |-- CLAUDE.md
56
  |
57
 
 
 
 
 
 
 
 
 
 
 
55
  | |-- CLAUDE.md
56
  |
57
 
58
+
59
+ # google drive:
60
+ ## 下载文件
61
+ node download-file.js "文件名.txt" ./downloads/
62
+ node download-file.js 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms
63
+
64
+ ## 上传文件
65
+ node upload-file.js ./local-file.txt
66
+ node upload-file.js ./document.pdf "云端文档.pdf"
service/gdrive/download-file.js ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const GoogleDriveService = require('./gdrive-service');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ // 共享目录的文件夹ID(从共享链接中提取)
6
+ const SHARED_FOLDER_ID = '1UCRmKH3yfMq4u6NjMMW9av30oyNjTgFk';
7
+
8
+ async function main() {
9
+ // 获取命令行参数
10
+ const args = process.argv.slice(2);
11
+
12
+ if (args.length < 1) {
13
+ console.log('使用方法: node download-file.js <文件ID或文件名> [保存路径]');
14
+ console.log('示例:');
15
+ console.log(' node download-file.js 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms ./downloaded-file.txt');
16
+ console.log(' node download-file.js "文档.txt" ./downloads/');
17
+ process.exit(1);
18
+ }
19
+
20
+ const fileIdentifier = args[0];
21
+ let destinationPath = args[1];
22
+
23
+ const drive = new GoogleDriveService();
24
+
25
+ try {
26
+ console.log('正在搜索文件...');
27
+ let targetFile = null;
28
+
29
+ // 首先尝试作为文件ID查找
30
+ try {
31
+ targetFile = await drive.getFileInfo(fileIdentifier);
32
+ console.log(`通过ID找到文件: ${targetFile.name}`);
33
+ } catch (idError) {
34
+ // 如果ID查找失败,尝试按文件名搜索
35
+ console.log('ID查找失败,尝试按文件名搜索...');
36
+ const query = `name='${fileIdentifier}' and '${SHARED_FOLDER_ID}' in parents and trashed=false`;
37
+ const searchResult = await drive.listFiles({ query: query });
38
+
39
+ if (searchResult.files && searchResult.files.length > 0) {
40
+ targetFile = searchResult.files[0];
41
+ console.log(`通过文件名找到: ${targetFile.name}`);
42
+
43
+ if (searchResult.files.length > 1) {
44
+ console.log(`注意: 找到 ${searchResult.files.length} 个同名文件,将下载第一个`);
45
+ }
46
+ }
47
+ }
48
+
49
+ if (!targetFile) {
50
+ console.error('未找到指定文件,请检查文件ID或文件名是否正确');
51
+ process.exit(1);
52
+ }
53
+
54
+ // 检查是否为文件夹
55
+ if (targetFile.mimeType === 'application/vnd.google-apps.folder') {
56
+ console.error('无法下载文件夹,请指定具体的文件');
57
+ process.exit(1);
58
+ }
59
+
60
+ // 如果没有指定保存路径,使用当前目录和原文件名
61
+ if (!destinationPath) {
62
+ destinationPath = `./${targetFile.name}`;
63
+ } else {
64
+ // 如果指定的是目录,在目录后添加文件名
65
+ if (fs.existsSync(destinationPath) && fs.statSync(destinationPath).isDirectory()) {
66
+ destinationPath = path.join(destinationPath, targetFile.name);
67
+ }
68
+ }
69
+
70
+ // 确保目标目录存在
71
+ const targetDir = path.dirname(destinationPath);
72
+ if (!fs.existsSync(targetDir)) {
73
+ fs.mkdirSync(targetDir, { recursive: true });
74
+ console.log(`创建目标目录: ${targetDir}`);
75
+ }
76
+
77
+ console.log(`开始下载文件: ${targetFile.name}`);
78
+ console.log(`文件大小: ${targetFile.size ? (parseInt(targetFile.size) / 1024).toFixed(2) + ' KB' : '未知'}`);
79
+ console.log(`保存路径: ${destinationPath}`);
80
+ console.log('-'.repeat(60));
81
+
82
+ // 下载文件
83
+ await drive.downloadFile(targetFile.id, destinationPath);
84
+
85
+ // 验证下载的文件
86
+ if (fs.existsSync(destinationPath)) {
87
+ const stats = fs.statSync(destinationPath);
88
+ console.log('-'.repeat(60));
89
+ console.log('✅ 文件下载成功!');
90
+ console.log(`📁 保存位置: ${destinationPath}`);
91
+ console.log(`📊 本地文件大小: ${(stats.size / 1024).toFixed(2)} KB`);
92
+ console.log(`📅 下载时间: ${new Date().toLocaleString('zh-CN')}`);
93
+ } else {
94
+ console.error('❌ 文件下载失败,未找到本地文件');
95
+ process.exit(1);
96
+ }
97
+
98
+ } catch (error) {
99
+ console.error('❌ 下载过程中发生错误:', error.message);
100
+
101
+ // 提供一些调试信息
102
+ if (error.message.includes('not found')) {
103
+ console.log('可能的原因:');
104
+ console.log('1. 文件ID或文件名不正确');
105
+ console.log('2. 服务账户没有访问该文件的权限');
106
+ console.log('3. 文件已被删除或移动');
107
+ console.log('4. 文件不在指定的共享目录中');
108
+ }
109
+
110
+ process.exit(1);
111
+ }
112
+ }
113
+
114
+ main().catch(error => {
115
+ console.error('执行过程中发生错误:', error);
116
+ process.exit(1);
117
+ });
service/gdrive/upload-file.js ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const GoogleDriveService = require('./gdrive-service');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ // 共享目录的文件夹ID(从共享链接中提取)
6
+ const SHARED_FOLDER_ID = '1UCRmKH3yfMq4u6NjMMW9av30oyNjTgFk';
7
+
8
+ async function main() {
9
+ // 获取命令行参数
10
+ const args = process.argv.slice(2);
11
+
12
+ if (args.length < 1) {
13
+ console.log('使用方法: node upload-file.js <本地文件路径> [云端文件名]');
14
+ console.log('示例:');
15
+ console.log(' node upload-file.js ./local-file.txt');
16
+ console.log(' node upload-file.js ./document.pdf "云端文档.pdf"');
17
+ console.log(' node upload-file.js ./data.json "backup/data.json"');
18
+ process.exit(1);
19
+ }
20
+
21
+ const localFilePath = args[0];
22
+ let remoteFileName = args[1];
23
+
24
+ const drive = new GoogleDriveService();
25
+
26
+ try {
27
+ // 检查本地文件是否存在
28
+ if (!fs.existsSync(localFilePath)) {
29
+ console.error(`❌ 本地文件不存在: ${localFilePath}`);
30
+ process.exit(1);
31
+ }
32
+
33
+ // 获取文件信息
34
+ const stats = fs.statSync(localFilePath);
35
+ if (!stats.isFile()) {
36
+ console.error(`❌ 指定路径不是文件: ${localFilePath}`);
37
+ process.exit(1);
38
+ }
39
+
40
+ // 如果没有指定云端文件名,使用本地文件名
41
+ if (!remoteFileName) {
42
+ remoteFileName = path.basename(localFilePath);
43
+ }
44
+
45
+ console.log('准备上传文件到 Google Drive...');
46
+ console.log(`📁 本地文件: ${localFilePath}`);
47
+ console.log(`📊 文件大小: ${(stats.size / 1024).toFixed(2)} KB`);
48
+ console.log(`📝 云端文件名: ${remoteFileName}`);
49
+ console.log(`📂 目标文件夹ID: ${SHARED_FOLDER_ID}`);
50
+ console.log('-'.repeat(60));
51
+
52
+ // 检查是否已存在同名文件
53
+ const existingFileQuery = `name='${remoteFileName}' and '${SHARED_FOLDER_ID}' in parents and trashed=false`;
54
+ const existingFiles = await drive.listFiles({ query: existingFileQuery });
55
+
56
+ if (existingFiles.files && existingFiles.files.length > 0) {
57
+ console.log(`⚠️ 发现同名文件已存在: ${remoteFileName}`);
58
+ console.log('文件信息:');
59
+ existingFiles.files.forEach(file => {
60
+ const size = file.size ? `(${(parseInt(file.size) / 1024).toFixed(2)} KB)` : '(大小未知)';
61
+ const modifiedTime = file.modifiedTime ? new Date(file.modifiedTime).toLocaleString('zh-CN') : '时间未知';
62
+ console.log(` - ID: ${file.id} ${size} 修改时间: ${modifiedTime}`);
63
+ });
64
+
65
+ console.log('⚠️ 将覆盖现有文件(如需保留请先重命名)');
66
+ }
67
+
68
+ // 开始上传
69
+ console.log('🚀 开始上传...');
70
+ const startTime = Date.now();
71
+
72
+ const result = await drive.uploadFile(localFilePath, remoteFileName, SHARED_FOLDER_ID);
73
+
74
+ const uploadTime = ((Date.now() - startTime) / 1000).toFixed(2);
75
+
76
+ console.log('-'.repeat(60));
77
+ console.log('✅ 文件上传成功!');
78
+ console.log(`📁 云端文件名: ${result.name}`);
79
+ console.log(`🆔 文件ID: ${result.id}`);
80
+ console.log(`📊 文件大小: ${result.size ? (parseInt(result.size) / 1024).toFixed(2) + ' KB' : '未知'}`);
81
+ console.log(`📄 文件类型: ${result.mimeType}`);
82
+ console.log(`⏱️ 上传耗时: ${uploadTime} 秒`);
83
+ console.log(`📅 上传时间: ${new Date().toLocaleString('zh-CN')}`);
84
+
85
+ // 提供访问提示
86
+ console.log('\n💡 提示:');
87
+ console.log('- 可以使用文件ID来下载此文件');
88
+ console.log('- 使用 list-files.js 查看共享目录中的所有文件');
89
+
90
+ } catch (error) {
91
+ console.error('❌ 上传过程中发生错误:', error.message);
92
+
93
+ // 提供一些调试信息
94
+ if (error.message.includes('permission')) {
95
+ console.log('可能的原因:');
96
+ console.log('1. 服务账户没有写入该共享目录的权限');
97
+ console.log('2. 共享目录设置为只读');
98
+ console.log('3. 服务账户配额已用完');
99
+ } else if (error.message.includes('not found')) {
100
+ console.log('可能的原因:');
101
+ console.log('1. 目标文件夹ID不正确');
102
+ console.log('2. 共享目录已被删除或移动');
103
+ } else if (error.message.includes('file too large')) {
104
+ console.log('可能的原因:');
105
+ console.log('1. 文件大小超过了 Google Drive 的限制');
106
+ console.log('2. 服务账户的存储空间已用完');
107
+ }
108
+
109
+ process.exit(1);
110
+ }
111
+ }
112
+
113
+ main().catch(error => {
114
+ console.error('执行过程中发生错误:', error);
115
+ process.exit(1);
116
+ });