EvalueTonSavoir/server/models/image.js
2025-03-15 14:53:37 -04:00

47 lines
1.1 KiB
JavaScript

const { ObjectId } = require('mongodb');
class Image {
constructor(db) {
this.db = db;
}
async upload(file, userId) {
const conn = await this.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) {
const conn = await this.db.getConnection();
const imagesCollection = conn.collection('images');
const result = await imagesCollection.findOne({ _id: ObjectId.createFromHexString(id) });
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 = Image;