@ -11,10 +11,11 @@ require('dotenv').config()
const app = express ( ) ;
const app = express ( ) ;
app . use ( cors ( ) ) ;
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
// 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 ) {
if ( err ) {
console . error ( 'Error opening database:' , err ) ;
console . error ( 'Error opening database:' , err ) ;
}
}
@ -29,11 +30,11 @@ const s3Client = new S3Client({
}
}
} ) ;
} ) ;
const UPLOAD_DIR = 'uploads' ;
const uploadDir = 'uploads' ;
const upload = multer ( { dest : UPLOAD_DIR + '/' } ) ;
const uploadMulter = multer ( { dest : uploadDir + '/' } ) ;
// POST endpoint to upload an image
// 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 ;
const file = req . file ;
// Read the file from the temporary location
// Read the file from the temporary location
@ -55,22 +56,11 @@ app.post('/image', upload.single('image'), (req, res) => {
// Upload the file to S3
// Upload the file to S3
try {
try {
const command = new PutObjectCommand ( params ) ;
const response = await s3Client . send ( command ) ;
// record the upload in the database
if ( response . $metadata . httpStatusCode !== 200 ) {
const errorTime = new Date ( ) . toISOString ( ) ;
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 currentDate = new Date ( ) . toISOString ( ) ;
const localFile = file . path . startsWith ( UPLOAD_DIR + '/' ) ? file . path . substring ( UPLOAD_DIR . length + 1 ) : file . path ;
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 (?, ?, ?, ?, ?, ?)' , [
db . run ( 'INSERT INTO image (date, did, local_file, size, aws_file, url) VALUES (?, ?, ?, ?, ?, ?)' , [
currentDate ,
currentDate ,
"UNKNOWN" ,
"UNKNOWN" ,
@ -83,6 +73,20 @@ app.post('/image', upload.single('image'), (req, res) => {
console . error ( currentDate , "Error inserting record from" , "UNKNOWN" , "into database:" , 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 ) {
const errorTime = new Date ( ) . toISOString ( ) ;
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 {
fs . rm ( file . path , ( err ) => {
if ( err ) {
console . error ( "Error deleting temp file" , file . path , "with error:" , err ) ;
}
} ) ;
res . send ( JSON . stringify ( { success : true , url : finalUrl } ) ) ;
res . send ( JSON . stringify ( { success : true , url : finalUrl } ) ) ;
}
}
} catch ( uploadError ) {
} catch ( uploadError ) {
@ -103,7 +107,6 @@ process.on('SIGINT', () => {
if ( err ) {
if ( err ) {
return console . error ( 'Error closing DB connection' , err ) ;
return console . error ( 'Error closing DB connection' , err ) ;
}
}
console . log ( 'Closed DB connection.' ) ;
process . exit ( 0 ) ;
process . exit ( 0 ) ;
} ) ;
} ) ;
} ) ;
} ) ;