diff --git a/server.js b/server.js index 02758a7..08954d5 100644 --- a/server.js +++ b/server.js @@ -11,10 +11,11 @@ require('dotenv').config() const app = express(); app.use(cors()); -const port = 3000; +const port = process.env.PORT || 3000; +const dbFile = process.env.SQLITE_FILE || './sqlite-db.sqlite'; // Open a connection to the SQLite database -const db = new sqlite3.Database('sqlite-db.sqlite', (err) => { +const db = new sqlite3.Database(dbFile, (err) => { if (err) { console.error('Error opening database:', err); } @@ -29,11 +30,11 @@ const s3Client = new S3Client({ } }); -const UPLOAD_DIR = 'uploads'; -const upload = multer({ dest: UPLOAD_DIR + '/' }); +const uploadDir = 'uploads'; +const uploadMulter = multer({ dest: uploadDir + '/' }); // POST endpoint to upload an image -app.post('/image', upload.single('image'), (req, res) => { +app.post('/image', uploadMulter.single('image'), (req, res) => { const file = req.file; // Read the file from the temporary location @@ -55,6 +56,25 @@ app.post('/image', upload.single('image'), (req, res) => { // Upload the file to S3 try { + + // record the upload in the database + const currentDate = new Date().toISOString(); + const localFile = file.path.startsWith(uploadDir + '/') ? file.path.substring(uploadDir.length + 1) : file.path; + const finalUrl = `https://${bucketName}.s3.amazonaws.com/${fileName}`; + db.run('INSERT INTO image (date, did, local_file, size, aws_file, url) VALUES (?, ?, ?, ?, ?, ?)', [ + currentDate, + "UNKNOWN", + localFile, + file.size, + fileName, + finalUrl + ], (dbErr) => { + if (dbErr) { + console.error(currentDate, "Error inserting record from", "UNKNOWN", "into database:", dbErr); + } + }); + + // send to AWS const command = new PutObjectCommand(params); const response = await s3Client.send(command); if (response.$metadata.httpStatusCode !== 200) { @@ -62,27 +82,11 @@ app.post('/image', upload.single('image'), (req, res) => { console.error(errorTime, "Error uploading to S3 with bad HTTP status. Metadata:", response.$metadata); res.status(500).send(JSON.stringify({ success: false, message: "Got bad status of " + response.$metadata.httpStatusCode + " from AWS. See server logs at " + errorTime })); } else { - const finalUrl = `https://${bucketName}.s3.amazonaws.com/${fileName}`; fs.rm(file.path, (err) => { if (err) { console.error("Error deleting temp file", file.path, "with error:", err); } }); - // Record the upload in the database - const currentDate = new Date().toISOString(); - const localFile = file.path.startsWith(UPLOAD_DIR + '/') ? file.path.substring(UPLOAD_DIR.length + 1) : file.path; - db.run('INSERT INTO image (date, did, local_file, size, aws_file, url) VALUES (?, ?, ?, ?, ?, ?)', [ - currentDate, - "UNKNOWN", - localFile, - file.size, - fileName, - finalUrl - ], (dbErr) => { - if (dbErr) { - console.error(currentDate, "Error inserting record from", "UNKNOWN", "into database:", dbErr); - } - }); res.send(JSON.stringify({ success: true, url: finalUrl })); } } catch (uploadError) { @@ -103,7 +107,6 @@ process.on('SIGINT', () => { if (err) { return console.error('Error closing DB connection', err); } - console.log('Closed DB connection.'); process.exit(0); }); });