gd-utils/copy

54 lines
2.4 KiB
Plaintext
Raw Normal View History

2020-06-27 14:19:43 +08:00
#!/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')
2020-07-07 20:41:02 +08:00
.describe('u', '不使用本地快取,則無視快取記錄強制從線上獲取源資料夾資訊')
2020-07-30 17:43:16 +08:00
.alias('y', 'yes')
.describe('yes', '如果發現拷貝紀錄,直接繼續上次的進度')
2020-06-27 14:19:43 +08:00
.alias('f', 'file')
2020-07-07 20:41:02 +08:00
.describe('f', '複製單一文件')
2020-06-27 14:19:43 +08:00
.alias('n', 'name')
2020-07-07 20:41:02 +08:00
.describe('n', '給目標資料夾重新命名,不填則保留原始目錄名')
2020-06-27 14:19:43 +08:00
.alias('N', 'not_teamdrive')
2020-07-07 20:41:02 +08:00
.describe('N', '如果不是小組雲端硬碟連結,可以加上此参數以提高接口查詢效率,降低延遲')
2020-06-27 14:19:43 +08:00
.alias('s', 'size')
2020-07-07 20:41:02 +08:00
.describe('s', '不填則預設拷貝全部文件如果設置了这個值則過濾掉小於这個大小的檔案必須以b為結尾比如10mb')
2020-06-27 14:19:43 +08:00
.alias('S', 'service_account')
2020-07-07 20:41:02 +08:00
.describe('S', '指定使用service account進行操作前提是必須在 ./sa 目錄下放置json授權文件請確認sa帳號擁有相關操作權限。')
.alias('D', 'dncnr')
2020-07-07 20:41:02 +08:00
.describe('D', 'do not create new root, 不在目的地創建同名資料夾,直接將來源資料夾中的文件原樣複製到目的資料夾中')
2020-06-27 14:19:43 +08:00
.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, dncnr } = argv
2020-06-27 14:19:43 +08:00
if (file) {
target = target || DEFAULT_TARGET
if (!validate_fid(target)) throw new Error('target id 格式不正确')
return copy_file(source, target, service_account).then(r => {
2020-06-27 14:19:43 +08:00
const link = 'https://drive.google.com/drive/folders/' + target
2020-07-07 20:41:02 +08:00
console.log('任務完成,文件所在位置:\n', link)
2020-06-27 14:19:43 +08:00
}).catch(console.error)
}
let min_size
if (size) {
2020-07-07 20:41:02 +08:00
console.log(`不複製大小低於 ${size} 的文件`)
2020-06-27 14:19:43 +08:00
min_size = bytes.parse(size)
}
copy({ source, target, name, min_size, update, not_teamdrive, service_account, dncnr }).then(folder => {
2020-06-27 14:19:43 +08:00
if (!folder) return
const link = 'https://drive.google.com/drive/folders/' + folder.id
2020-07-07 20:41:02 +08:00
console.log('\n任務完成新資料夾連結\n', link)
2020-06-27 14:19:43 +08:00
})
} else {
2020-07-07 20:41:02 +08:00
console.warn('無目錄ID或格式錯誤')
2020-06-27 14:19:43 +08:00
}