Page MenuHomeSealhub

No OneTemporary

diff --git a/lib/chip-types/file-manager.js b/lib/chip-types/file-manager.js
new file mode 100644
index 00000000..b53f947a
--- /dev/null
+++ b/lib/chip-types/file-manager.js
@@ -0,0 +1,53 @@
+var Promise = require("bluebird");
+var UUIDGenerator = require("uid");
+var path = require('path');
+var fse = require("fs-extra");
+var fs = require("fs");
+
+/**
+ * Manages files
+ * @class
+ */
+var FileManager = new function() {
+
+ this.save_file = function(payload, upload_path) {
+ var files = payload["files"];
+
+ console.log("file-manager:", payload);
+
+ if(files.hapi)
+ if(files.hapi.filename=='')
+ return [{ upload_path: "./resources/default/book.png"}];
+
+ var parent_tmp = module.parent;
+ var parent = null;
+ while(parent_tmp){
+ parent=parent_tmp;
+ parent_tmp = parent_tmp.parent;
+ }
+
+ var upload_path_abs = path.resolve(parent.filename, "../"+upload_path);
+
+ if (!fs.existsSync(upload_path_abs)) {
+ fse.mkdirs(upload_path_abs, function(err) {
+ if (err) return console.error(err);
+ });
+ }
+
+ if (!files[0]) {
+ files = [files];
+ }
+ for (var i in files) {
+ var upload_path_with_filename = upload_path_abs + "/" + files[i].hapi.filename;
+ files[i].pipe(fs.createWriteStream(upload_path_with_filename));
+ }
+
+ return files;
+
+ }
+}
+
+
+//wersjonowanie, historia dostępu, przetrzymywanie poprzednich wersji
+
+module.exports = FileManager;
diff --git a/lib/chip-types/uploader.js b/lib/chip-types/uploader.js
new file mode 100644
index 00000000..aa9e02ad
--- /dev/null
+++ b/lib/chip-types/uploader.js
@@ -0,0 +1,39 @@
+var fs = require("fs");
+var fse = require('fs-extra');
+var path = require('path');
+var uid = require('uid');
+
+module.exports = function(uploader, dispatcher, dependencies){
+ this.save_file = function(payload, upload_path) {
+ var photos = payload["photos"];
+
+ if(photos.hapi)
+ if(photos.hapi.filename=='')
+ return [{ upload_path: "./resources/default/book.png"}];
+
+ var parent_tmp = module.parent;
+ var parent = null;
+ while(parent_tmp){
+ parent=parent_tmp;
+ parent_tmp = parent_tmp.parent;
+ }
+
+ var upload_path_abs = path.resolve(parent.filename, "../"+upload_path);
+
+ if (!fs.existsSync(upload_path_abs)) {
+ fse.mkdirs(upload_path_abs, function(err) {
+ if (err) return console.error(err);
+ });
+ }
+
+ if (!photos[0]) {
+ photos = [photos];
+ }
+ for (var i in photos) {
+ var upload_path_with_filename = upload_path_abs + "/" + photos[i].hapi.filename;
+ photos[i].pipe(fs.createWriteStream(upload_path_with_filename));
+ }
+
+ }
+
+}
diff --git a/lib/dispatchers/biz/dispatcher-biz.js b/lib/dispatchers/biz/dispatcher-biz.js
index 6efc1c6c..9e099a75 100644
--- a/lib/dispatchers/biz/dispatcher-biz.js
+++ b/lib/dispatchers/biz/dispatcher-biz.js
@@ -1,20 +1,25 @@
var DispatcherDistributed = require("../dispatcher-distributed.js");
var DispatcherDistributedBIZ = new DispatcherDistributed("biz");
DispatcherDistributedBIZ.process_resource_manager_method = function(ResourceManager, method_name) {
return ResourceManager[method_name].bind(ResourceManager, this);
}
+DispatcherDistributedBIZ.process_file_manager_method = function(FileManager, method_name) {
+ return FileManager[method_name].bind(FileManager, this);
+}
+
+
DispatcherDistributedBIZ.process_service_method = function(service, service_name, method_name) {
return service[method_name].bind(service, this);
}
DispatcherDistributedBIZ.process_datastore_method = function(datastore, method_name) {
return function(){
return this.call_over_socket("datastore."+ method_name, arguments);
}.bind(this);
}
module.exports = DispatcherDistributedBIZ;
diff --git a/lib/dispatchers/db/dispatcher-db.js b/lib/dispatchers/db/dispatcher-db.js
index 11b8dc40..c36c85db 100644
--- a/lib/dispatchers/db/dispatcher-db.js
+++ b/lib/dispatchers/db/dispatcher-db.js
@@ -1,22 +1,28 @@
var DispatcherDistributed = require("../dispatcher-distributed.js");
var DispatcherDistributedDB = new DispatcherDistributed("db");
DispatcherDistributedDB.process_resource_manager_method = function(ResourceManager, method_name) {
return function(){
throw new Sealious.Errors.Error("DispatcherDistributedDB tried to call a ResourceManager method, which is not allowed.")
}
}
+DispatcherDistributedDB.process_file_manager_method = function(FileManager, method_name) {
+ return function(){
+ throw new Sealious.Errors.Error("DispatcherDistributedDB tried to call a FileManager method, which is not allowed.")
+ }
+}
+
DispatcherDistributedDB.process_service_method = function(service, service_name, method_name) {
return function(){
throw new Sealious.Errors.Error("DispatcherDistributedDB tried to call a service method, which is not allowed.")
}
}
DispatcherDistributedDB.process_datastore_method = function(datastore, method_name) {
return datastore[method_name];
}
module.exports = DispatcherDistributedDB;
diff --git a/lib/dispatchers/dispatcher-wrapper.js b/lib/dispatchers/dispatcher-wrapper.js
deleted file mode 100644
index 975bc12a..00000000
--- a/lib/dispatchers/dispatcher-wrapper.js
+++ /dev/null
@@ -1,74 +0,0 @@
-var Set = require("Set");
-
-function DispatcherWrapper(real_dispatcher, allowed_chip_longids){
-
- this.real_dispatcher = real_dispatcher;
-
-
-
- this.allowed_chip_longids = new Set();
- this.allowed_chip_longids.addAll(allowed_chip_longids);
-
-
- var functions_to_copy = [
- "resources_update",
- "resources_search_resource",
- "users_user_id_exists",
- "users_update_user_data",
- "users_delete_user",
- "users_get_all_users",
- "resources_find",
- "resources_get_access_mode",
- "resources_edit_resource_access_mode",
- "resources_get_by_id",
- "users_create_user",
- "users_get_user_data",
- "users_password_match",
- "users_user_exists",
- "database_query",
- "resources_search_by_mode"
- ];
-
- for(var i in functions_to_copy){
- this[functions_to_copy[i]] = this.real_dispatcher[functions_to_copy[i]];
- }
-
-}
-
-DispatcherWrapper.prototype = new function(){
-
- var that = this;
-
- this.resources_list_by_type = function(type_name){
- if(this.can_access("resource_type." + type_name)){
- return this.real_dispatcher.resources_list_by_type(type_name);
- }else{
- throw new Sealious.Errors.DependencyError("cannot access chip: resource_type."+type_name);
- }
- }
-
- this.resources_create = function(type_name, body, owner){
- if(this.can_access("resource_type." + type_name)){
- return this.real_dispatcher.resources_create.apply(this.real_dispatcher, arguments);
- }else{
- throw new Sealious.Errors.DependencyError("cannot access chip: resource_type."+type_name);
- }
- }
-
- this.resources_delete = function(type_name, body){
- if(this.can_access("resource_type." + type_name)){
- return this.real_dispatcher.resources_delete.apply(this.real_dispatcher, arguments);
- }
- }
-
- this.can_access = function(requested_chip_longid){
- if (this.allowed_chip_longids.has("resource_type.*") && requested_chip_longid.indexOf("resource_type") === 0) {
- return true;
- } else {
- return this.allowed_chip_longids.has(requested_chip_longid);
- }
- }
-
-}
-
-module.exports = DispatcherWrapper;
diff --git a/lib/dispatchers/dispatcher.js b/lib/dispatchers/dispatcher.js
index ced8938d..9ea85d50 100644
--- a/lib/dispatchers/dispatcher.js
+++ b/lib/dispatchers/dispatcher.js
@@ -1,71 +1,86 @@
var ChipManager = require("../chip-types/chip-manager.js");
var ResourceManager = require("../chip-types/resource-manager.js");
+var FileManager = require("../chip-types/file-manager.js");
Dispatcher = function(){
this.datastore_chip = null;
}
Dispatcher.prototype = new function(){
this.set_datastore = function(datastore_chip){
this.datastore_chip = datastore_chip;
}
this.init = function(){
this.set_datastore(ChipManager.get_datastore_chip());
this.process_all_resource_manager_methods();
+ this.process_all_file_manager_methods();
this.process_all_service_methods();
this.process_all_datastore_methods();
}
this.start = function(){
};
this.process_all_resource_manager_methods = function(){
this.resources = {};
if(this.process_resource_manager_method){
for(var method_name in ResourceManager){
if(ResourceManager[method_name] instanceof Function){
this.resources[method_name] = this.process_resource_manager_method(ResourceManager, method_name);
}
}
}else{
throw new Sealious.Errors.Error("Dispatcher should implement `process_resource_manager_method`");
}
}
+ this.process_all_file_manager_methods = function(){
+ this.files = {};
+ if(this.process_file_manager_method){
+ for(var method_name in FileManager){
+ if(FileManager[method_name] instanceof Function){
+ this.files[method_name] = this.process_file_manager_method(FileManager, method_name);
+ }
+ }
+ }else{
+ throw new Sealious.Errors.Error("Dispatcher should implement `process_file_manager_method`");
+ }
+ }
+
this.process_all_service_methods = function(){
if(this.process_service_method){
var services = ChipManager.get_chips_by_type("service");
this.services = {};
for(var service_name in services){
Sealious.Logger.info("service_name:", service_name);
var service = services[service_name];
this.services[service_name] = {};
for(var method_name in service){
if(service[method_name] instanceof Function){
this.services[service_name][method_name] = this.process_service_method(service, service_name, method_name);
}
}
}
}else{
throw new Sealious.Errors.Error("Dispatcher should implement `process_service_method`");
}
}
this.process_all_datastore_methods = function(){
if(this.process_datastore_method){
this.datastore = {};
for(var method_name in this.datastore_chip){
if(this.datastore_chip[method_name] instanceof Function){
this.datastore[method_name] = this.process_datastore_method(this.datastore_chip, method_name);
}
}
}else{
throw new Sealious.Errors.Error("Dispatcher should implement `process_datastore_method`");
}
}
}
module.exports = Dispatcher;
\ No newline at end of file
diff --git a/lib/dispatchers/local/dispatcher-local.js b/lib/dispatchers/local/dispatcher-local.js
index 904307ca..be652430 100644
--- a/lib/dispatchers/local/dispatcher-local.js
+++ b/lib/dispatchers/local/dispatcher-local.js
@@ -1,21 +1,26 @@
var Promise = require("bluebird");
var Dispatcher = require("../dispatcher.js");
var DispatcherLocal= Object.create(Dispatcher.prototype);
DispatcherLocal.process_service_method = function(service, service_name, method_name){
return service[method_name].bind(service, this);
}
DispatcherLocal.process_resource_manager_method = function(ResourceManager, method_name){
return ResourceManager[method_name].bind(ResourceManager, this);
}
+DispatcherLocal.process_file_manager_method = function(FileManager, method_name){
+ return FileManager[method_name].bind(FileManager, this);
+}
+
+
DispatcherLocal.process_datastore_method = function(datastore_chip, method_name){
return datastore_chip[method_name].bind(datastore_chip);
}
module.exports = DispatcherLocal;
diff --git a/lib/dispatchers/web/dispatcher-web.js b/lib/dispatchers/web/dispatcher-web.js
index ea69d7c8..6843fab5 100644
--- a/lib/dispatchers/web/dispatcher-web.js
+++ b/lib/dispatchers/web/dispatcher-web.js
@@ -1,23 +1,29 @@
var DispatcherDistributed = require("../dispatcher-distributed.js");
var DispatcherDistributedWEB = new DispatcherDistributed("web");
DispatcherDistributedWEB.process_resource_manager_method = function(ResourceManager, method_name){
return function(){
return this.call_over_socket("resources." + method_name, arguments);
}.bind(this);
}
+DispatcherDistributedWEB.process_file_manager_method = function(FileManager, method_name){
+ return function(){
+ return this.call_over_socket("files." + method_name, arguments);
+ }.bind(this);
+}
+
DispatcherDistributedWEB.process_service_method = function(service, service_name, method_name){
return function(){
return this.call_over_socket("services." + service_name + "." + method_name, arguments);
}.bind(this);
}
DispatcherDistributedWEB.process_datastore_method = function(datastore, method_name){
return function(){
return this.call_over_socket("datastore." + method_name, arguments);
}.bind(this);
}
module.exports = DispatcherDistributedWEB;
diff --git a/lib/module/module.js b/lib/module/module.js
index 21ed2794..7673bef9 100644
--- a/lib/module/module.js
+++ b/lib/module/module.js
@@ -1,165 +1,163 @@
var fs = require("fs");
var Set = require("Set");
var Promise = require("bluebird");
var ChipManager = require("../chip-types/chip-manager.js");
-var DispatcherWrapper = require("../dispatchers/dispatcher-wrapper.js");
-
function ChipDescription(param){
switch(typeof param){
case "string":
this.longid = param;
this.requires = [];
break;
case "object":
this.longid = param.longid;
this.requires = param.requires.map(function(string){return new ChipDescription(string)});
break;
}
this.type = this.longid.split(".")[0];
this.name = this.longid.split(".")[1];
}
//in order for Set to properly store instances of this description
ChipDescription.prototype.toString = function(){
return this.longid;
}
ChipActionDescription = function(verb, chip_description, module_object){
this.verb = verb; //"define" | "use";
this.chip_description = chip_description;
this.module = module_object;
}
ChipActionDescription.prototype = new function(){
this.execute = function(dispatcher){
var type = this.chip_description.type;
var chip_name = this.chip_description.name;
ChipManager.perform_chip_action(this.module.path, this, dispatcher);
}
this.toString = function(){
return this.verb + ":" + this.chip_description.longid;
}
}
var Module = function(path_to_module){
this.path = path_to_module;
this.info = null;
this.load_info();
this.initialized = false;
}
Module.prototype = new function(){
this.load_info = function(){
this.info = JSON.parse(fs.readFileSync(this.path + "/sealious-module.json"));
var that = this;
["modifies", "requires", "defines", "uses"].map(function(attribute_name){
that.info[attribute_name] = that.info[attribute_name] || [];
that.info[attribute_name] = that.info[attribute_name].map(function(param){
return new ChipDescription(param);
})
})
}
this.filter_chip_descriptions = function(attribute_names){
var ret = [];
for(var i in attribute_names){
var attribute_name = attribute_names[i];
ret = ret.concat(this.info[attribute_name]);
}
return ret;
}
this.get_required_chip_ids = function(){
return this.filter_chip_descriptions(["uses", "modifies"]).map(function(chip_description){
return chip_description.longid;
})
}
this.get_defined_chip_ids = function(){
return this.filter_chip_descriptions(["defines"]).map(function(chip_description){
return chip_description.longid;
})
}
this.get_initialization_instructions = function(){
var that = this;
var chip_initialization_instructions = [];
var to_load = ["defines", "uses"];
for(var i in to_load){
var attribute_name = to_load[i];
var to_add = this.info[attribute_name].map(function(description){
if(attribute_name=="uses"){
var verb = "use";
}else{
var verb = "define";
}
return new ChipActionDescription(verb, description, that);
})
chip_initialization_instructions = chip_initialization_instructions.concat(to_add);
}
return chip_initialization_instructions;
}
this.decide_chip_initialization_order = function(){
var init_instructions_list = [];
var accepted_instructions = new Set();
var possible_init_instructions = this.get_initialization_instructions();
var defined_so_far = new Set(this.info.requires);//we're assuming that ModuleManager took care of executing modules in such an order that all chips that are required by this module have been already initialized
do{
var initialized_in_this_iteration = 0;
for(var i in possible_init_instructions){
var current_init_instruction = possible_init_instructions[i];
if(accepted_instructions.has(current_init_instruction)){
//skipping any chips that have already been put on initialization list
continue;
}
var chip_description = current_init_instruction.chip_description;
var can_initialize = true;
var required = chip_description.requires;
for(var j in required){
if(!defined_so_far.has(required[j])){
can_initialize = false;
break;
}
}
if(can_initialize){
initialized_in_this_iteration++;
accepted_instructions.add(current_init_instruction)
defined_so_far.add(current_init_instruction.chip_description);
init_instructions_list.push(current_init_instruction);
}
}
}while(initialized_in_this_iteration!=0 && init_instructions_list.length!=possible_init_instructions.length);
if(init_instructions_list.length!=possible_init_instructions.length){
throw new Sealious.Error.DependencyError("cannot initialize all chips from module ", this.info.name, "because of unmet dependencies"); //~
}
return init_instructions_list;
}
this.initialize = function(dispatcher){
var init_instructions_list = this.decide_chip_initialization_order();
init_instructions_list.map(function(init_instruction){
init_instruction.execute(dispatcher);
});
this.initialized = true;
}
this.start = function(chip_types_to_start){
var promises = [];
for(var i in chip_types_to_start){
var chip_type = chip_types_to_start[i];
promises.push(ChipManager.start_chips_from_module(this.path, chip_types_to_start));
}
return Promise.all(promises);
}
}
module.exports = Module;
\ No newline at end of file
diff --git a/package.json b/package.json
index 64c05df7..93696a91 100755
--- a/package.json
+++ b/package.json
@@ -1,44 +1,47 @@
{
"name": "sealious",
"version": "0.3.2",
"description": "",
"main": "./lib/main.js",
"scripts": {
"test": "mocha ./tests/test.js && mocha ./tests/test.js -R travis-cov"
},
"repository": {
"type": "git",
"url": "https://github.com/Sealious/Sealious/"
},
"keywords": [
"sealious"
],
"author": "Kuba Orlik",
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/Sealious/Sealious/issues"
},
"dependencies": {
"Set": "~0.4.1",
"bluebird": "~2.9.9",
+ "fs": "0.0.2",
+ "fs-extra": "^0.18.2",
"merge": "^1.2.0",
+ "path": "^0.11.14",
"socket.io": "~1.2.0",
"socket.io-client": "~1.2.0",
- "winston": "^1.0.0",
- "uid": "0.0.2"
+ "uid": "0.0.2",
+ "winston": "^1.0.0"
},
"devDependencies": {
"blanket": "1.1.6",
"mocha": "*",
"should": "~4.4.0",
"travis-cov ": "*"
},
"config": {
"blanket": {
"pattern": "node_modules"
}
},
"peerDependencies": {
"sealious-base-chips": "^0.2.0"
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 2, 18:37 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1030484
Default Alt Text
(19 KB)

Event Timeline