...
 
Commits (12)
......@@ -12014,6 +12014,11 @@
}
}
},
"os": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/os/-/os-0.1.1.tgz",
"integrity": "sha1-IIhF6J4ZOtTZcUdLk5R3NqVtE/M="
},
"os-browserify": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/os-browserify/-/os-browserify-0.3.0.tgz",
......
......@@ -6,6 +6,7 @@ const logger = require('winston')
const cookie = require('cookie')
const path = require('path')
const os = require('os')
const fileUtils = require('./helpers/fileUtils')
const feathers = require('@feathersjs/feathers')
// Set the Node configuration directory to server/config instead of the default
......@@ -28,6 +29,12 @@ const appHooks = require('./app.hooks')
const channels = require('./channels')
const dataDirectoryPath = path.join(os.homedir(), '.hallo.gent')
fileUtils.makeDir(`${dataDirectoryPath}/uploads/`)
.catch(err => {
throw err
})
const secretFile = require(`${dataDirectoryPath}/secret.json`)
const app = express(feathers())
......@@ -60,6 +67,13 @@ app.configure(jwt({
audience: 'hallo.gent'
}))
// Expose dataDirectoryPath to requests and feathers services
app.use((req, res, next) => {
req.dataDirectoryPath = dataDirectoryPath
req.feathers.dataDirectoryPath = dataDirectoryPath
next()
})
// Setup a hook to only allow valid JWTs to authenticate
// and get new JWT access tokens
app.service('authentication').hooks({
......@@ -70,6 +84,9 @@ app.service('authentication').hooks({
}
})
// Add express routes
app.use('/uploads', require('./routes/uploads'))
// Configure other middleware (see `middleware/index.js`)
app.configure(middleware)
// Set up our services (see `services/index.js`)
......
......@@ -58,8 +58,7 @@ function removeFile (fileName) {
}
// remove a dir
function removeDir(path) {
console.log(path)
function removeDir (path) {
return fs.remove(path)
}
......
......@@ -3,18 +3,18 @@ const fileUtils = require('../helpers/fileUtils')
module.exports = (image, original) => {
if (image === '' && original && original !== '') {
// Original image was removed and no new image was uploaded, so remove original image.
fileUtils.removeFile(`./client/static${original}`).catch(err => {
fileUtils.removeFile(original).catch(err => {
throw err
})
image = null
} else if (image && image !== '' && (!original || original === '' || image !== original)) {
// An image was uploaded, save only the path to the image
image = `${image[0].destination.replace('./client/static', '')}/${image[0].filename}`
// An image was uploaded, save only the PUBLIC path to the image
image = image[0].public
// And remove originalImage if present
if (!original && original !== '') {
fileUtils.removeFile(`./client/static${original}`).catch(err => {
if (original && original !== '') {
fileUtils.removeFile(original).catch(err => {
throw err
})
}
......
......@@ -5,8 +5,8 @@ const fileUtils = require('../helpers/fileUtils')
// storage for header and profile images
const infoStorage = multer.diskStorage({
destination: function (req, file, next) {
fileUtils.makeDir(`./client/static/uploads/${req.params.__feathersId}`).then(() => {
next(null, `./client/static/uploads/${req.params.__feathersId}`)
fileUtils.makeDir(`${req.dataDirectoryPath}/uploads/${req.params.__feathersId}`).then(() => {
next(null, `${req.dataDirectoryPath}/uploads/${req.params.__feathersId}`)
}).catch(err => {
throw err
})
......
......@@ -2,6 +2,26 @@
// Note: files will only be added to the request when multer is previous middleware
module.exports = (req, res, next) => {
if (req.files) {
// provide a publicly accessible file path
Object.keys(req.files).forEach(
key => {
let file = req.files[key][0]
file.public = `/uploads/${req.params.__feathersId}/${file.filename}`
}
)
Object.keys(req.body).forEach(key => {
// convert string 'null' to real null
if (req.body[key] === 'null') {
req.body[key] = null
}
// convert public upload path to server path
if (typeof req.body[key] === 'string' && req.body[key].indexOf('/uploads') === 0 ) {
req.body[key] = req.dataDirectoryPath + req.body[key]
}
})
const data = Object.assign({}, req.body, req.files)
req.body = data
}
......
const express = require('express')
const router = express.Router()
// const jsonWebToken = require('jsonwebtoken')
router.get('/:domain/:id',
// authenticate request with JSONWebToken
// optional
// function (req, res, next) {
// jsonWebToken.verify(req.headers.authorization, secretFile.secret, err => {
// if (err) {
// res.status(401).end()
// } else {
// next()
// }
// })
// },
// fetch file and return
function (req, res) {
console.log('mkqsdf')
res.sendFile(`${req.params.domain}/${req.params.id}`, {root: `${req.dataDirectoryPath}/uploads/`}, err => {
if (err) {
res.status(err.status).end()
}
})
})
module.exports = router
......@@ -13,7 +13,6 @@ class Service {
try {
const site = new PendingSite(id)
const code = new AccountCode(params.payload.accountCode)
await site.saveInfo(data)
await code.setLocale(data.locale)
} catch (err) {
......
......@@ -15,17 +15,9 @@ module.exports = {
// check if peningSite exists
const site = new PendingSite(context.id)
site.getMetaData().then(() => {
// convert string 'null' to real null
Object.keys(context.data).forEach(key => {
if (context.data[key] === 'null') {
context.data[key] = null
}
})
// set the correct path for the images
context.data.headerImage = fileCheck(context.data.headerImage, context.data.headerOriginalImage)
context.data.profileImage = fileCheck(context.data.profileImage, context.data.profileOriginalImage)
// String to JSON convertion
context.data.headerImageMetadata = JSON.parse(context.data.headerImageMetadata)
context.data.profileImageMetadata = JSON.parse(context.data.profileImageMetadata)
......
......@@ -25,7 +25,7 @@ class Service {
await site.remove()
// Delete images from site
await fileUtils.removeDir(`./client/static/uploads/${id}`)
await fileUtils.removeDir(`${params.dataDirectoryPath}/uploads/${id}`)
// delete the conversation on the server.
await messagesDb.deleteConversation(metaData.accountCode)
......