Permalink
Browse files

successful to load project from mongo db

  • Loading branch information...
ipbebnev committed Jan 9, 2016
1 parent a2c6863 commit 366d27acdc4bd9ca033f61ae9718da54fcef93e7
Showing with 89 additions and 87 deletions.
  1. +87 −3 default/index.js
  2. +2 −84 routes/utils.js
View
@@ -43,12 +43,28 @@
* path: "/dir1/dir2/file2"
* }]
*/
var util = require('util'); // for debug
var async = require('async');
var fs = require("fs");
var Path = require("path");
var MemoryStream = require("memorystream");
var Tar = require("tar-stream");
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
BSON = require('mongodb').pure().BSON,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
assert = require('assert');
var config = require("../routes/config");
var BUFFER = "BUFFER";
var FILE_STREAM = "FILE STREAM";
var TAR_STREAM = "TAR STREAM";
@@ -137,9 +153,77 @@ function cacheProjectFiles() {
projects.splice(projects.indexOf("index.js"), 1);
projects.forEach(function(projectName) {
var project = readDirectory(Path.join(__dirname, projectName));
_defaultProjectPaths[projectName] = project.paths;
_defaultProjects[projectName] = project.contents;
// create mongo connection:
MongoClient.connect("mongodb://"+config.mongoHost+"/"+config.mongoBase, function(err, db) {
if(err) return console.dir("Error at connection to mongoDB "+err);
var grid = new Grid(db, 'fs');
var collection = db.collection(config.sessionId);
// check the collection of default project is already exists
collection.findOne({_id : projectName}, function(err, doc) {
// check existing mongo project
if (!doc) {
console.log('case from hdd');
// copy files from HDD to Mongo
var project = readDirectory(Path.join(__dirname, projectName));
var bufferObjFilesId = [];
// send all files to mongo grid
for (var i = 0, len = project.contents.length; i < len; i++) {
grid.put(project.contents[i].buffer, {metadata:{category:'text'}, content_type: 'text', filename: project.contents[i].path}, function(err, fileInfo) {
if(!!err) {
console.error("ERROR of but file to MONGO: ", err.message);
}
// saving all files id to mongo
bufferObjFilesId.push(fileInfo._id);
db.collection( config.sessionId ).update( { _id : projectName }, {bufferObjFilesId: bufferObjFilesId} , {upsert:true, w: 1}, function (err, inserted) {
if (!!err) {
console.log("Error of updating id of saved to MONGO file: "+err.message);
}
});
});
}
// return project files directly from HDD
_defaultProjectPaths[projectName] = project.paths;
_defaultProjects[projectName] = project.contents;
// if files already exits in MONGO return it
} else {
console.log('case from mongo');
// reading existing project from MONGO
GridStore.list(db, function(err, items) {
var projects = {
contents: [],
paths: []
};
async.filter(items, function (item) {
GridStore.read(db, item, function(err, fileData) {
projects.contents.push({
buffer: fileData,
path: item
});
projects.paths.push(item);
// check the end of reading
if (projects.contents.length === items.length) {
// return project
_defaultProjects[projectName] = projects.contents;
_defaultProjectPaths[projectName] = projects.paths;
}
});
}, function (results) {});
});
}
});
});
});
}
View
@@ -2,22 +2,9 @@ var request = require("request");
var url = require("url");
var NodeFormData = require("form-data");
var async = require("async");
var util = require('util');
var defaultProject = require("../default");
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
BSON = require('mongodb').pure().BSON,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
assert = require('assert');
function createProject(config, user, data, callback) {
var createURL = config.publishURL + "/projects";
var project;
@@ -238,76 +225,7 @@ function getProjectFileMetadata(config, user, projectId, callback) {
function getProjectFileTar(config, user, projectId) {
if(!user) {
// create mongo connection:
MongoClient.connect("mongodb://"+config.mongoHost+"/"+config.mongoBase, function(err, db) {
if(err) return console.dir(err);
var grid = new Grid(db, 'fs');
var collection = db.collection(config.mongoCollection);
collection.findOne({_id : config.sessionId}, function(err, doc) {
// check some existing mongo project
if (!doc) {
console.log('// get files from HDD/');
// get files from HDD
var bufferObj = defaultProject.getAsBuffers(config.DEFAULT_PROJECT_TITLE);
var bufferFileNames = defaultProject.getPaths(config.DEFAULT_PROJECT_TITLE);
var bufferObjFilesId = [];
// send all files to mongo grid
for (var i = 0, len = bufferObj.length; i < len; i++) {
grid.put(bufferObj[i].buffer, {metadata:{category:'text'}, content_type: 'text', filename: bufferFileNames[i]}, function(err, fileInfo) {
if(!!err) {
console.error("ERROR: ", err.message);
}
// saving all files id to mongo
bufferObjFilesId.push(fileInfo._id);
db.collection(config.mongoCollection).update({_id : config.sessionId},{bufferObjFilesId: bufferObjFilesId} , {upsert:true, w: 1}, function (err, inserted) {
if (!!err) {
console.log(err.message);
}
});
});
}
// return project directly from HDD
return bufferObj;
} else { // return existing project from MONGO
console.log('// return existing project from MONGO');
GridStore.list(db, function(err, items) {
var bufferExistingObj = [];
async.filter(items, function (item) {
GridStore.read(db, item, function(err, fileData) {
bufferExistingObj.push({
buffer: fileData,
path: item
});
if (bufferExistingObj.length === items.length) {
return bufferExistingObj;
}
});
}, function (results) {});
});
// setTimeout(function() {
// console.log("existing \n"+util.inspect(bufferExistingObj, false, null));
// return bufferExistingObj;
// }, 500);
}
});
// setTimeout(function() {
// var collection = db.collection(config.mongoCollection);
// collection.findOne( {_id : config.sessionId}, function(err, item) {
// // console.log("ITEM >>> \n"+util.inspect(item.bufferObjFilesId, false, null));
// // grid.get(item.bufferObjFilesId[0], function(err, data) {
// // console.log("file >>> " +data.metadata.filename);
// // });
// })
// }, 1000);
});
// return defaultProject.getAsTar(config.DEFAULT_PROJECT_TITLE);
return defaultProject.getAsTar(config.DEFAULT_PROJECT_TITLE);
}
var url = config.publishURL + "/projects/" + projectId + "/files/tar";
@@ -363,4 +281,4 @@ module.exports = {
getProjectFileTar: getProjectFileTar,
getRemixedProjectFileMetadata: getRemixedProjectFileMetadata,
getRemixedProjectFileTar: getRemixedProjectFileTar
};
};

0 comments on commit 366d27a

Please sign in to comment.