Browse Source

add check for blank file, parameterize bucket, refactor messages

pull/1/head
Trent Larson 7 months ago
parent
commit
bc05827656
  1. 2
      README.md
  2. 36
      server.js

2
README.md

@ -4,6 +4,8 @@ Remaining:
- parameterize bucket for test server - parameterize bucket for test server
- try American Cloud - try American Cloud
- dockerize - dockerize
- add file extension
- images.timesafari.app
- pretty-up the client, show thumbnail - pretty-up the client, show thumbnail

36
server.js

@ -19,6 +19,7 @@ app.use(cors());
const port = process.env.PORT || 3001; const port = process.env.PORT || 3001;
// file name also referenced in flyway.conf and potentially in .env files or in environment variables // file name also referenced in flyway.conf and potentially in .env files or in environment variables
const dbFile = process.env.SQLITE_FILE || './image-db.sqlite'; const dbFile = process.env.SQLITE_FILE || './image-db.sqlite';
const bucketName = process.env.AWS_BUCKET_NAME || 'gifts-image-test';
const ethrDidResolver = getResolver; const ethrDidResolver = getResolver;
const resolver = const resolver =
@ -51,6 +52,10 @@ const uploadMulter = multer({ dest: uploadDir + '/' });
// POST endpoint to upload an image // POST endpoint to upload an image
app.post('/image', uploadMulter.single('image'), async (req, res) => { app.post('/image', uploadMulter.single('image'), async (req, res) => {
const reqFile = req.file;
if (reqFile == null) {
return res.status(400).send(JSON.stringify({ success: false, message: 'No file uploaded.' }));
}
// Verify the JWT // Verify the JWT
try { try {
@ -62,7 +67,7 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
const verified = await didJwt.verifyJWT(jwt, { resolver }); const verified = await didJwt.verifyJWT(jwt, { resolver });
if (!verified.verified) { if (!verified.verified) {
const errorTime = new Date().toISOString(); const errorTime = new Date().toISOString();
console.log(errorTime, 'Got invalid JWT in Authorization header:', verified); console.error(errorTime, 'Got invalid JWT in Authorization header:', verified);
return res.status(401).send(JSON.stringify({ success: false, message: 'Got invalid JWT in Authorization header. See server logs at ' + errorTime })); return res.status(401).send(JSON.stringify({ success: false, message: 'Got invalid JWT in Authorization header. See server logs at ' + errorTime }));
} }
const issuerDid = verified.issuer; const issuerDid = verified.issuer;
@ -74,7 +79,7 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
[ issuerDid ], [ issuerDid ],
(dbErr, row) => { (dbErr, row) => {
if (dbErr) { if (dbErr) {
console.error('Error getting user record from database:', dbErr) console.error('Error getting user record from database (but continuing):', dbErr)
// may not matter, so continue // may not matter, so continue
} }
resolve(row?.per_week); resolve(row?.per_week);
@ -100,7 +105,7 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
[issuerDid, limitPerWeek], [issuerDid, limitPerWeek],
(dbErr) => { (dbErr) => {
if (dbErr) { if (dbErr) {
console.error("Error inserting user record for", issuerDid, "into database:", dbErr); console.error("Error inserting user record for", issuerDid, "into database (but continuing):", dbErr);
// we can continue... it just means we'll check the endorser server again next time // we can continue... it just means we'll check the endorser server again next time
} }
resolve(); resolve();
@ -123,7 +128,7 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
[issuerDid, startOfWeekString], [issuerDid, startOfWeekString],
(dbErr, row) => { (dbErr, row) => {
if (dbErr) { if (dbErr) {
console.error(currentDate, "Error counting records for", issuerDid, "into database:", dbErr); console.error(currentDate, "Error counting records for", issuerDid, "into database (but continuing):", dbErr);
// we can continue... it just means we'll check the endorser server again next time // we can continue... it just means we'll check the endorser server again next time
} }
resolve(row?.week_count); resolve(row?.week_count);
@ -135,27 +140,20 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
} }
// Read the file from the temporary location // Read the file from the temporary location
fs.readFile(req.file.path, async (err, data) => { fs.readFile(reqFile.path, async (err, data) => {
if (err) throw err; // Handle error if (err) throw err; // Handle error
const hashSum = crypto.createHash('sha256'); const hashSum = crypto.createHash('sha256');
hashSum.update(data); hashSum.update(data);
const hashHex = hashSum.digest('hex'); const hashHex = hashSum.digest('hex');
const bucketName = 'gifts-image';
const fileName = hashHex; const fileName = hashHex;
const params = {
Body: data,
Bucket: bucketName, // S3 Bucket name
ContentType: req.file.mimetype, // File content type
Key: fileName, // File name to use in S3
};
try { try {
// record the upload in the database // record the upload in the database
const currentDate = new Date().toISOString(); const currentDate = new Date().toISOString();
const localFile = req.file.path.startsWith(uploadDir + '/') ? req.file.path.substring(uploadDir.length + 1) : req.file.path; const localFile = reqFile.path.startsWith(uploadDir + '/') ? reqFile.path.substring(uploadDir.length + 1) : reqFile.path;
const finalUrl = `https://${bucketName}.s3.amazonaws.com/${fileName}`; const finalUrl = `https://${bucketName}.s3.amazonaws.com/${fileName}`;
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
db.run( db.run(
@ -164,7 +162,7 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
currentDate, currentDate,
issuerDid, issuerDid,
localFile, localFile,
req.file.size, reqFile.size,
fileName, fileName,
finalUrl finalUrl
], ],
@ -180,6 +178,12 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
}); });
// send to AWS // send to AWS
const params = {
Body: data,
Bucket: bucketName, // S3 Bucket name
ContentType: reqFile.mimetype, // File content type
Key: fileName, // File name to use in S3
};
const command = new PutObjectCommand(params); const command = new PutObjectCommand(params);
const response = await s3Client.send(command); const response = await s3Client.send(command);
if (response.$metadata.httpStatusCode !== 200) { if (response.$metadata.httpStatusCode !== 200) {
@ -190,9 +194,9 @@ app.post('/image', uploadMulter.single('image'), async (req, res) => {
message: "Got bad status of " + response.$metadata.httpStatusCode + " from AWS. See server logs at " + errorTime message: "Got bad status of " + response.$metadata.httpStatusCode + " from AWS. See server logs at " + errorTime
})); }));
} else { } else {
fs.rm(req.file.path, (err) => { fs.rm(reqFile.path, (err) => {
if (err) { if (err) {
console.error("Error deleting temp file", req.file.path, "with error:", err); console.error("Error deleting temp file", reqFile.path, "with error (but continuing):", err);
} }
}); });
res.send(JSON.stringify({success: true, url: finalUrl})); res.send(JSON.stringify({success: true, url: finalUrl}));

Loading…
Cancel
Save