-
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
dd38a68
commit baed6a2
Showing
5 changed files
with
269 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
const { MongoClient } = require('mongodb'); | ||
|
||
if (!process.env.DOCKER_ENV) { | ||
require('dotenv').config(); | ||
} | ||
|
||
// Construct the MongoDB URI using environment variables | ||
const uri = `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}` | ||
+ `@${process.env.MONGODB_HOST}:${process.env.MONGODB_PORT}/?authSource=admin`; | ||
|
||
const client = new MongoClient(uri); | ||
let dbConnection = null; | ||
|
||
module.exports = { | ||
connectToServer() { | ||
return new Promise((resolve, reject) => { | ||
client.connect() | ||
.then(() => { | ||
console.log("Connected successfully to MongoDB server"); | ||
// Connect to the log database | ||
dbConnection = client.db(process.env.MONGODB_DB); | ||
resolve({ dbConnection }); | ||
}) | ||
.catch((err) => { | ||
console.error("Failed to connect to MongoDB server:", err); | ||
reject(err); | ||
}); | ||
}); | ||
}, | ||
getDb() { | ||
return dbConnection; | ||
}, | ||
closeConnection() { | ||
console.log("Closing connection to MongoDB server"); | ||
client.close(); | ||
}, | ||
}; |
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,78 @@ | ||
const mongoUtil = require('./db'); | ||
|
||
let dbConnection = null; | ||
|
||
mongoUtil.connectToServer() | ||
.then(({ dbConnection: localDbConnection }) => { | ||
console.log("Successfully connected to MongoDB."); | ||
|
||
// Create a unique index for 'userId' | ||
localDbConnection.collection('imageUpload').createIndex({ userId: 1 }, { unique: true }) | ||
.then(() => console.log("Unique index created successfully on 'userId'")) | ||
.catch(err => console.error("Error creating unique index on 'userId':", err)); | ||
|
||
// Create a compound index for 'fileName' and 'createdAt' without enforcing uniqueness | ||
localDbConnection.collection('imageUpload').createIndex({ userId: 1, fileName: 1, createdAt: -1 }, { unique: false }) | ||
.then(() => console.log("Index created successfully on 'fileName' and 'createdAt'")) | ||
.catch(err => console.error("Error creating index on 'fileName' and 'createdAt':", err)); | ||
|
||
// Assign the database connections to the variables declared at higher scope | ||
dbConnection = localDbConnection; | ||
}) | ||
.catch(err => { | ||
console.error("Failed to connect to MongoDB:", err); | ||
}); | ||
|
||
// Authenticate with B2 | ||
async function authenticateB2() { | ||
try { | ||
await b2.authorize(); | ||
} catch (error) { | ||
console.error('B2 Authorization error:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
// Upload file to B2 | ||
async function uploadToB2(filePath, fileName) { | ||
try { | ||
await authenticateB2(); | ||
const uploadUrl = await b2.getUploadUrl({ | ||
bucketId: process.env.B2_BUCKET_ID | ||
}); | ||
|
||
const response = await b2.uploadFile({ | ||
uploadUrl: uploadUrl.data.uploadUrl, | ||
uploadAuthToken: uploadUrl.data.authorizationToken, | ||
filename: fileName, | ||
data: Buffer.from(require('fs').readFileSync(filePath)) | ||
}); | ||
|
||
return response.data; | ||
} catch (error) { | ||
console.error('B2 upload error:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function logUploadDetails(userId, fileName) { | ||
try { | ||
const collection = dbConnection.collection('imageUpload'); | ||
|
||
const logEntry = { | ||
userId: userId, | ||
fileName: fileName, | ||
createdAt: new Date() // Record the current timestamp | ||
}; | ||
|
||
await collection.insertOne(logEntry); | ||
console.log('Upload logged successfully:', logEntry); | ||
} catch (error) { | ||
console.error('Error logging upload to MongoDB:', error); | ||
} | ||
} | ||
|
||
module.exports = { | ||
uploadToB2, | ||
logUploadDetails | ||
}; |
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