This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpapi.js
174 lines (151 loc) · 4.55 KB
/
npapi.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* eslint no-console:0, no-unused-vars:0, no-undef:0 */
const dotenv = require('dotenv')
const aws = require('aws-sdk')
const WPAPI = require('wpapi')
const axios = require('axios')
const express = require('express')
const fs = require('fs')
const crypto = require('crypto')
const S3ReadableStream = require('s3-readable-stream')
const winston = require('winston')
const winstonExRegLogger = require('winston-express-request-logger')
dotenv.config({ path: 'env' })
const host = process.env.HOST
const app = express()
const s3 = new aws.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'ap-southeast-2'
})
const gopressId = process.env.GOPRESS_ID
const gopressName = process.env.GOPRESS_NAME
const gopressToken = process.env.GOPRESS_TOKEN
winstonExRegLogger.createLogger({
transports: [
new (winston.transports.Console)({
handleExceptions: true,
timestamp: true,
level: 'info'
})
],
exitOnError: false
})
app.listen(3333)
app.use(winstonExRegLogger.requestDetails)
app.set('trust proxy', true)
app.get('/', (req, res) => {
res.json({ path: '/', status: 'okay' })
})
app.get('/api/v1/posts/:id/:token', (req, res) => {
http = axios.create({
baseURL: `https://${host}.com.au/api/v4`,
headers: { Authorization: `Bearer ${req.params['token']}` }
})
collectPost(req.params['id'])
res.json({ id: req.params['id'], token: req.params['token'] })
})
console.log(`got ${gopressId}`)
console.log(`npapi up for ${host}`)
// private methods outside main loop
const decrypt = (key, ciphertext) => {
key = Buffer.from(key, 'hex')
ciphertext = Buffer.from(ciphertext, 'base64')
const aesgcm = crypto.createDecipheriv('aes-256-gcm', key, ciphertext.slice(0, 12))
aesgcm.setAuthTag(ciphertext.slice(-16))
const plaintext = aesgcm.update(ciphertext.slice(12, -16)) + aesgcm.final()
return plaintext
}
const collectPost = (id) => {
http.get(`/scheduled_posts/${id}`)
.then(res => res.data.scheduled_post)
.then(post => {
const api = buildApi(post)
const res = run(id, api, post)
return res
})
.catch(err => console.log(err))
}
const updatePost = (id, wordpressRes) => {
http.put(`/scheduled_posts/${id}`, { post: { provider_response: wordpressRes } })
.then(res => console.log(res))
.catch(err => console.log(err))
}
const buildApi = (post) => {
const id = decrypt(gopressId, post.authorisation.gopress_id_ciphertext)
const name = decrypt(gopressName, post.authorisation.gopress_name_ciphertext)
const token = decrypt(gopressToken, post.authorisation.gopress_token_ciphertext)
const api = new WPAPI({
endpoint: `${id}/wp-json`,
username: name,
password: token
})
return api
}
const createPost = (id, api, mediaId, post, tags) => {
api.posts()
.create({
title: post.document.title,
content: post.document.content,
excerpt: post.document.excerpt,
comment_status: 'open',
status: post.draft === true ? 'draft' : 'publish',
featured_media: mediaId,
tags: tags
})
.then(res => {
console.log(res)
updatePost(id, res)
return res
})
.catch(err => console.log(err))
}
const createTags = (id, api, local, post) => {
const remoteTags = []
for (const tag of post.document.tags) {
api.tags()
.create({ name: tag })
.then(res => {
console.log(`good tags: ${res}`)
remoteTags.push(res.id)
})
.catch(err => {
console.log(err)
if (err.code === 'term_exists') remoteTags.push(err.data.term_id)
})
}
createMedia(id, api, local, post, remoteTags)
}
const createMedia = (id, api, local, post, tags) => {
api.media().setHeaders('Content-Disposition', 'inline')
.file(local)
.create({
title: `Featured image for ${post.document.title}`
})
.then(res => {
console.log(`good media: ${res}`)
const mediaId = res.id
createPost(id, api, mediaId, post, tags)
return res
})
.catch(err => console.log(err))
}
const run = (id, api, post) => {
const path = `./tmp/${post.filename}`
const dest = fs.createWriteStream(path)
const params = { Bucket: post.bucket, Key: post.key }
const stream = new S3ReadableStream(s3, params)
stream
.on('error', (err) => console.log(err))
.on('open', (data) => console.log(data))
.on('close', () => {
console.log(`close`)
createTags(id, api, path, post)
return dest
})
.on('end', () => {
console.log(`end`)
createTags(id, api, path, post)
return dest
})
stream.pipe(dest)
}