-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
119 lines (114 loc) · 3.49 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
const login = require('./login')
const prompts = require('prompts')
const qs = require('querystring')
const mime = require('mime-types')
const { CookieJar } = require('tough-cookie')
const got = require('got')
const FormData = require('form-data')
const cheerio = require('cheerio')
const fs = require('fs-extra')
const path = require('path')
if (process.argv.length < 3) {
console.error(`Usage: gupl <filepath>`)
process.exit(1)
}
const ALLOWED_FILETYPES = ['gif', 'jpg', 'jpeg', 'png', 'docx', 'gz', 'log', 'pdf', 'pptx', 'txt', 'xlsx', 'zip']
const REPOURL = 'https://github.com/isaacs/github/issues/new'
const COOKIEFILE = path.join(__dirname, 'cookie.json')
const FILEPATH = path.join(process.cwd(), process.argv[2])
const isFileAllowed = ALLOWED_FILETYPES.some(ext => FILEPATH.toLowerCase().endsWith(ext))
if (!isFileAllowed) {
console.log('Your file is not allowed to upload to GitHub.')
console.log(`Only ${ALLOWED_FILETYPES.join(', ')} are allowed.`)
process.exit(1)
}
const readCookie = async () => {
if (await fs.exists(COOKIEFILE)) {
// if cookie already exists
return CookieJar.fromJSON(await fs.readFile(COOKIEFILE, 'utf-8'))
}
const questions = [
{
type: 'text',
name: 'username',
message: 'What is your GitHub username?',
validate: v => !!v
},
{
type: 'password',
name: 'password',
message: 'What is your GitHub password?',
validate: v => !!v
},
{
type: 'confirm',
name: 'has2fa',
message: 'Do you have 2fa enabled?'
},
{
type: has2fa => (has2fa ? 'text' : null),
name: 'otp',
message: 'Please enter current otp here:',
validate: v => !!v && /^\d{6}$/.test(v)
}
]
const r = await prompts(questions)
const cookie = await login(r.username, r.password, r.otp)
await fs.writeFile(COOKIEFILE, JSON.stringify(cookie.toJSON()))
console.log(`Your GitHub login credentials(cookies) have been saved to "${COOKIEFILE}".`)
console.log('To delete it, you can execute "gupl-reset" or "ghfileupl-reset".')
return cookie
}
;(async () => {
const cookieJar = await readCookie()
const client = got.extend({
cookieJar,
followRedirect: false,
hooks: {
beforeRequest: opts => {
//console.log(opts)
if (opts.form) {
opts.body = qs.stringify(opts.form)
opts.headers['Content-Length'] = opts.body.length
}
}
}
})
const $ = await client.get(REPOURL).then(r => cheerio.load(r.body))
const stat = await fs.stat(FILEPATH)
try {
const res = await client
.post('https://github.com/upload/policies/assets', {
form: {
name: path.basename(FILEPATH),
size: stat.size,
content_type: mime.lookup(FILEPATH),
authenticity_token: $('.js-upload-markdown-image').data('upload-policy-authenticity-token'),
repository_id: parseInt($('meta[name="octolytics-dimension-repository_id"]').attr('content'))
}
})
.then(r => JSON.parse(r.body))
.catch(e => {
throw JSON.parse(e.body)
})
const fd = new FormData()
for (const [k, v] of Object.entries(res.form)) {
fd.append(k, v)
}
fd.append('file', fs.createReadStream(FILEPATH))
await client.post(res.upload_url, { body: fd })
const fd2 = new FormData()
fd2.append('authenticity_token', res.asset_upload_authenticity_token)
const result = await client
.put('https://github.com' + res.asset_upload_url, { headers: { Accept: 'application/json' }, body: fd2 })
.then(r => JSON.parse(r.body))
console.log(result)
} catch (e) {
if (e.errors) {
console.error(e.errors)
} else {
console.error(e)
}
}
})().catch(console.error)