Commit 28a72f8b authored by Leo Iannacone's avatar Leo Iannacone

added utils.js - moved there some common functions

parent 97f1ef29
...@@ -9,6 +9,7 @@ var express = require('express') ...@@ -9,6 +9,7 @@ var express = require('express')
, send = require('./send.js') , send = require('./send.js')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, utils = require('./utils.js')
var app = module.exports = express.createServer(); var app = module.exports = express.createServer();
...@@ -38,7 +39,7 @@ var io = require('socket.io').listen(app); ...@@ -38,7 +39,7 @@ var io = require('socket.io').listen(app);
// Routes // Routes
app.get('/', routes.index); app.get('/', routes.index);
function watcher_on_socket(event_name, socket, data, watch_path, updater) { function watch_dir(event_name, socket, data, watch_path, updater) {
name = "watcher-" + event_name name = "watcher-" + event_name
socket.get(name, function (err, watcher) { socket.get(name, function (err, watcher) {
if (watcher) if (watcher)
...@@ -53,34 +54,32 @@ function watcher_on_socket(event_name, socket, data, watch_path, updater) { ...@@ -53,34 +54,32 @@ function watcher_on_socket(event_name, socket, data, watch_path, updater) {
}) })
} }
function check_data_distribution(data) {
return data && data.distribution && data.distribution.name
}
function check_data_package(data) {
return check_data_distribution(data) && data.package && data.package.name && data.package.version
}
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
send.distributions(socket); send.distributions(socket);
// send distribution packages // send distribution packages
socket.on('get_distribution_packages', function (data) { socket.on('get_distribution_packages', function (data) {
if (! check_data_distribution(data)) if (! utils.check_data_distribution(data))
return return
distribution_path = path.join(config.debomatic_path, data.distribution.name, 'pool') distribution_path = path.join(config.debomatic_path, data.distribution.name, 'pool')
watcher_on_socket('get_distribution_packages', socket, data, distribution_path, send.distribution_packages) watch_dir('get_distribution_packages', socket, data, distribution_path, send.distribution_packages)
send.distribution_packages(socket, data); send.distribution_packages(socket, data);
}) })
socket.on('get_package_file_list', function(data) { socket.on('get_package_file_list', function(data) {
if (! check_data_package(data)) if (! utils.check_data_package(data))
return return
package_path = path.join(config.debomatic_path, data.distribution.name, 'pool', data.package.name + "_" + data.package.version) package_path = utils.get_package_path(data)
watcher_on_socket('get_package_file_list', socket, data, package_path, send.package_file_list) watch_dir('get_package_file_list', socket, data, package_path, send.package_file_list)
send.package_file_list(socket, data) send.package_file_list(socket, data)
}) })
socket.on('get_file', function (data){
if (! utils.check_data_file(data))
return
send.file(socket, data)
})
}); });
......
var fs = require('fs') var fs = require('fs')
, path = require('path') , path = require('path')
, config = require('./config.js') , config = require('./config.js')
, utils = require('./utils.js')
var BASE_DIR = config.debomatic_path;
function __get_files_list(dir, onlyDirectories, callback) { function __get_files_list(dir, onlyDirectories, callback) {
fs.readdir(dir, function(err, files){ fs.readdir(dir, function(err, files){
...@@ -56,9 +55,7 @@ function __get_files_list_from_package(package_path, callback) { ...@@ -56,9 +55,7 @@ function __get_files_list_from_package(package_path, callback) {
} }
function __send_package_files_list (socket, data) { function __send_package_files_list (socket, data) {
distro_path = path.join(BASE_DIR, data.distribution.name, 'pool'); package_path = utils.get_package_path(data)
p = data.package.name + "_" + data.package.version
package_path = path.join(distro_path, p)
__get_files_list_from_package(package_path, function(package_files){ __get_files_list_from_package(package_path, function(package_files){
data.package.files = package_files.files data.package.files = package_files.files
data.package.debs = package_files.debs data.package.debs = package_files.debs
...@@ -68,7 +65,7 @@ function __send_package_files_list (socket, data) { ...@@ -68,7 +65,7 @@ function __send_package_files_list (socket, data) {
} }
function __send_distribution_packages (socket, data) { function __send_distribution_packages (socket, data) {
distro_path = path.join(BASE_DIR, data.distribution.name, 'pool'); distro_path = utils.get_distribution_pool_path(data)
__get_files_list(distro_path, true, function (packages) { __get_files_list(distro_path, true, function (packages) {
data.distribution.packages = [] data.distribution.packages = []
packages.forEach( function (p) { packages.forEach( function (p) {
...@@ -76,7 +73,7 @@ function __send_distribution_packages (socket, data) { ...@@ -76,7 +73,7 @@ function __send_distribution_packages (socket, data) {
info = p.split('_') info = p.split('_')
pack.name = info[0] pack.name = info[0]
pack.version = info[1] pack.version = info[1]
if(data.package && if( data.package &&
pack.name == data.package.name && pack.name == data.package.name &&
pack.version == data.package.version ) { pack.version == data.package.version ) {
pack.selected = true; pack.selected = true;
...@@ -87,10 +84,19 @@ function __send_distribution_packages (socket, data) { ...@@ -87,10 +84,19 @@ function __send_distribution_packages (socket, data) {
}); });
} }
function __send_file (socket, data) {
file_path = utils.get_file_path(data)
fs.readFile(file_path, 'utf8', function (err, content) {
if (err) return;
data.file.content = content
socket.emit('file', data)
});
}
debomatic_sender = { debomatic_sender = {
distributions: function(socket) { distributions: function(socket) {
__get_files_list(BASE_DIR, true, function(distros){ __get_files_list(config.debomatic_path, true, function(distros){
socket.emit('distributions', distros); socket.emit('distributions', distros);
}); });
}, },
...@@ -101,6 +107,10 @@ debomatic_sender = { ...@@ -101,6 +107,10 @@ debomatic_sender = {
distribution_packages: function(socket, data) { distribution_packages: function(socket, data) {
__send_distribution_packages(socket, data) __send_distribution_packages(socket, data)
},
file: function(socket, data) {
__send_file(socket, data)
} }
} }
......
var path = require('path')
, config = require('./config.js')
function __check_data_distribution(data) {
return data && data.distribution && data.distribution.name
}
function __check_data_package(data) {
return __check_data_distribution(data) && data.package && data.package.name && data.package.version
}
function __check_data_file(data) {
return __check_data_package(data) && data.file.name
}
function __get_distribution_pool_path(data) {
return path.join(config.debomatic_path, data.distribution.name, 'pool')
}
function __get_package_path(data) {
return path.join(__get_distribution_pool_path(data), data.package.name + '_' + data.package.version)
}
function __get_file_path(data) {
return path.join(__get_package_path(data), data.file.name)
}
utils = {
check_data_distribution: function(data) {
return __check_data_distribution(data)
},
check_data_package: function(data) {
return __check_data_package(data)
},
check_data_file: function(data) {
return __check_data_file(data)
},
get_distribution_pool_path: function(data) {
return __get_distribution_pool_path(data)
},
get_package_path: function(data) {
return __get_package_path(data)
},
get_file_path: function(data) {
return __get_file_path(data)
},
}
module.exports = utils
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment