diff --git a/lib/web/userRouter.js b/lib/web/userRouter.ts similarity index 62% rename from lib/web/userRouter.js rename to lib/web/userRouter.ts index f1f999f15..c22c604f6 100644 --- a/lib/web/userRouter.js +++ b/lib/web/userRouter.ts @@ -1,30 +1,33 @@ -'use strict' +import archiver from 'archiver' +import async from 'async' +import { Request, Response, Router } from 'express' +import { errors } from '../errors' +import { Note, User } from '../models' +import { logger } from '../logger' +import { generateAvatar } from '../letter-avatars' +import { config } from '../config' -const archiver = require('archiver') -const async = require('async') -const Router = require('express').Router - -const errors = require('../errors') -const config = require('../config') -const models = require('../models') -const logger = require('../logger') -const { generateAvatar } = require('../letter-avatars') - -const UserRouter = module.exports = Router() +const UserRouter = Router() // get me info -UserRouter.get('/me', function (req, res) { +UserRouter.get('/me', function (req: Request, res: Response) { if (req.isAuthenticated()) { - models.User.findOne({ + if (req.user == null) { + return errors.errorInternalError(res) + } + User.findOne({ where: { id: req.user.id } }).then(function (user) { if (!user) { return errors.errorNotFound(res) } - var profile = models.User.getProfile(user) + const profile = User.getProfile(user) + if (profile == null) { + return errors.errorInternalError(res) + } res.send({ status: 'ok', - id: req.user.id, + id: user.id, name: profile.name, photo: profile.photo }) @@ -40,9 +43,12 @@ UserRouter.get('/me', function (req, res) { }) // delete the currently authenticated user -UserRouter.get('/me/delete/:token?', function (req, res) { +UserRouter.get('/me/delete/:token?', function (req: Request, res: Response) { if (req.isAuthenticated()) { - models.User.findOne({ + if (req.user == null) { + return errors.errorInternalError(res) + } + User.findOne({ where: { id: req.user.id } @@ -67,10 +73,13 @@ UserRouter.get('/me/delete/:token?', function (req, res) { }) // export the data of the authenticated user -UserRouter.get('/me/export', function (req, res) { +UserRouter.get('/me/export', function (req: Request, res: Response) { if (req.isAuthenticated()) { + if (req.user == null) { + return errors.errorInternalError(res) + } // let output = fs.createWriteStream(__dirname + '/example.zip'); - let archive = archiver('zip', { + const archive = archiver('zip', { zlib: { level: 3 } // Sets the compression level. }) res.setHeader('Content-Type', 'application/zip') @@ -80,25 +89,28 @@ UserRouter.get('/me/export', function (req, res) { logger.error('export user data failed: ' + err) return errors.errorInternalError(res) }) - models.User.findOne({ + User.findOne({ where: { id: req.user.id } }).then(function (user) { - models.Note.findAll({ + if (user == null) { + return errors.errorInternalError(res) + } + Note.findAll({ where: { ownerId: user.id } }).then(function (notes) { - let filenames = {} + const filenames = {} async.each(notes, function (note, callback) { - let basename = note.title.replace(/\//g, '-') // Prevent subdirectories + const basename = note.title.replace(/\//g, '-') // Prevent subdirectories let filename - let suffix = '' + let numberOfDuplicateFilename = 0 do { - let seperator = typeof suffix === 'number' ? '-' : '' - filename = basename + seperator + suffix + '.md' - suffix++ + const suffix = numberOfDuplicateFilename !== 0 ? '-' + numberOfDuplicateFilename : '' + filename = basename + suffix + '.md' + numberOfDuplicateFilename++ } while (filenames[filename]) filenames[filename] = true @@ -122,8 +134,10 @@ UserRouter.get('/me/export', function (req, res) { } }) -UserRouter.get('/user/:username/avatar.svg', function (req, res, next) { +UserRouter.get('/user/:username/avatar.svg', function (req: Request, res: Response, _) { res.setHeader('Content-Type', 'image/svg+xml') res.setHeader('Cache-Control', 'public, max-age=86400') res.send(generateAvatar(req.params.username)) }) + +export { UserRouter } diff --git a/package.json b/package.json index e76c4d4fd..34f96af96 100644 --- a/package.json +++ b/package.json @@ -168,6 +168,7 @@ "url": "https://github.com/codimd/server.git" }, "devDependencies": { + "@types/archiver": "^3.1.0", "@types/bluebird": "^3.5.30", "@types/diff-match-patch": "^1.0.32", "@types/express": "^4.17.6", @@ -175,8 +176,8 @@ "@types/lodash": "^4.14.149", "@types/minio": "^7.0.5", "@types/node": "^13.11.1", - "@types/randomcolor": "^0.5.4", "@types/passport": "^1.0.3", + "@types/randomcolor": "^0.5.4", "@types/validator": "^13.0.0", "@typescript-eslint/eslint-plugin": "^2.27.0", "@typescript-eslint/parser": "^2.27.0", diff --git a/yarn.lock b/yarn.lock index 7ddc723cd..1a37cf4c4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -110,6 +110,13 @@ resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a" integrity sha512-/+CRPXpBDpo2RK9C68N3b2cOvO0Cf5B9aPijHsoDQTHivnGSObdOF2BRQOYjojWTDy6nQvMjmqRXIxH55VjxxA== +"@types/archiver@^3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@types/archiver/-/archiver-3.1.0.tgz#0d5bd922ba5cf06e137cd6793db7942439b1805e" + integrity sha512-nTvHwgWONL+iXG+9CX+gnQ/tTOV+qucAjwpXqeUn4OCRMxP42T29FFP/7XaOo0EqqO3TlENhObeZEe7RUJAriw== + dependencies: + "@types/glob" "*" + "@types/bluebird@^3.5.30": version "3.5.30" resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.30.tgz#ee034a0eeea8b84ed868b1aa60d690b08a6cfbc5" @@ -150,6 +157,11 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw== +"@types/events@*": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" + integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== + "@types/express-serve-static-core@*": version "4.17.4" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.4.tgz#157c79c2d28b632d6418497c57c93185e392e444" @@ -173,6 +185,15 @@ resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.7.tgz#c8fa532b60a0042219cdf173ca21a975ef0666ad" integrity sha512-wE2v81i4C4Ol09RtsWFAqg3BUitWbHSpSlIo+bNdsCJijO9sjme+zm+73ZMCa/qMC8UEERxzGbvmr1cffo2SiQ== +"@types/glob@*": + version "7.1.1" + resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" + integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== + dependencies: + "@types/events" "*" + "@types/minimatch" "*" + "@types/node" "*" + "@types/helmet@^0.0.45": version "0.0.45" resolved "https://registry.yarnpkg.com/@types/helmet/-/helmet-0.0.45.tgz#3eab6550a4e19acf86012596a7f1981529480fd5" @@ -207,6 +228,11 @@ resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d" integrity sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw== +"@types/minimatch@*": + version "3.0.3" + resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" + integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== + "@types/minio@^7.0.5": version "7.0.5" resolved "https://registry.yarnpkg.com/@types/minio/-/minio-7.0.5.tgz#9b4da55935a1159438ae038ec4a8bc7035dd11c4"