EvalueTonSavoir/server/models/images.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

const db = require('../config/db.js')
2024-03-29 20:08:34 -04:00
const { ObjectId } = require('mongodb');
class Images {
constructor(db) {
this.db = db;
}
2024-03-29 20:08:34 -04:00
async upload(file, userId) {
await db.connect()
const conn = db.getConnection();
const imagesCollection = conn.collection('images');
const newImage = {
userId: userId,
file_name: file.originalname,
file_content: file.buffer.toString('base64'),
mime_type: file.mimetype,
created_at: new Date()
};
const result = await imagesCollection.insertOne(newImage);
return result.insertedId;
}
async get(id) {
await db.connect()
const conn = db.getConnection();
const imagesCollection = conn.collection('images');
2024-10-03 12:05:20 -04:00
const result = await imagesCollection.findOne({ _id: ObjectId.createFromHexString(id) });
2024-03-29 20:08:34 -04:00
if (!result) return null;
return {
file_name: result.file_name,
file_content: Buffer.from(result.file_content, 'base64'),
mime_type: result.mime_type
};
}
}
module.exports = Images;