Skip to content

Commit

Permalink
Merge pull request #63 from pes2324q2-gei-upc/images
Browse files Browse the repository at this point in the history
Images
  • Loading branch information
Eman0209 authored May 28, 2024
2 parents a41e308 + 2a3d279 commit 96baf96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 27 additions & 5 deletions back_api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const admin = require('firebase-admin')
const express = require('express')
const router = express.Router()

const { v4: uuidv4 } = require("uuid");
const multer=require('multer')
const upload=multer({storage: multer.memoryStorage()})

router.use(express.json());

const { db } = require('../firebaseConfig');
const { db, bucket} = require('../firebaseConfig');
const { checkUserAndFetchData, checkAdmin, checkPerson, decryptToken } = require('./middleware');

const crypto = require('crypto');
Expand All @@ -19,6 +23,15 @@ function encrypt(text) {
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}

//funcio per afegir imatges al bucket
async function createImage(file){
const uuid = uuidv4();
const name = uuid + '_' + file.originalname;
const fileName = 'users/' + name + '.jpg';
await bucket.file(fileName).createWriteStream().end(file.buffer);
return fileName;
}

router.get('/exists', async (req, res) => {
try {
var uid = req.query.uid;
Expand Down Expand Up @@ -350,7 +363,6 @@ router.get('/data/:data', checkUserAndFetchData, async (req, res) => {
})



router.get('/username', async (req, res) => {
try {
var uid = req.query.uid;
Expand All @@ -369,20 +381,28 @@ router.get('/username', async (req, res) => {
}
});

router.post('/edit', checkUserAndFetchData, async(req, res) => { //MODIFICAR PARA USO DE TOKENS
router.post('/edit', checkUserAndFetchData, upload.single('file'), async(req, res) => { //MODIFICAR PARA USO DE TOKENS
try {

const { uid, username, favcategories } = req.body;
const { uid, username, favcategories, imatge } = req.body;

userDoc = await req.userDocument;
const categories = JSON.parse(favcategories);
const categories = Array.isArray(favcategories) ? favcategories : JSON.parse(favcategories)
//const categories = JSON.parse(favcategories);

const usersCollection = db.collection('users');

filename = imatge;

if (req.file !== undefined) {
filename = await createImage(req.file);
}

if (userDoc.exists && userDoc.id == uid) {
await usersCollection.doc(uid).update({
'username': username,
'favcategories': categories,
'image': filename
});
res.status(200).send('OK');
}
Expand Down Expand Up @@ -702,6 +722,7 @@ router.post('/create', async(req, res) => {
const blockedUsers = [];

const AssitedActivities = [];

await usersCollection.doc(uid).set({
'email': email,
'username': username,
Expand All @@ -712,6 +733,7 @@ router.post('/create', async(req, res) => {
'blockedUsers': blockedUsers,
'AssitedActivities': AssitedActivities,
'private': false,
'image': '',
'devices': devices
});
await crearInsignies(uid);
Expand Down
3 changes: 2 additions & 1 deletion back_api/tests/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ describe('POST /users/edit', () => {
.send({
uid: 'testUid1',
username: 'newUsername',
favcategories: JSON.stringify(['festa', 'cinema'])
favcategories: JSON.stringify(['festa', 'cinema']),
imatge: ''
});

expect(res2.statusCode).toEqual(200);
Expand Down

0 comments on commit 96baf96

Please sign in to comment.