-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
47 lines (38 loc) · 1.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {relative} from 'node:path'
import {stat} from 'node:fs/promises'
import {createReadStream} from 'node:fs'
import 'dotenv/config'
import qiniu from 'qiniu'
import glob from '@actions/glob'
import core from '@actions/core'
const accessKey = core.getInput('ACCESS_KEY')
const secretKey = core.getInput('SECRET_KEY')
const bucket = core.getInput('BUCKET')
const sourceDir = core.getInput('SOURCE_DIR')
const destDir = core.getInput('DEST_DIR')
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const uploader = new qiniu.form_up.FormUploader()
!async function() {
const globber = await glob.create([
`${sourceDir}/*`
].join('\n'))
for await (const file of globber.globGenerator()) {
const stats = await stat(file)
if (stats.isDirectory()) continue
upload(file)
}
}()
function upload(file: string) {
new Promise<void>(resolve => {
const key = `${destDir}/${relative(sourceDir, file)}`
const policy = new qiniu.rs.PutPolicy({
scope: `${bucket}:${key}`,
})
const token = policy.uploadToken(mac)
uploader.putStream(token, key, createReadStream(file), null, (err) => {
resolve()
if (err) console.log(err.message)
console.log(`ok: ${key}`)
})
})
}