From 58a2baddde0d85357d50489277249e1c9be09f36 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 23 Jan 2023 08:49:43 -0800 Subject: [PATCH] Working again Signed-off-by: James Ketrenos --- docker-compose.yml | 2 +- server/development.location | 6 ++-- server/nginx.conf | 14 ++++---- server/production.location | 7 ++-- server/routes/identities.js | 71 ++++++++++++++++++++++++++++--------- 5 files changed, 68 insertions(+), 32 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 78c5384..2b7967e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,7 @@ services: # - db restart: always ports: - - ${PORT}:80 # nginx -> server/app.js express app + - ${PORT}:443 # nginx -> server/app.js express app # - 127.0.0.1:${SHELL_PORT}:4200 # shellinabox volumes: - /etc/letsencrypt:/etc/letsencrypt:ro # Use host web keys diff --git a/server/development.location b/server/development.location index 4f904a5..be77d16 100644 --- a/server/development.location +++ b/server/development.location @@ -1,9 +1,10 @@ # DEVELOPMENT -- use npm development server on port 3000 (entrypoint.sh) location /identities/api/v1/ { rewrite ^/identities/api/v1/(.*)$ /api/v1/$1 break; - proxy_pass http://localhost/; + proxy_pass http://localhost:8123; proxy_redirect off; proxy_set_header Host $host; + proxy_ssl_verify off; } location /identities { @@ -17,5 +18,6 @@ location /identities { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; - proxy_pass http://localhost:3000; + proxy_ssl_verify off; + proxy_pass https://localhost:3000; } diff --git a/server/nginx.conf b/server/nginx.conf index ed10d6d..eef3b08 100644 --- a/server/nginx.conf +++ b/server/nginx.conf @@ -1,17 +1,17 @@ server { listen 80 default_server; listen [::]:80 default_server; -# return 301 https://$host$request_uri; -#} + return 301 https://$host$request_uri; +} -#server { -# listen 443 ssl; +server { + listen 443 ssl; client_max_body_size 5g; -# ssl on; + ssl on; -# ssl_certificate /etc/letsencrypt/live/ketrenos.com/fullchain.pem; -# ssl_certificate_key /etc/letsencrypt/live/ketrenos.com/privkey.pem; + ssl_certificate /etc/letsencrypt/live/ketrenos.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/ketrenos.com/privkey.pem; root /website; index index.html; diff --git a/server/production.location b/server/production.location index dc140d9..0d57dd9 100644 --- a/server/production.location +++ b/server/production.location @@ -1,13 +1,10 @@ # PRODUCTION -- pre-built source location /identities/api/v1/ { - rewrite ^/identities/api/v1/(.*)$ /api/v1/$1 break; - proxy_pass http://localhost/; - proxy_redirect off; - proxy_set_header Host $host; + rewrite ^/identities/api/v1/(.*)$ https://${host}/api/v1/$1 permanent; } location /identities { try_files $uri $uri/ =404; alias /website/client/build; index index.html; -} +} \ No newline at end of file diff --git a/server/routes/identities.js b/server/routes/identities.js index bd7625e..297076a 100755 --- a/server/routes/identities.js +++ b/server/routes/identities.js @@ -135,6 +135,35 @@ router.put('/:id', async (req, res) => { }); +router.delete('/:id', async (req, res) => { + console.log(`DELETE ${req.url}`) + if (!req.user.maintainer) { + console.warn(`${req.user.name} attempted to modify photos.`); + return res.status(401).send({ message: "Unauthorized to modify photos." }); + } + + const { id } = req.params; + if (!id || isNaN(+id)) { + return res.status(400).send({ message: `Invalid identity id ${id}` }); + } + + await photoDB.sequelize.query( + 'UPDATE faces SET distance=0,identityId=NULL ' + + 'WHERE identityId=:id', { + replacements: { id } + } + ); + + await photoDB.sequelize.query( + 'DELETE FROM identities ' + + 'WHERE identityId=:id', { + replacements: { id } + }); + + return res.status(200).send({}); +}); + + const addFaceToIdentityDescriptors = (identity, face) => { }; @@ -345,9 +374,10 @@ const updateIdentityFaces = async (identity) => { const faces = await photoDB.sequelize.query( "SELECT " + "faces.*,faceDescriptors.* " + + "FROM faces,faceDescriptors " + "WHERE " + "faces.identityId=:identityId " + - "AND faceDescriptors.id=faces.descriptorsId", { + "AND faceDescriptors.id=faces.descriptorId", { replacements: identity, type: photoDB.Sequelize.QueryTypes.SELECT, raw: true @@ -359,7 +389,7 @@ const updateIdentityFaces = async (identity) => { count = 0; faces.forEach((face) => { - if (!descriptors[index]) { + if (!identity.descriptors) { return; } if (!face.descriptors) { @@ -408,7 +438,7 @@ const updateIdentityFaces = async (identity) => { } let sql = ''; - if (closestId !== undefined && closestId !== identity.faceId) { + if (closestId !== -1 && closestId !== identity.faceId) { sql = `${sql} faceId=:faceId`; } if (!same) { @@ -416,16 +446,14 @@ const updateIdentityFaces = async (identity) => { sql = `${sql}, `; } sql = `${sql} descriptors=:descriptors`; + identity.descriptors = average; } if (sql !== '') { - await photoDB.sequelize.select( + identity.faceId = closestId; + await photoDB.sequelize.query( `UPDATE identities SET ${sql} ` + `WHERE id=:identityId`, { - replacements: { - faceId: closestId, - descriptors: average, - identityId: identity.id - } + replacements: identity } ); } @@ -468,27 +496,36 @@ router.get("/:id?", async (req, res) => { }); await Promise.map(identities, async (identity) => { - let where; - /* If id was set, only return a single face */ - if (id !== undefined) { - if (identity.faceId !== -1) { + for (let field in identity) { + if (field.match(/.*Name/) && identity[field] === null) { + identity[field] = ''; + } + } + + let where, limit = ''; + /* If id was not set, only return a single face */ + if (id === undefined) { + if (identity.faceId !== -1 && identity.faceId !== null) { where = 'faceId=:faceId'; } else { - where = 'identityId=:identityId LIMIT 1'; + where = 'identityId=:identityId'; + limit = 'LIMIT 1'; } } else { where = 'identityId=:identityId' } identity.relatedFaces = await photoDB.sequelize.query( - 'SELECT id as faceId,photoId,distance FROM faces ' + - `WHERE ${where} ORDER BY distance ASC`, { + 'SELECT id as faceId,identityId,photoId,distance ' + + 'FROM faces ' + + `WHERE ${where} ` + + 'ORDER BY distance ASC ' + + limit, { replacements: identity, type: photoDB.Sequelize.QueryTypes.SELECT, raw: true } ); - if (identity.relatedFaces.length !== 0 && (!identity.faceId || identity.faceId === -1)) { await updateIdentityFaces(identity);