dont use synchronous filesystem functions in workers

This commit is contained in:
Evert Prants 2017-09-09 14:36:30 +03:00
parent f54f6fb1b0
commit a03af7ca0f
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 26 additions and 7 deletions

17
scripts/existsSync.js Normal file
View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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})
})
}))
/*
========