diff --git a/client/constants.js b/client/constants.js index 7ef4983c01..c1945a8bb1 100644 --- a/client/constants.js +++ b/client/constants.js @@ -130,3 +130,5 @@ export const HIDE_HELP_MODAL = 'HIDE_HELP_MODAL'; export const HIDE_RUNTIME_ERROR_WARNING = 'HIDE_RUNTIME_ERROR_WARNING'; export const SHOW_RUNTIME_ERROR_WARNING = 'SHOW_RUNTIME_ERROR_WARNING'; export const SET_ASSETS = 'SET_ASSETS'; + +export const SET_P5_VERSION = 'SET_P5_VERSION'; diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 2e1c91634a..4dcc37acf5 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -9,13 +9,12 @@ function draw() { background(220); }`; -const defaultHTML = -` +const defaultHTML = (version = '0.5.0') => `
- - - + + + @@ -58,7 +57,7 @@ const initialState = () => { }, { name: 'index.html', - content: defaultHTML, + content: defaultHTML(), id: b, _id: b, fileType: 'file', @@ -227,6 +226,16 @@ const files = (state, action) => { } return file; }); + case ActionTypes.SET_P5_VERSION: + return state.map((file) => { + if (file.name === 'index.html') { + return { + ...file, + content: defaultHTML(action.data.version) + }; + } + return file; + }); default: return state; } diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index 3150da4b7a..3bc1de6eb2 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -68,17 +68,26 @@ export function validateAndLoginUser(previousPath, formProps, dispatch) { export function getUser() { return (dispatch) => { + let userResponse; axios.get(`${ROOT_URL}/session`, { withCredentials: true }) - .then((response) => { - dispatch({ - type: ActionTypes.AUTH_USER, - user: response.data - }); - dispatch({ - type: ActionTypes.SET_PREFERENCES, - preferences: response.data.preferences - }); - }) + .then((response) => { + userResponse = response; + return axios.get(`${ROOT_URL}/p5version`); + }) + .then(({ data }) => { + dispatch({ + type: ActionTypes.AUTH_USER, + user: userResponse.data + }); + dispatch({ + type: ActionTypes.SET_PREFERENCES, + preferences: userResponse.data.preferences + }); + dispatch({ + type: ActionTypes.SET_P5_VERSION, + data, + }); + }) .catch((response) => { dispatch(authError(response.data.error)); }); diff --git a/server/config.js b/server/config.js index cf7fb8e8a1..3322e30ec0 100644 --- a/server/config.js +++ b/server/config.js @@ -1,6 +1,7 @@ const config = { mongoURL: process.env.MONGO_URL || 'mongodb://localhost:27017/p5js-web-editor', port: process.env.PORT || 8000, + p5versionURL: 'https://p5js.org/download/version.json', }; export default config; diff --git a/server/controllers/meta.controller.js b/server/controllers/meta.controller.js new file mode 100644 index 0000000000..b08b533229 --- /dev/null +++ b/server/controllers/meta.controller.js @@ -0,0 +1,8 @@ +import rp from 'request-promise'; + +import config from '../config'; + +export function getP5Version(req, res) { // eslint-disable-line + rp({ uri: config.p5versionURL, json: true }) + .then(response => res.json(response)); +} diff --git a/server/examples.js b/server/examples.js index 73b3fd89d3..72cc8d197d 100644 --- a/server/examples.js +++ b/server/examples.js @@ -4,16 +4,18 @@ import mongoose from 'mongoose'; import objectID from 'bson-objectid'; import shortid from 'shortid'; import eachSeries from 'async/eachSeries'; + +import config from './config'; import User from './models/user'; import Project from './models/project'; -const defaultHTML = +const defaultHTML = version => ` - - - + + + @@ -189,44 +191,51 @@ function createProjectsInP5user(projectsInAllCategories) { _id: shortid.generate() }); } else { - newProject = new Project({ - name: project.projectName, - user: user._id, - files: [ - { - name: 'root', - id: r, - _id: r, - children: [a, b, c], - fileType: 'folder' - }, - { - name: 'sketch.js', - content: project.sketchContent, - id: a, - _id: a, - isSelectedFile: true, - fileType: 'file', - children: [] - }, - { - name: 'index.html', - content: defaultHTML, - id: b, - _id: b, - fileType: 'file', - children: [] - }, - { - name: 'style.css', - content: defaultCSS, - id: c, - _id: c, - fileType: 'file', - children: [] - } - ], - _id: shortid.generate() + const p5versionOptions = { + uri: config.p5versionURL, + json: true + }; + + rp(p5versionOptions).then((response) => { + newProject = new Project({ + name: project.projectName, + user: user._id, + files: [ + { + name: 'root', + id: r, + _id: r, + children: [a, b, c], + fileType: 'folder' + }, + { + name: 'sketch.js', + content: project.sketchContent, + id: a, + _id: a, + isSelectedFile: true, + fileType: 'file', + children: [] + }, + { + name: 'index.html', + content: defaultHTML(response.version), + id: b, + _id: b, + fileType: 'file', + children: [] + }, + { + name: 'style.css', + content: defaultCSS, + id: c, + _id: c, + fileType: 'file', + children: [] + } + ], + _id: shortid.generate() + }); }); } diff --git a/server/routes/aws.routes.js b/server/routes/aws.routes.js index 6367f32c73..fd5bb2555f 100644 --- a/server/routes/aws.routes.js +++ b/server/routes/aws.routes.js @@ -5,8 +5,11 @@ import isAuthenticated from '../utils/isAuthenticated'; const router = new Router(); router.post('/S3/sign', isAuthenticated, AWSController.signS3); + router.post('/S3/copy', isAuthenticated, AWSController.copyObjectInS3); + router.delete('/S3/:object_key', isAuthenticated, AWSController.deleteObjectFromS3); + router.get('/S3/:username/objects', AWSController.listObjectsInS3ForUser); export default router; diff --git a/server/routes/embed.routes.js b/server/routes/embed.routes.js index bea7e57350..698b0aff2e 100644 --- a/server/routes/embed.routes.js +++ b/server/routes/embed.routes.js @@ -4,6 +4,7 @@ import * as EmbedController from '../controllers/embed.controller'; const router = new Router(); router.get('/embed/:project_id', EmbedController.serveProject); + router.get('/full/:project_id', EmbedController.serveProject); export default router; diff --git a/server/routes/file.routes.js b/server/routes/file.routes.js index 36139c46ef..508c3f3756 100644 --- a/server/routes/file.routes.js +++ b/server/routes/file.routes.js @@ -5,6 +5,7 @@ import isAuthenticated from '../utils/isAuthenticated'; const router = new Router(); router.post('/projects/:project_id/files', isAuthenticated, FileController.createFile); + router.delete('/projects/:project_id/files/:file_id', isAuthenticated, FileController.deleteFile); export default router; diff --git a/server/routes/meta.routes.js b/server/routes/meta.routes.js new file mode 100644 index 0000000000..c20c571160 --- /dev/null +++ b/server/routes/meta.routes.js @@ -0,0 +1,8 @@ +import { Router } from 'express'; +import { getP5Version } from '../controllers/meta.controller'; + +const router = new Router(); + +router.get('/p5version', getP5Version); + +export default router; diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index b0d9eade85..2805e42af0 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -1,4 +1,5 @@ import { Router } from 'express'; + import { renderIndex } from '../views/index'; import { get404Sketch } from '../views/404Page'; import { userExists } from '../controllers/user.controller'; diff --git a/server/server.js b/server/server.js index 7cbaa42197..79b0047726 100644 --- a/server/server.js +++ b/server/server.js @@ -16,6 +16,7 @@ import config from '../webpack.config.dev'; // Import all required modules import serverConfig from './config'; +import meta from './routes/meta.routes'; import users from './routes/user.routes'; import sessions from './routes/session.routes'; import projects from './routes/project.routes'; @@ -80,6 +81,7 @@ app.use(session({ app.use(passport.initialize()); app.use(passport.session()); +app.use('/api', requestsOfTypeJSON(), meta); app.use('/api', requestsOfTypeJSON(), users); app.use('/api', requestsOfTypeJSON(), sessions); app.use('/api', requestsOfTypeJSON(), projects);