From a03af7ca0f48a35d78597c3e448585f136977da9 Mon Sep 17 00:00:00 2001 From: Evert Date: Sat, 9 Sep 2017 14:36:30 +0300 Subject: [PATCH] dont use synchronous filesystem functions in workers --- scripts/existsSync.js | 17 +++++++++++++++++ server/api/image.js | 2 +- server/api/index.js | 7 ++++--- server/routes/index.js | 7 ++++--- 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 scripts/existsSync.js diff --git a/scripts/existsSync.js b/scripts/existsSync.js new file mode 100644 index 0000000..42adacd --- /dev/null +++ b/scripts/existsSync.js @@ -0,0 +1,17 @@ +import path from 'path' +import Promise from 'bluebird' +import fs from 'fs' + +const access = Promise.promisify(fs.access) + +async function exists (fpath) { + try { + await access(path.resolve(fpath)) + } catch (e) { + return false + } + + return true +} + +module.exports = exists diff --git a/server/api/image.js b/server/api/image.js index 20ca21e..6d6706c 100644 --- a/server/api/image.js +++ b/server/api/image.js @@ -61,7 +61,7 @@ async function imageBase64 (baseObj) { let fpath = path.join(images, imageName) try { - fs.writeFileSync(fpath, imgData.data) + await fs.writeFileAsync(fpath, imgData.data) } catch (e) { console.error(e) return null diff --git a/server/api/index.js b/server/api/index.js index f696ab5..9f8149e 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -2,6 +2,7 @@ import path from 'path' import cprog from 'child_process' import config from '../../scripts/load-config' import http from '../../scripts/http' +import exists from '../../scripts/existsSync' import models from './models' import crypto from 'crypto' import notp from 'notp' @@ -185,14 +186,14 @@ const API = { let uploadsDir = path.join(__dirname, '../../', 'usercontent', 'images') let pathOf = path.join(uploadsDir, fileName) - if (!fs.existsSync(pathOf)) { + if (!await exists(pathOf)) { return {error: 'No such file'} } // Delete previous upload if (user.avatar_file != null) { let file = path.join(uploadsDir, user.avatar_file) - if (fs.existsSync(file)) { + if (await exists(file)) { await fs.unlinkAsync(file) } } @@ -206,7 +207,7 @@ const API = { if (!user.avatar_file) return {} let file = path.join(uploadsDir, user.avatar_file) - if (fs.existsSync(file)) { + if (await exists(file)) { await fs.unlinkAsync(file) } diff --git a/server/routes/index.js b/server/routes/index.js index 210be14..1f955ee 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -3,6 +3,7 @@ import path from 'path' import express from 'express' import RateLimit from 'express-rate-limit' import config from '../../scripts/load-config' +import exists from '../../scripts/existsSync' import wrap from '../../scripts/asyncRoute' import http from '../../scripts/http' import API from '../api' @@ -629,9 +630,9 @@ router.post('/user/manage/email', accountLimiter, wrap(async (req, res, next) => // Serve a document form the documents directory, cache it. const docsDir = path.join(__dirname, '../../documents') -router.get('/docs/:name', (req, res, next) => { +router.get('/docs/:name', wrap(async (req, res, next) => { let doc = path.join(docsDir, req.params.name + '.html') - if (!fs.existsSync(docsDir) || !fs.existsSync(doc)) { + if (!await exists(docsDir) || !await exists(doc)) { return next() } @@ -643,7 +644,7 @@ router.get('/docs/:name', (req, res, next) => { res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000) // 1 week res.render('document', {doc: doc}) -}) +})) /* ========