gd-utils/copy

50 lines
2.0 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const bytes = require('bytes')
const { argv } = require('yargs')
.usage('用法: ./$0 <source id> <target id> [options]\ntarget id可选不填则使用config.js里的DEFAULT_TARGET')
.alias('u', 'update')
.describe('u', '不使用本地缓存,强制从线上获取源文件夹信息')
.alias('f', 'file')
.describe('f', '复制单个文件')
.alias('n', 'name')
.describe('n', '给目标文件夹重命名,不填则保留原始目录名')
.alias('N', 'not_teamdrive')
.describe('N', '如果不是团队盘链接,可以加上此参数以提高接口查询效率,降低延迟')
.alias('s', 'size')
.describe('s', '不填默认拷贝全部文件如果设置了这个值则过滤掉小于这个size的文件必须以b结尾比如10mb')
.alias('S', 'service_account')
.describe('S', '指定使用service account进行操作前提是必须在 ./sa 目录下放置json授权文件请确保sa帐号拥有操作权限。')
.help('h')
.alias('h', 'help')
const { copy, copy_file, validate_fid } = require('./src/gd')
const { DEFAULT_TARGET } = require('./config')
let [source, target] = argv._
if (validate_fid(source)) {
const { name, update, file, not_teamdrive, size, service_account } = argv
if (file) {
target = target || DEFAULT_TARGET
if (!validate_fid(target)) throw new Error('target id 格式不正确')
return copy_file(source, target).then(r => {
const link = 'https://drive.google.com/drive/folders/' + target
console.log('任务完成,文件所在位置:\n', link)
}).catch(console.error)
}
let min_size
if (size) {
console.log(`不复制大小低于 ${size} 的文件`)
min_size = bytes.parse(size)
}
copy({ source, target, name, min_size, update, not_teamdrive, service_account }).then(folder => {
if (!folder) return
const link = 'https://drive.google.com/drive/folders/' + folder.id
console.log('\n任务完成新文件夹链接\n', link)
})
} else {
console.warn('目录ID缺失或格式错误')
}