Skip to content

Commit

Permalink
fixins
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgehl3n committed Nov 26, 2023
1 parent 4483ea2 commit b02ac32
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 19 deletions.
398 changes: 398 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"redis": "^4.6.10",
"sequelize": "^6.32.1",
"sequelize-cli": "^6.6.1",
"sharp": "^0.32.6",
"ts-node": "^10.9.1",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
Expand Down
16 changes: 13 additions & 3 deletions src/controllers/AnimalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Color from "../database/models/Color";
import BehavioralProfile from "../database/models/BehavioralProfile";
import UserRole from "../database/models/UserRole";
import { Roles } from "../enums/Roles";
import sharp from "sharp";
const _mapRequestToData = async (req: Request) => {
let data = req.body as unknown as Animal;
data = mapData(data);
Expand Down Expand Up @@ -68,8 +69,14 @@ const mapData = (data: any) => {
return data;
}

const compressImage = async (imageBuffer: Buffer): Promise<Buffer> => {
return await sharp(imageBuffer)
.jpeg({ quality: 70 })
.png({ quality: 70 })
.toBuffer();
};

const _mapGalleryToData = (req: Request, images: Record<string, any>) => {
const _mapGalleryToData = async (req: Request, images: Record<string, any>) => {
const animalImages = images.filter(
(file: Express.Multer.File) =>
file.fieldname.includes('animalImages')
Expand All @@ -82,7 +89,10 @@ const _mapGalleryToData = (req: Request, images: Record<string, any>) => {
const id = req.body[`animalImages[${i}].id`]
imageObject.id = id != 0 ? id : null;
imageObject.idAnimal = idAnimal;
imageObject.image = animalImages[i].buffer;

const compressedBuffer = await compressImage(animalImages[i].buffer);
imageObject.image = compressedBuffer;

imagesConfig.push(imageObject);
const originalname = animalImages[i].originalname;
const fileExtension = originalname.split('.').pop();
Expand Down Expand Up @@ -240,7 +250,7 @@ export default class AnimalController {

const images = (req.files as Record<string, any>);
if (images && images.length > 0) {
animal.animalImages = _mapGalleryToData(req, images);
animal.animalImages = await _mapGalleryToData(req, images);
animal.animalAttachments = _mapAnimalAttachmentToData(req, images);
}

Expand Down
25 changes: 17 additions & 8 deletions src/controllers/InstitutionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import UserRole from "../database/models/UserRole";
import InstitutionFilters from "../filters/InstitutionFilters";
import { Op } from "sequelize";
import User from "../database/models/User";
import sharp from "sharp";

const _mapGalleryToData = (req: Request, images: Record<string, any>) => {
const _mapGalleryToData = async (req: Request, images: Record<string, any>) => {
const publicImages = images.filter(
(file: Express.Multer.File) =>
file.fieldname.includes('publicImages')
Expand All @@ -24,8 +25,8 @@ const _mapGalleryToData = (req: Request, images: Record<string, any>) => {
const imageObject = new InstitutionImage();
const id = req.body[`publicImages[${i}].id`]
imageObject.id = id != 0 ? id : null;
const image = publicImages[i].buffer;
imageObject.image = image.toString('base64');
const compressedBuffer = await compressImage(publicImages[i].buffer);
imageObject.image = compressedBuffer;
imagesConfig.push(imageObject);
}

Expand All @@ -35,7 +36,7 @@ const _mapGalleryToData = (req: Request, images: Record<string, any>) => {
const _mapRequestToData = async (req: Request) => {
const data = req.body as unknown as Institution;
const images = (req.files as Record<string, any>);
const imagesConfig = _mapGalleryToData(req, images);
const imagesConfig = await _mapGalleryToData(req, images);
data.publicImages = [...imagesConfig];
data.adress = req.body.address as Address;

Expand All @@ -55,7 +56,8 @@ const _mapRequestToData = async (req: Request) => {
}

if (logoImage) {
data.image = logoImage.buffer?.toString('base64');
const compressedBuffer = await compressImage(logoImage.buffer);
data.image = compressedBuffer.toString('base64');
}

return data;
Expand All @@ -73,14 +75,14 @@ const _mapInstitutionImage = async (entity: Institution) => {

const _mapPublicImages = async (entity: Institution) => {
if (entity.publicImages) {
await entity.publicImages.forEach(async (image: InstitutionImage) => {
await Promise.all(entity.publicImages.map(async (image: InstitutionImage) => {
if (image.image) {
const imageBase64 = await Buffer.from(image.image, 'base64').toString('ascii');
const imageBase64 = await Buffer.from(image.image as string, 'base64').toString('base64');
if (imageBase64) {
image.image = `data:image/png;base64,${imageBase64}`;
}
}
});
}));
}
return entity;
};
Expand Down Expand Up @@ -139,6 +141,13 @@ const sortingOptions: SortingOptions = {
'old': [['createdAt', 'ASC']],
};

const compressImage = async (imageBuffer: Buffer): Promise<Buffer> => {
return await sharp(imageBuffer)
.jpeg({ quality: 70 })
.png({ quality: 70 })
.toBuffer();
};

class InstitutionController {
public static async save(req: Request, res: Response) {
try {
Expand Down
14 changes: 12 additions & 2 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import moment from 'moment';
import Interest from '../database/models/Interest';
import { Op } from 'sequelize';
import { AuthenticatedRequest } from '../..';
import sharp from 'sharp';
const _mapCitiesToData = (req: Request) => {
const formData = req.body;

Expand Down Expand Up @@ -43,6 +44,14 @@ const _mapInterestsToData = (req: Request) => {
return interestsArray;
}


const compressImage = async (imageBuffer: Buffer): Promise<Buffer> => {
return await sharp(imageBuffer)
.jpeg({ quality: 70 })
.png({ quality: 70 })
.toBuffer();
};

const _mapRequestToData = async (req: Request) => {
const data = req.body as unknown as User;
const images = (req.files as Record<string, any>);
Expand All @@ -53,7 +62,8 @@ const _mapRequestToData = async (req: Request) => {
);

if (profileImage) {
data.profileImage = profileImage.buffer?.toString('base64');
const compressedBuffer = await compressImage(profileImage.buffer);
data.profileImage = compressedBuffer;
}
data.address = req.body.address as Address;
data.address.id = null
Expand Down Expand Up @@ -131,7 +141,7 @@ export default class UserController {
const AuthenticatedRequest = req as unknown as AuthenticatedRequest;
const entity = AuthenticatedRequest.user;
if (entity?.profileImage) {
const image = await Buffer.from(entity.profileImage, 'base64').toString('ascii');
const image = await Buffer.from(entity.profileImage as string, 'base64').toString('ascii');
if (image) {
entity.profileImage = `data:image/png;base64,${image}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/database/models/Animal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Animal extends Model {
public gender!: number;
public collectionDate!: Date;
public collectionPlace!: string;
public age!: number;
public weight!: number;
public age?: number;
public weight?: number;
public deathDate!: Date;
public deathDetail!: string;
public idBreed!: number;
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/AnimalImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Animal from "./Animal";
class AnimalImage extends Model {
public id!: number | null;
public description!: string;
public image!: string;
public image!: Buffer | string;
public type!: string;
public idAnimal!: number;
public animal!: Animal;
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/InstitutionImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Institution from './Institution';

class InstitutionImage extends Model {
public id!: number | null;
public image?: string | null;
public image?: string | Buffer | null;
public description?: string;
public idInstitution!: number | null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class User extends Model {
public encriptedPassword!: string;
public birthdayDate!: Date;
public phone!: string;
public profileImage!: string | null;
public profileImage!: string | null | Buffer;
public address!: Address;
public userRoles!: UserRole[];
public cities!: InterestCity[];
Expand Down
30 changes: 29 additions & 1 deletion src/filters/AnimalFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,30 @@ const _bondedFilters = (userRoles: UserRole[], searchFilter: any) => {
as: 'institution',
where: {
id: {
[Op.in]: [userRoles.map(ur => ur.idInstitution)]
[Op.in]: [...userRoles.map(ur => ur.idInstitution)]
}
},
},
{
model: AnimalImage,
as: 'animalImages',
separate: true, // Isso garantirá que apenas a primeira imagem seja incluída
limit: 1, // Limita o número de imagens incluídas para cada animal a 1
},
],
}
}

const _notBondedFilters = (userRoles: UserRole[], searchFilter: any) => {
return {
...searchFilter,
include: [
{
model: Institution,
as: 'institution',
where: {
id: {
[Op.notIn]: [...userRoles.map(ur => ur.idInstitution)]
}
},
},
Expand Down Expand Up @@ -59,6 +82,7 @@ export default class AnimalFilters {
static ApplyFilters = (req: Request) => {
const authenticatedRequest = req as unknown as AuthenticatedRequest;
const bonded = req.query.bonded || false;
const notBonded = req.query.notBonded || false;
let searchFilter = _getSearchFilters(req) as any;

const { userRoles } = (req as unknown as AuthenticatedRequest).user!;
Expand All @@ -67,6 +91,10 @@ export default class AnimalFilters {
searchFilter = _bondedFilters(userRoles, searchFilter);
}

if (notBonded) {
searchFilter = _notBondedFilters(userRoles, searchFilter);
}


const specie = req.query.dog ? Species.Dog : req.query.cat ? Species.Cat : null;
if (specie) {
Expand Down

0 comments on commit b02ac32

Please sign in to comment.