diff --git a/server/controllers/aws.controller.js b/server/controllers/aws.controller.js index 69dc81ef..2cce85d1 100644 --- a/server/controllers/aws.controller.js +++ b/server/controllers/aws.controller.js @@ -11,7 +11,7 @@ export function signS3(req, res) { filename = uuid.v4() + fileExtension, acl = 'public-read', p = policy({ - acl: acl, + acl, secret: process.env.AWS_SECRET_KEY, length: 5000000, // in bytes? bucket: process.env.S3_BUCKET, @@ -25,4 +25,4 @@ export function signS3(req, res) { 'signature': p.signature }; return res.json(result); -}; \ No newline at end of file +} diff --git a/server/controllers/embed.controller.js b/server/controllers/embed.controller.js index 803b20ae..069cb88f 100644 --- a/server/controllers/embed.controller.js +++ b/server/controllers/embed.controller.js @@ -12,7 +12,7 @@ import jsdom, { serializeDocument } from 'jsdom'; export function serveProject(req, res) { Project.findById(req.params.project_id) .exec((err, project) => { - //TODO this does not parse html + // TODO this does not parse html const files = project.files; let htmlFile = files.find(file => file.name.match(/\.html$/i)).content; const filesToInject = files.filter(file => file.name.match(/\.(js|css)$/i)); @@ -28,4 +28,4 @@ export function serveProject(req, res) { res.send(serializeDocument(sketchDoc)); }); }); -} \ No newline at end of file +} diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index 643c4b28..ebe2643e 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -47,7 +47,7 @@ function deleteChild(files, parentId, id) { files = files.map((file) => { if (file.id === parentId) { file.children = file.children.filter(child => child !== id); - return file + return file; } return file; }); @@ -63,19 +63,19 @@ export function deleteFile(req, res) { // project.files.id(req.query.parentId).children = childrenArray.filter(id => id !== req.params.file_id); project.save(innerErr => { res.json(project.files); - }) + }); }); } export function getFileContent(req, res) { Project.findById(req.params.project_id, (err, project) => { if (err) { - return res.status(404).send({success: false, message: 'Project with that id does not exist.'}); + return res.status(404).send({ success: false, message: 'Project with that id does not exist.' }); } const filePath = req.params[0]; const resolvedFile = resolvePathToFile(filePath, project.files); if (!resolvedFile) { - return res.status(404).send({success: false, message: 'File with that name and path does not exist.'}); + return res.status(404).send({ success: false, message: 'File with that name and path does not exist.' }); } res.send(resolvedFile.content); }); diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 89321213..ff7b86b6 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -14,11 +14,11 @@ export function createProject(req, res) { Project.create(projectValues, (err, newProject) => { if (err) { return res.json({ success: false }); } Project.populate(newProject, - {path: 'user', select: 'username'}, + { path: 'user', select: 'username' }, (innerErr, newProjectWithUser) => { if (innerErr) { return res.json({ success: false }); } return res.json(newProjectWithUser); - }); + }); }); } @@ -64,7 +64,7 @@ export function getProject(req, res) { } export function deleteProject(req, res) { - Project.remove({_id: req.params.project_id}, (err) => { + Project.remove({ _id: req.params.project_id }, (err) => { if (err) { return res.status(404).send({ message: 'Project with that id does not exist' }); } @@ -74,7 +74,7 @@ export function deleteProject(req, res) { export function getProjects(req, res) { if (req.user) { - Project.find({user: req.user._id}) // eslint-disable-line no-underscore-dangle + Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle .sort('-createdAt') .select('name files id createdAt updatedAt') .exec((err, projects) => { @@ -84,13 +84,12 @@ export function getProjects(req, res) { // could just move this to client side return res.json([]); } - } export function getProjectsForUser(req, res) { if (req.params.username) { User.findOne({ username: req.params.username }, (err, user) => { - Project.find({user: user._id}) // eslint-disable-line no-underscore-dangle + Project.find({ user: user._id }) // eslint-disable-line no-underscore-dangle .sort('-createdAt') .select('name files id createdAt updatedAt') .exec((err, projects) => { @@ -111,8 +110,8 @@ function buildZip(project, req, res) { const projectName = project.name; let numCompletedFiles = 0; - zip.on('error', function(err) { - res.status(500).send({error: err.message}); + zip.on('error', function (err) { + res.status(500).send({ error: err.message }); }); res.attachment(`${project.name}.zip`); @@ -150,7 +149,7 @@ function buildZip(project, req, res) { export function downloadProjectAsZip(req, res) { Project.findById(req.params.project_id, (err, project) => { - //save project to some path + // save project to some path buildZip(project, req, res); }); } diff --git a/server/controllers/session.controller.js b/server/controllers/session.controller.js index cff3a0ef..dd35a07f 100644 --- a/server/controllers/session.controller.js +++ b/server/controllers/session.controller.js @@ -33,6 +33,6 @@ export function getSession(req, res) { export function destroySession(req, res) { req.logout(); - res.json({success: true}); + res.json({ success: true }); } diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 2adc74e1..b9d5c14d 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -58,10 +58,10 @@ export function duplicateUserCheck(req, res) { export function updatePreferences(req, res) { User.findById(req.user.id, (err, user) => { if (err) { - return res.status(500).json({error: err}); + return res.status(500).json({ error: err }); } - if (!user){ - return res.status(404).json({error: 'Document not found'}); + if (!user) { + return res.status(404).json({ error: 'Document not found' }); } const preferences = Object.assign({}, user.preferences, req.body.preferences); @@ -69,31 +69,31 @@ export function updatePreferences(req, res) { user.save((err) => { if (err) { - return res.status(500).json({error: err}); + return res.status(500).json({ error: err }); } return res.json(user.preferences); }); - }) + }); } export function resetPasswordInitiate(req, res) { async.waterfall([ (done) => { - crypto.randomBytes(20, function(err, buf) { + crypto.randomBytes(20, function (err, buf) { var token = buf.toString('hex'); done(err, token); }); }, (token, done) => { - User.findOne({ email: req.body.email }, (err, user) => { + User.findOne({ email: req.body.email }, (err, user) => { if (!user) { - return res.json({success: true, message: 'If the email is registered with the editor, an email has been sent.'}); + return res.json({ success: true, message: 'If the email is registered with the editor, an email has been sent.' }); } user.resetPasswordToken = token; user.resetPasswordExpires = Date.now() + 3600000; // 1 hour - user.save(function(err) { + user.save(function (err) { done(err, token, user); }); }); @@ -124,34 +124,34 @@ export function resetPasswordInitiate(req, res) { ], (err) => { if (err) { console.log(err); - return res.json({success: false}); + return res.json({ success: false }); } - //send email here - return res.json({success: true, message: 'If the email is registered with the editor, an email has been sent.'}); + // send email here + return res.json({ success: true, message: 'If the email is registered with the editor, an email has been sent.' }); }); } export function validateResetPasswordToken(req, res) { User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, (err, user) => { if (!user) { - return res.status(401).json({success: false, message: 'Password reset token is invalid or has expired.'}); + return res.status(401).json({ success: false, message: 'Password reset token is invalid or has expired.' }); } res.json({ success: true }); }); } export function updatePassword(req, res) { - User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) { + User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function (err, user) { if (!user) { - return res.status(401).json({success: false, message: 'Password reset token is invalid or has expired.'}); + return res.status(401).json({ success: false, message: 'Password reset token is invalid or has expired.' }); } user.password = req.body.password; user.resetPasswordToken = undefined; user.resetPasswordExpires = undefined; - user.save(function(err) { - req.logIn(user, function(err) { + user.save(function (err) { + req.logIn(user, function (err) { return res.json({ email: req.user.email, username: req.user.username, @@ -162,5 +162,5 @@ export function updatePassword(req, res) { }); }); - //eventually send email that the password has been reset + // eventually send email that the password has been reset } diff --git a/server/models/project.js b/server/models/project.js index e586e84b..eed9fd22 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -9,7 +9,7 @@ const defaultSketch = `function setup() { } function draw() { background(220); -}` +}`; const defaultHTML = ` @@ -24,7 +24,7 @@ const defaultHTML = -` +`; const defaultCSS = `html, body { overflow: hidden; @@ -37,32 +37,32 @@ const fileSchema = new Schema({ name: { type: String, default: 'sketch.js' }, content: { type: String, default: '' }, url: { type: String }, - children: { type: [ String ], default: [] }, + children: { type: [String], default: [] }, fileType: { type: String, default: 'file' }, isSelectedFile: { type: Boolean } }, { timestamps: true, _id: true }); -fileSchema.virtual('id').get(function(){ - return this._id.toHexString(); +fileSchema.virtual('id').get(function () { + return this._id.toHexString(); }); fileSchema.set('toJSON', { - virtuals: true + virtuals: true }); const projectSchema = new Schema({ name: { type: String, default: "Hello p5.js, it's the server" }, user: { type: Schema.Types.ObjectId, ref: 'User' }, - files: { type: [ fileSchema ] }, + files: { type: [fileSchema] }, _id: { type: String, default: shortid.generate } }, { timestamps: true }); -projectSchema.virtual('id').get(function(){ - return this._id; +projectSchema.virtual('id').get(function () { + return this._id; }); projectSchema.set('toJSON', { - virtuals: true + virtuals: true }); // projectSchema.pre('save', function createSelectedFile(next) { diff --git a/server/models/user.js b/server/models/user.js index c88fef8f..0ac286dd 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -39,12 +39,12 @@ userSchema.pre('save', function checkPassword(next) { // eslint-disable-line con }); }); -userSchema.virtual('id').get(function(){ - return this._id.toHexString(); +userSchema.virtual('id').get(function () { + return this._id.toHexString(); }); userSchema.set('toJSON', { - virtuals: true + virtuals: true }); diff --git a/server/routes/file.routes.js b/server/routes/file.routes.js index 8a881057..0146184c 100644 --- a/server/routes/file.routes.js +++ b/server/routes/file.routes.js @@ -7,4 +7,4 @@ router.route('/projects/:project_id/files').post(FileController.createFile); router.route('/projects/:project_id/files/:file_id').delete(FileController.deleteFile); router.route('/projects/:project_id/*?').get(FileController.getFileContent); -export default router; \ No newline at end of file +export default router; diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index c9884a60..d18a932e 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -1,7 +1,7 @@ import { Router } from 'express'; const router = new Router(); import path from 'path'; -import { renderIndex } from '../views/index' +import { renderIndex } from '../views/index'; // this is intended to be a temporary file // until i figure out isomorphic rendering diff --git a/server/utils/db_reformat.js b/server/utils/db_reformat.js index 9aca9c58..c311aacb 100644 --- a/server/utils/db_reformat.js +++ b/server/utils/db_reformat.js @@ -134,4 +134,4 @@ User.find({}) console.log('user saved'); }); }); - }); \ No newline at end of file + }); diff --git a/server/utils/db_reformat_start.js b/server/utils/db_reformat_start.js index 9d949a02..aea58a29 100644 --- a/server/utils/db_reformat_start.js +++ b/server/utils/db_reformat_start.js @@ -1,3 +1,3 @@ require('babel-register'); require('babel-polyfill'); -require('./db_reformat'); \ No newline at end of file +require('./db_reformat'); diff --git a/server/utils/previewGeneration.js b/server/utils/previewGeneration.js index 9e1abf91..57efe73d 100644 --- a/server/utils/previewGeneration.js +++ b/server/utils/previewGeneration.js @@ -13,13 +13,13 @@ function resolveLinksInString(content, files, projectId) { const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i; fileStrings = fileStrings || []; fileStrings.forEach(fileString => { - //if string does not begin with http or https + // if string does not begin with http or https if (fileString.match(fileStringRegex)) { const filePath = fileString.substr(1, fileString.length - 2); const resolvedFile = resolvePathToFile(filePath, files); if (resolvedFile) { if (resolvedFile.url) { - newContent = newContent.replace(filePath,resolvedFile.url); + newContent = newContent.replace(filePath, resolvedFile.url); } else if (resolvedFile.name.match(/(.+\.json$|.+\.txt$|.+\.csv$)/i)) { let resolvedFilePath = filePath; if (resolvedFilePath.startsWith('.')) { @@ -56,47 +56,47 @@ export function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) { } export function resolveScripts(sketchDoc, files, projectId) { - const scriptsInHTML = sketchDoc.getElementsByTagName('script'); - const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML); - scriptsInHTMLArray.forEach(script => { - if (script.getAttribute('src') && script.getAttribute('src').match(NOT_EXTERNAL_LINK_REGEX) !== null) { - const resolvedFile = resolvePathToFile(script.getAttribute('src'), files); - if (resolvedFile) { - if (resolvedFile.url) { - script.setAttribute('src', resolvedFile.url); - } else { - script.removeAttribute('src'); - script.innerHTML = resolvedFile.content; - } + const scriptsInHTML = sketchDoc.getElementsByTagName('script'); + const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML); + scriptsInHTMLArray.forEach(script => { + if (script.getAttribute('src') && script.getAttribute('src').match(NOT_EXTERNAL_LINK_REGEX) !== null) { + const resolvedFile = resolvePathToFile(script.getAttribute('src'), files); + if (resolvedFile) { + if (resolvedFile.url) { + script.setAttribute('src', resolvedFile.url); + } else { + script.removeAttribute('src'); + script.innerHTML = resolvedFile.content; } - } else if (!(script.getAttribute('src') && script.getAttribute('src').match(EXTERNAL_LINK_REGEX) !== null)) { - script.innerHTML = resolveLinksInString(script.innerHTML, files, projectId); } - }); - } + } else if (!(script.getAttribute('src') && script.getAttribute('src').match(EXTERNAL_LINK_REGEX) !== null)) { + script.innerHTML = resolveLinksInString(script.innerHTML, files, projectId); + } + }); +} export function resolveStyles(sketchDoc, files, projectId) { - const inlineCSSInHTML = sketchDoc.getElementsByTagName('style'); - const inlineCSSInHTMLArray = Array.prototype.slice.call(inlineCSSInHTML); - inlineCSSInHTMLArray.forEach(style => { - style.innerHTML = resolveLinksInString(style.innerHTML, files, projectId); - }); + const inlineCSSInHTML = sketchDoc.getElementsByTagName('style'); + const inlineCSSInHTMLArray = Array.prototype.slice.call(inlineCSSInHTML); + inlineCSSInHTMLArray.forEach(style => { + style.innerHTML = resolveLinksInString(style.innerHTML, files, projectId); + }); - const cssLinksInHTML = sketchDoc.querySelectorAll('link[rel="stylesheet"]'); - const cssLinksInHTMLArray = Array.prototype.slice.call(cssLinksInHTML); - cssLinksInHTMLArray.forEach(css => { - if (css.getAttribute('href') && css.getAttribute('href').match(NOT_EXTERNAL_LINK_REGEX) !== null) { - const resolvedFile = resolvePathToFile(css.getAttribute('href'), files); - if (resolvedFile) { - if (resolvedFile.url) { - css.setAttribute('href', resolvedFile.url); - } else { - const style = sketchDoc.createElement('style'); - style.innerHTML = `\n${resolvedFile.content}`; - sketchDoc.getElementsByTagName("head")[0].appendChild(style); - css.parentNode.removeChild(css); - } + const cssLinksInHTML = sketchDoc.querySelectorAll('link[rel="stylesheet"]'); + const cssLinksInHTMLArray = Array.prototype.slice.call(cssLinksInHTML); + cssLinksInHTMLArray.forEach(css => { + if (css.getAttribute('href') && css.getAttribute('href').match(NOT_EXTERNAL_LINK_REGEX) !== null) { + const resolvedFile = resolvePathToFile(css.getAttribute('href'), files); + if (resolvedFile) { + if (resolvedFile.url) { + css.setAttribute('href', resolvedFile.url); + } else { + const style = sketchDoc.createElement('style'); + style.innerHTML = `\n${resolvedFile.content}`; + sketchDoc.getElementsByTagName('head')[0].appendChild(style); + css.parentNode.removeChild(css); } } - }); - } + } + }); +}