This commit is contained in:
Alexey 2020-01-30 17:00:34 +03:00
parent 0a261626c8
commit 5a51b659b5
5 changed files with 42 additions and 38 deletions

View File

@ -1,6 +1,7 @@
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_S3_BUCKET=
DISABLE_S3=false
MYSQL_USER=root
MYSQL_PASSWORD=secret

View File

@ -53,6 +53,9 @@
<b-button
v-if="isContributeBtnDisabled && status.type === 'is-success'"
type="is-primary"
tag="a"
href="https://twitter.com/intent/tweet?text=Hello%20world"
target="_blank"
outlined
>
Tweet about your contribution

View File

@ -1,28 +1,30 @@
// const aws = require('aws-sdk')
// const s3 = new aws.S3()
const fs = require('fs').promises
const path = require('path')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const aws = require('aws-sdk')
const express = require('express')
const router = express.Router()
const { Mutex } = require('async-mutex')
const mutex = new Mutex()
const multer = require('multer')
const mutex = new Mutex()
const s3 = new aws.S3()
const router = express.Router()
const Contribution = require('../models/contributions.model.js')
const upload = multer({ dest: '/tmp/tornado' })
// async function uploadToS3(response) {
// const currentContributionIndex = await Contribution.currentContributionIndex()
// return await s3
// .upload({
// Bucket: process.env.AWS_S3_BUCKET,
// Key: `response_${currentContributionIndex}`,
// ACL: 'public-read',
// Body: response
// })
// .promise()
// }
async function uploadToS3({ filename }) {
const currentContributionIndex = await Contribution.currentContributionIndex()
const fileContent = await fs.readFile(`/tmp/tornado/${filename}`)
return s3
.upload({
Bucket: process.env.AWS_S3_BUCKET,
Key: `response_${currentContributionIndex}`,
ACL: 'public-read',
Body: fileContent
})
.promise()
}
async function verifyResponse({ filename }) {
console.log('Running verifier')
@ -56,7 +58,6 @@ router.post('/response', upload.single('response'), async (req, res) => {
const currentContributionIndex = await Contribution.currentContributionIndex()
try {
console.log(`Started processing contribution ${currentContributionIndex}`)
// await fs.writeFile('/tmp/new.params', req.file.response.data)
await verifyResponse({ filename: req.file.filename })
} catch (e) {
console.error('Error', e)
@ -66,11 +67,9 @@ router.post('/response', upload.single('response'), async (req, res) => {
try {
console.log('Contribution is correct, uploading to storage')
// await uploadToS3(req.files.response.data)
// await fs.copyFile(
// '/tmp/new.params',
// `./snark_files/response_${currentContributionIndex}`
// )
if (process.env.DISABLE_S3 !== 'true') {
await uploadToS3({ filename: req.file.filename })
}
console.log('Committing changes')
await fs.rename(`/tmp/tornado/${req.file.filename}`, './server/snark_files/current.params')

View File

@ -1,40 +1,41 @@
const db = require('./db.js')
const crypto = require('crypto')
const db = require('./db.js')
let sql
const Contributions = { }
const Contributions = {}
Contributions.currentContributionIndex = async function() {
const [rows,] = await sql.query('select max(id) as max from contributions')
const [rows] = await sql.query('select max(id) as max from contributions')
return (rows[0].max || 0) + 1
}
Contributions.insertContributionInfo = async function(name, company) {
const token = crypto.randomBytes(32).toString('hex')
await sql.execute(
'insert into contributions(token, name, company) values(?, ?, ?)',
[token, name, company]
)
await sql.execute('insert into contributions(token, name, company) values(?, ?, ?)', [
token,
name,
company
])
}
Contributions.updateContributionInfo = async function(token, name, company) {
await sql.execute(
'insert into contributions(token, name, company) values(?, ?, ?)',
[token, name, company]
)
await sql.execute('insert into contributions(token, name, company) values(?, ?, ?)', [
token,
name,
company
])
}
Contributions.getContributions = async function() {
const [rows,] = await db.execute('select id, name, company from contributions')
const [rows] = await db.execute('select id, name, company from contributions')
return rows
}
async function main () {
({ sql } = await db())
async function main() {
;({ sql } = await db())
const contribitionIndex = await Contributions.currentContributionIndex()
console.log('Next contribution index is', contribitionIndex)
}
main()
module.exports = Contributions
module.exports = Contributions

Binary file not shown.