forked from inventid/iaas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21da2a8
commit ade84e0
Showing
13 changed files
with
190 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import AWS from "aws-sdk"; | ||
import config from "config"; | ||
|
||
// The AWS config needs to be set before this object is created | ||
AWS.config.update({ | ||
accessKeyId: config.get('aws.access_key'), | ||
secretAccessKey: config.get('aws.secret_key'), | ||
region: config.get('aws.region') | ||
}); | ||
const S3 = new AWS.S3(); | ||
export default S3; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import parsing from './UrlParsing'; | ||
import log from './Log'; | ||
|
||
function logRequest(req, res, time) { | ||
const remoteIp = req.headers['x-forwarded-for'] || req.ip; | ||
const obj = { | ||
datetime: Date.now(), | ||
method: req.method, | ||
url: req.url, | ||
client: remoteIp, | ||
response_time: (time / 1e3), | ||
response_status: res.statusCode | ||
}; | ||
if (parsing.isGetRequest(req)) { | ||
if (res.statusCode === 200) { | ||
obj.cache_hit = false; | ||
} else if (res.statusCode === 307) { | ||
obj.cache_hit = true; | ||
} | ||
try { | ||
const params = parsing.getImageParams(req); | ||
for (let param in params) { | ||
if (params.hasOwnProperty(param)) { | ||
obj[param] = params[param]; | ||
} | ||
} | ||
} catch (e) { | ||
log.log('info', `Could not extract image parameters, might not have been an image request: ${req.url}`); | ||
} | ||
} | ||
log.log('debug', JSON.stringify(obj)); | ||
} | ||
|
||
export default logRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import uuid from "node-uuid"; | ||
import log from "./Log"; | ||
|
||
/*eslint-disable quotes*/ | ||
const insertToken = `INSERT INTO tokens (id, image_id, valid_until, used) VALUES ($1,$2,now() + interval '15 minute', 0)`; | ||
const consumeToken = `UPDATE tokens SET used=1 WHERE id=$1 AND image_id=$2 AND valid_until >= now() AND used=0`; | ||
const deleteOldTokens = `DELETE FROM tokens WHERE valid_until < datetime('now') AND used=0`; | ||
/*eslint-enable*/ | ||
|
||
const Token = (db) => { | ||
const module = { | ||
consume(token, id, callback) { | ||
db.query(consumeToken, [token, id], (err, res) => { | ||
callback(err, res); | ||
}); | ||
}, | ||
create(req, res) { | ||
// Here we create a token which is valid for one single upload | ||
// This way we can directly send the file here and just a small json payload to the app | ||
const newToken = uuid.v4(); | ||
if (!req.body.id) { | ||
res.writeHead(400, 'Bad request'); | ||
res.end(); | ||
return; | ||
} | ||
// Ensure the id wasnt requested or used previously | ||
db.query(insertToken, [newToken, req.body.id], (err) => { | ||
if (!err) { | ||
res.json({token: newToken}).end(); | ||
log.log('info', 'Created token successfully'); | ||
if (module.shouldRunCleanup()) { | ||
module.cleanup(); | ||
} | ||
} else { | ||
res.statusCode = 403; | ||
res.json({error: 'The requested image_id is already requested'}).end(); | ||
} | ||
}); | ||
}, | ||
shouldRunCleanup() { | ||
return Math.floor(Math.random() * 10) === 0; | ||
}, | ||
cleanup() { | ||
log.log('info', 'Doing a token cleanup'); | ||
db.query(deleteOldTokens, [], (err) => { | ||
if (!err) { | ||
log.log('info', `Cleaned ${this.changes} tokens from the db`); | ||
} else { | ||
log.log('error', `Encountered error ${err} when cleaning up tokens`); | ||
} | ||
}); | ||
} | ||
}; | ||
return module; | ||
}; | ||
|
||
export default Token; |
File renamed without changes.
Oops, something went wrong.