From f96978f8eedb09692382842c4cdba42e1f552fcb Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 29 Nov 2022 12:21:55 +0200 Subject: [PATCH] functionality to upload an avatar --- README.md | 4 ++++ package.json | 1 + src/server/endpoints/api/User.js | 26 ++++++++++++++++++++++++++ yarn.lock | 19 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/README.md b/README.md index 4ee61c6..8280094 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ Endpoints have a loadOrder property that can be adjusted, lower values are loade **Serving files** By default the framework looks for files to serve under the `/files` directory, it also tries to satisfy requests to `/` with an `index.html` from `/files/site`. This is with the frontend in mind. +## TODO +- Dependency injection for structures, such as User. +- Some kind of plugin system + ## Main components **Controller:** `/src/controller/Controller.js` Master process, orchestrates the whole program. Takes care of starting up the shards and communication with them. diff --git a/package.json b/package.json index 47bf221..277841d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", + "express-fileupload": "^1.4.0", "express-session": "^1.17.3", "helmet": "^6.0.0", "mongodb": "^4.11.0", diff --git a/src/server/endpoints/api/User.js b/src/server/endpoints/api/User.js index ced8583..f1cbc1e 100644 --- a/src/server/endpoints/api/User.js +++ b/src/server/endpoints/api/User.js @@ -4,10 +4,12 @@ const { UtilMiddleware } = require('../../middleware'); const pkg = require('../../../../package.json'); const { Util } = require("../../../util"); +const fileUpload = require('express-fileupload'); const qrcode = require('qrcode'); const { authenticator } = require('otplib'); const { inspect } = require('node:util'); +const path = require('node:path'); // Populated by the constructor const ServiceProfileLinks = {}; @@ -26,6 +28,8 @@ class UserEndpoint extends ApiEndpoint { ]; this.subpaths = [ + [ 'post', '/avatar', this.uploadAvatar.bind(this), [ fileUpload({ limits: { fileSize: 1024 * 1024 * 12, files: 1 } }) ]], + // 2 Factor Authentication [ 'get', '/2fa', this.twoFactor.bind(this) ], [ 'post', '/2fa/disable', this.disable2fa.bind(this) ], @@ -55,6 +59,28 @@ class UserEndpoint extends ApiEndpoint { return res.json(req.user.safeJson); } + async uploadAvatar (req, res) { + const { user, files } = req; + const { file } = files; + if (!file) return res.status(400).send('Missing file'); + const x = file.name.split('.'); + const fileName = `${user.id}.${x[x.length - 1]}`; + const filesDir = path.resolve(this.server.serveFiles, 'avatars', fileName); + + file.mv(filesDir, async (err) => { + if (err) { + this.logger.error(`Error saving file: ${err.stack}`); + return res.status(500).end(); + } + + user.avatar = fileName; + await user.save(); + res.end(); + + }); + + } + async twoFactor (req, res) { const { user } = req; diff --git a/yarn.lock b/yarn.lock index 0ccc3d2..2363b11 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1068,6 +1068,13 @@ buffer@^5.6.0: base64-js "^1.3.1" ieee754 "^1.1.13" +busboy@^1.6.0: + version "1.6.0" + resolved "https://registry.corgi.wtf/busboy/-/busboy-1.6.0.tgz#966ea36a9502e43cdb9146962523b92f531f6893" + integrity sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA== + dependencies: + streamsearch "^1.1.0" + bytes@3.1.2: version "3.1.2" resolved "https://registry.corgi.wtf/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5" @@ -1418,6 +1425,13 @@ etag@~1.8.1: resolved "https://registry.corgi.wtf/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg== +express-fileupload@^1.4.0: + version "1.4.0" + resolved "https://registry.corgi.wtf/express-fileupload/-/express-fileupload-1.4.0.tgz#be9d70a881d6c2b1ce668df86e4f89ddbf238ec7" + integrity sha512-RjzLCHxkv3umDeZKeFeMg8w7qe0V09w3B7oGZprr/oO2H/ISCgNzuqzn7gV3HRWb37GjRk429CCpSLS2KNTqMQ== + dependencies: + busboy "^1.6.0" + express-session@^1.17.3: version "1.17.3" resolved "https://registry.corgi.wtf/express-session/-/express-session-1.17.3.tgz#14b997a15ed43e5949cb1d073725675dd2777f36" @@ -2513,6 +2527,11 @@ statuses@2.0.1: resolved "https://registry.corgi.wtf/statuses/-/statuses-2.0.1.tgz#55cb000ccf1d48728bd23c685a063998cf1a1b63" integrity sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ== +streamsearch@^1.1.0: + version "1.1.0" + resolved "https://registry.corgi.wtf/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764" + integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg== + "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.corgi.wtf/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"