Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions lib/index-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,49 @@ module.exports = function(config, auth, storage) {
Handlebars.registerPartial('entry', fs.readFileSync(require.resolve('./GUI/entry.hbs'), 'utf8'))
var template = Handlebars.compile(fs.readFileSync(require.resolve('./GUI/index.hbs'), 'utf8'))
}
app.get('/', function(req, res, next) {
var base = config.url_prefix
? config.url_prefix.replace(/\/$/, '')
: req.protocol + '://' + req.get('host')
res.setHeader('Content-Type', 'text/html')

storage.get_local(function(err, packages) {
// returns an object of local packages
function get_library(user, cb, getAllVersions) {
getAllVersions = getAllVersions || false;
var getFn = getAllVersions ? storage.get_local_all_versions.bind(storage) : storage.get_local.bind(storage);
getFn(function(err, packages) {
if (err) throw err // that function shouldn't produce any
async.filterSeries(packages, function(package, cb) {
auth.allow_access(package.name, req.remote_user, function(err, allowed) {
auth.allow_access(package.name, user, function(err, allowed) {
setImmediate(function () {
cb(!err && allowed)
})
})
}, function(packages) {
next(template({
name: config.web && config.web.title ? config.web.title : 'Sinopia',
packages: packages,
baseUrl: base,
username: req.remote_user.name,
}))
})
}, cb);
})
}

app.get('/-/library/:all?', function(req, res, next) {
var all = req.params.all || false;
get_library(req.remote_user, function(packages) {
next(packages);
}, all);
});

app.get('/', function(req, res, next) {
var base = config.url_prefix
? config.url_prefix.replace(/\/$/, '')
: req.protocol + '://' + req.get('host')

res.setHeader('Content-Type', 'text/html')

get_library(req.remote_user, function(packages) {
next(template({
name: config.web && config.web.title ? config.web.title : 'Sinopia',
packages: packages,
baseUrl: base,
username: req.remote_user.name,
}));
});

})


// Static
app.get('/-/static/:filename', function(req, res, next) {
var file = __dirname + '/static/' + req.params.filename
Expand Down Expand Up @@ -143,4 +161,3 @@ module.exports = function(config, auth, storage) {
})
return app
}

27 changes: 26 additions & 1 deletion lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,32 @@ Storage.prototype.get_local = function(callback) {
callback(null, [])
}
}
//TODO: DRY
Storage.prototype.get_local_all_versions = function(callback) {
var self = this
var locals = this.config.localList.get()
var packages = []

var getPackage = function(i) {
self.local.get_package(locals[i], function(err, info) {
if (!err) {
packages.push(info);
}

if (i >= locals.length - 1) {
callback(null, packages)
} else {
getPackage(i + 1)
}
})
}

if (locals.length) {
getPackage(0)
} else {
callback(null, [])
}
}

// function fetches package information from uplinks and synchronizes it with local data
// if package is available locally, it MUST be provided in pkginfo
Expand Down Expand Up @@ -528,4 +554,3 @@ Storage._merge_versions = function(local, up, config) {
}
}
}