2024-03-29 20:08:34 -04:00
|
|
|
const { ObjectId } = require('mongodb');
|
|
|
|
|
|
|
|
|
|
class Images {
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(db) {
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
async upload(file, userId) {
|
2025-01-11 00:03:05 -05:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
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) {
|
2025-01-11 00:03:05 -05:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 20:26:30 -05:00
|
|
|
async getImages(page, limit) {
|
2025-02-22 17:56:53 -05:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
|
|
|
|
|
|
|
|
|
const imagesCollection = conn.collection('images');
|
|
|
|
|
|
2025-03-04 19:49:18 -05:00
|
|
|
|
2025-03-11 19:50:28 -04:00
|
|
|
const total = await imagesCollection.countDocuments();
|
2025-03-04 19:49:18 -05:00
|
|
|
if (!total || total === 0) return { images: [], total };
|
2025-02-22 17:56:53 -05:00
|
|
|
|
2025-03-04 19:49:18 -05:00
|
|
|
const result = await imagesCollection.find({})
|
2025-03-11 19:50:28 -04:00
|
|
|
.sort({ created_at: 1 })
|
|
|
|
|
.skip((page - 1) * limit)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.toArray();
|
|
|
|
|
|
|
|
|
|
const objImages = result.map(image => ({
|
|
|
|
|
id: image._id,
|
|
|
|
|
user: image.userId,
|
|
|
|
|
file_name: image.file_name,
|
|
|
|
|
file_content: image.file_content.toString('base64'),
|
|
|
|
|
mime_type: image.mime_type
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let respObj = {
|
|
|
|
|
images: objImages,
|
|
|
|
|
total: total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return respObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getUserImages(page, limit, uid) {
|
|
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
|
|
|
|
const imagesCollection = conn.collection('images');
|
|
|
|
|
const total = await imagesCollection.countDocuments({ userId: uid });
|
|
|
|
|
if (!total || total === 0) return { images: [], total };
|
|
|
|
|
|
|
|
|
|
const result = await imagesCollection.find({ userId: uid })
|
2025-03-27 19:18:43 -04:00
|
|
|
.sort({ created_at: -1 })
|
2025-03-04 19:49:18 -05:00
|
|
|
.skip((page - 1) * limit)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.toArray();
|
2025-03-02 20:52:08 -05:00
|
|
|
|
2025-03-04 19:49:18 -05:00
|
|
|
const objImages = result.map(image => ({
|
2025-02-25 17:38:23 -05:00
|
|
|
id: image._id,
|
|
|
|
|
user: image.userId,
|
2025-02-22 17:56:53 -05:00
|
|
|
file_name: image.file_name,
|
2025-02-25 17:38:23 -05:00
|
|
|
file_content: image.file_content.toString('base64'),
|
2025-02-22 17:56:53 -05:00
|
|
|
mime_type: image.mime_type
|
|
|
|
|
}));
|
|
|
|
|
|
2025-03-02 20:52:08 -05:00
|
|
|
let respObj = {
|
|
|
|
|
images: objImages,
|
|
|
|
|
total: total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return respObj;
|
2025-02-22 17:56:53 -05:00
|
|
|
}
|
2025-03-13 18:02:26 -04:00
|
|
|
|
|
|
|
|
async delete(uid, imgId) {
|
|
|
|
|
let resp = false;
|
|
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2025-03-13 22:44:43 -04:00
|
|
|
const quizColl = conn.collection('files');
|
2025-03-13 18:02:26 -04:00
|
|
|
const rgxImg = new RegExp(`/api/image/get/${imgId}`);
|
|
|
|
|
|
2025-03-13 22:44:43 -04:00
|
|
|
const result = await quizColl.find({ userId: uid, content: { $regex: rgxImg }}).toArray();
|
|
|
|
|
if(!result || result.length < 1){
|
|
|
|
|
const imgsColl = conn.collection('images');
|
|
|
|
|
const isDeleted = await imgsColl.deleteOne({ _id: ObjectId.createFromHexString(imgId) });
|
2025-03-13 18:02:26 -04:00
|
|
|
if(isDeleted){
|
|
|
|
|
resp = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { deleted: resp };
|
|
|
|
|
}
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
module.exports = Images;
|