gd-utils/copy

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