Skip to content

Instantly share code, notes, and snippets.

@cadecairos
Created March 26, 2013 15:01
Show Gist options
  • Save cadecairos/5246033 to your computer and use it in GitHub Desktop.
Save cadecairos/5246033 to your computer and use it in GitHub Desktop.
diff --git a/routes/index.js b/routes/index.js
index 2e08b35..0207cdd 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -106,72 +106,73 @@ module.exports = function routesCtor( app, User, filter, sanitizer, stores, util
app.post( '/api/project/:id?',
filter.isLoggedIn, filter.isStorageAvailable,
function( req, res ) {
-
- var files;
-
- var projectData = req.body;
-
- if ( req.body.id ) {
- files = datauri.filterProjectDataURIs( projectData.data, utils.generateDataURIPair );
-
- User.updateProject( req.session.email, req.body.id, projectData, function( err, doc, imagesToDestroy ) {
- if ( err ) {
- res.json( { error: err }, 500 );
- return;
- }
-
- if ( imagesToDestroy ) {
- imagesToDestroy.forEach( function( imageReference ) {
- stores.images.remove( imageReference.filename );
- });
- }
-
- if ( files && files.length > 0 ) {
- linkAndSaveImageFiles( files, doc.id, function( err ) {
- if ( err ) {
- res.json( { error: 'Unable to store data-uris.', }, 500 );
- return;
- }
- res.json( { error: 'okay', project: doc, imageURLs: files.map( function( file ) { return file.getJSONMetaData(); } ) }, 200 );
- });
- }
- else {
- res.json( { error: 'okay', project: doc } );
- }
- metrics.increment( 'project.save' );
- });
- } else {
- files = datauri.filterProjectDataURIs( projectData.data, utils.generateDataURIPair );
-
- User.createProject( req.session.email, projectData, function( err, doc ) {
- if ( err ) {
- res.json( { error: err }, 500 );
- metrics.increment( 'error.save' );
- return;
- }
-
- if ( files && files.length > 0 ) {
- linkAndSaveImageFiles( files, doc.id, function( err ) {
- if ( err ) {
- res.json( { error: 'Unable to store data-uris.', }, 500 );
- metrics.incremenet( 'error.save.store-data-uris' );
- return;
- }
- res.json( { error: 'okay', projectId: doc.id, imageURLs: files.map( function( file ) { return file.getJSONMetaData(); } ) }, 200 );
- metrics.increment( 'project.images-upload', files.length );
- });
- }
- else {
- // Send back the newly added row's ID
- res.json( { error: 'okay', projectId: doc.id }, 200 );
- }
-
- metrics.increment( 'project.create' );
- if ( doc.remixedFrom ) {
- metrics.increment( 'project.remix' );
- }
- });
- }
+ setTimeout(function() {
+ var files;
+
+ var projectData = req.body;
+
+ if ( req.body.id ) {
+ files = datauri.filterProjectDataURIs( projectData.data, utils.generateDataURIPair );
+
+ User.updateProject( req.session.email, req.body.id, projectData, function( err, doc, imagesToDestroy ) {
+ if ( err ) {
+ res.json( { error: err }, 500 );
+ return;
+ }
+
+ if ( imagesToDestroy ) {
+ imagesToDestroy.forEach( function( imageReference ) {
+ stores.images.remove( imageReference.filename );
+ });
+ }
+
+ if ( files && files.length > 0 ) {
+ linkAndSaveImageFiles( files, doc.id, function( err ) {
+ if ( err ) {
+ res.json( { error: 'Unable to store data-uris.', }, 500 );
+ return;
+ }
+ res.json( { error: 'okay', project: doc, imageURLs: files.map( function( file ) { return file.getJSONMetaData(); } ) }, 200 );
+ });
+ }
+ else {
+ res.json( { error: 'okay', project: doc } );
+ }
+ metrics.increment( 'project.save' );
+ });
+ } else {
+ files = datauri.filterProjectDataURIs( projectData.data, utils.generateDataURIPair );
+
+ User.createProject( req.session.email, projectData, function( err, doc ) {
+ if ( err ) {
+ res.json( { error: err }, 500 );
+ metrics.increment( 'error.save' );
+ return;
+ }
+
+ if ( files && files.length > 0 ) {
+ linkAndSaveImageFiles( files, doc.id, function( err ) {
+ if ( err ) {
+ res.json( { error: 'Unable to store data-uris.', }, 500 );
+ metrics.incremenet( 'error.save.store-data-uris' );
+ return;
+ }
+ res.json( { error: 'okay', projectId: doc.id, imageURLs: files.map( function( file ) { return file.getJSONMetaData(); } ) }, 200 );
+ metrics.increment( 'project.images-upload', files.length );
+ });
+ }
+ else {
+ // Send back the newly added row's ID
+ res.json( { error: 'okay', projectId: doc.id }, 200 );
+ }
+
+ metrics.increment( 'project.create' );
+ if ( doc.remixedFrom ) {
+ metrics.increment( 'project.remix' );
+ }
+ });
+ }
+ }, 5000 );
});
// We have a separate remix API for unsecured and sanitized access to projects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment