diff --git a/.env.example b/.env.example index 95ae791..d93a5c2 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,7 @@ AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_S3_BUCKET= +DISABLE_S3=false MYSQL_USER=root MYSQL_PASSWORD=secret diff --git a/pages/index.vue b/pages/index.vue index 5443ab1..1b34fef 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -53,6 +53,9 @@ Tweet about your contribution diff --git a/server/controllers/contributions.js b/server/controllers/contributions.js index e156ceb..9acadb9 100644 --- a/server/controllers/contributions.js +++ b/server/controllers/contributions.js @@ -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') diff --git a/server/models/contributions.model.js b/server/models/contributions.model.js index ed350c6..d02fe4c 100644 --- a/server/models/contributions.model.js +++ b/server/models/contributions.model.js @@ -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 \ No newline at end of file +module.exports = Contributions diff --git a/server/snark_files/current.params b/server/snark_files/current.params index b089a9a..c8960fe 100644 Binary files a/server/snark_files/current.params and b/server/snark_files/current.params differ