Permalink
Browse files

Read and write to local disc for development

Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
  • Loading branch information...
1 parent 67a92d4 commit eb252038acafccdaa67fbd8f22bc7d9bffc6ca7b @rwaldron committed Mar 11, 2012
Showing with 74 additions and 52 deletions.
  1. +74 −52 app.js
View
126 app.js
@@ -3,26 +3,32 @@ var express = require("express"),
fs = require("fs"),
cloudfiles = require("cloudfiles");
-var portInUse,
+
+var portInUse, cloudConf, cloud,
app = express.createServer(),
- io = socket.listen( app ),
- cloudConf = JSON.parse( fs.readFileSync( __dirname + "/.config", "utf-8" ) ),
- cloud = cloudfiles.createClient({
- auth: {
- username: cloudConf.username,
- apiKey: cloudConf.apiKey
- }
- });
+ io = socket.listen( app );
-cloud.setAuth(function() {
- // console.log( cloud );
- console.log( "Authorized Cloud Files" );
- // cloud.getContainer( "dmv", function( err, container ) {
- // container.getFiles(function( err, files ) {
- // console.log( files );
- // });
- // });
-});
+
+if ( process.env.NODE_ENV ) {
+ cloudConf = JSON.parse( fs.readFileSync( __dirname + "/.config", "utf-8" ) );
+
+ cloud = cloudfiles.createClient({
+ auth: {
+ username: cloudConf.username,
+ apiKey: cloudConf.apiKey
+ }
+ });
+
+ cloud.setAuth(function() {
+ // console.log( cloud );
+ console.log( "Authorized Cloud Files" );
+ // cloud.getContainer( "dmv", function( err, container ) {
+ // container.getFiles(function( err, files ) {
+ // console.log( files );
+ // });
+ // });
+ });
+}
// Express app Configuration
@@ -89,62 +95,78 @@ io.sockets.on( "connection", function( client ) {
fs.write( file, buffer, 0, buffer.length, 0, function( err, data ) {
if ( err == null ) {
- cloud.addFile( cloudConf.container, { remote: filename, local: filepath }, function( err, uploaded ) {
- if ( uploaded ) {
- // File has been uploaded
- console.log( "Uploaded to Cloud Files: " + filename );
-
- // Stream new file name to client
- toClient([ filename ]);
- // TODO: Delete local copy once index store is in place
- }
- });
+ if ( cloud ) {
+ cloud.addFile( cloudConf.container, { remote: filename, local: filepath }, function( err, uploaded ) {
+ if ( uploaded ) {
+ // File has been uploaded
+ console.log( "Uploaded to Cloud Files: " + filename );
+
+ // Stream new file name to client
+ streamToClient([ filename ]);
+ }
+ });
+ } else {
+ streamToClient([ filename ]);
+ }
+
+ // TODO: Delete local copy once index store is in place
}
});
});
client.on( "list:request", streamList );
+ // TODO: Refactor all of this code, it stinks.
function streamList( data ) {
- var size,
- list = [],
- id = data.id;
+ var id = data.id,
+ filepath = "public/saved/";
+
+ if ( cloud ) {
+ // Request Container and Files from Cloud Files
+ cloud.getContainer( cloudConf.container, function( err, container ) {
+ container.getFiles(function( err, files ) {
+ streamFilter( id, files.map(function( data ) { return data.name; }) );
+ });
+ });
+ } else {
+ // Look locally for development
+ fs.readdir( filepath, function( err, files ) {
+ streamFilter( id, files );
+ });
+ }
+ }
- // Request Container and Files from Cloud Files
- cloud.getContainer( cloudConf.container, function( err, container ) {
- container.getFiles(function( err, files ) {
+ function streamFilter( id, files ) {
+ var size,
+ list = [];
- files = files.filter(function( file ) { return (new RegExp("^" + id )).test( file.name ); });
- size = files.length;
+ files = files.filter(function( file ) { return (new RegExp("^" + id )).test( file ); });
+ size = files.length;
- files.forEach(function( file, index ) {
- // Push into array for streaming
- list.push( file.name );
+ files.forEach(function( file, index ) {
+ // Push into array for streaming
+ list.push( file );
- // Every 5th image, send to client and reset the list
- // If we've reached the end, send to client and reset the list
- if ( index % 5 === 0 || index === size - 1 ) {
- toClient( list );
+ // Every 5th image, send to client and reset the list
+ // If we've reached the end, send to client and reset the list
+ if ( index % 5 === 0 || index === size - 1 ) {
+ streamToClient( list );
- // Clear file array
- list = [];
- }
- });
- });
+ // Clear file array
+ list = [];
+ }
});
}
- function toClient( list ) {
+ function streamToClient( list ) {
io.sockets.emit( "list:response", {
+ path: cloud ? "http://c309459.r59.cf1.rackcdn.com/" : "/saved/",
files: list
});
}
});
-
-
-
app.listen( process.env.PORT || 3000 );
portInUse = app.address() ? app.address().port : 80;

0 comments on commit eb25203

Please sign in to comment.