Skip to content

Commit b0fec1b

Browse files
committed
Organize assets in subfolders.
1 parent 003e8dd commit b0fec1b

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed
File renamed without changes.
File renamed without changes.

lib/controllers/AssetsController.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,31 @@ function AssetsController(options) {
1818
var assetsPath = options.assetsPath || '/assets/';
1919
this._matcher = new RegExp('^' + Util.toRegExp(assetsPath) + '(.+)|^/(\\w*)\\.ico$');
2020

21-
// Read assets into memory
21+
// Read all assets
2222
var assetsFolder = options.assetsFolder || path.join(__dirname, '../../assets/');
23-
this._assets = fs.readdirSync(assetsFolder).reduce(function (assets, filename) {
24-
var assetType = mime.lookup(filename);
25-
return assets[filename.replace(/[.][^.]+$/, '')] = {
26-
type: assetType.indexOf('text/') ? assetType : assetType + ';charset=utf-8',
27-
contents: fs.readFileSync(path.join(assetsFolder, filename)),
28-
}, assets;
29-
}, {});
23+
this._assets = {};
24+
this._readAssetsFolder(assetsFolder, '');
3025
}
3126
Controller.extend(AssetsController);
3227

28+
// Recursively reads assets in the folder, assigning them to the URL path
29+
AssetsController.prototype._readAssetsFolder = function (assetsFolder, assetsPath) {
30+
fs.readdirSync(assetsFolder).forEach(function (name) {
31+
var filename = path.join(assetsFolder, name), stats = fs.statSync(filename);
32+
// Read an asset file into memory
33+
if (stats.isFile()) {
34+
var assetType = mime.lookup(filename);
35+
this._assets[assetsPath + name.replace(/[.][^.]+$/, '')] = {
36+
type: assetType.indexOf('text/') ? assetType : assetType + ';charset=utf-8',
37+
contents: fs.readFileSync(filename),
38+
};
39+
}
40+
// Read all asset files in a folder
41+
else if (stats.isDirectory())
42+
this._readAssetsFolder(filename, assetsPath + name + '/');
43+
}, this);
44+
};
45+
3346
// Try to serve the requested asset
3447
AssetsController.prototype._handleRequest = function (request, response, next) {
3548
var assetMatch = request.url.match(this._matcher), asset;

lib/views/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<head>
55
<meta charset="utf-8">
66
<title><%= title || header || 'Linked Data Fragments Server' %></title>
7-
<link rel="stylesheet" href="<%= assetsPath %>style" />
7+
<link rel="stylesheet" href="<%= assetsPath %>styles/ldf-server" />
88
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:700italic,400,700|Droid+Sans+Mono" type="text/css" />
99
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
1010
</head>
1111
<body>
1212
<header>
1313
<h1><a href="<%= baseURL %>"><%= header || 'Linked Data Fragments Server' %></a></h1>
1414
<figure class="logo">
15-
<a href="http://linkeddatafragments.org/"><img src="<%= assetsPath %>logo" alt="Linked Data Fragments" /></a>
15+
<a href="http://linkeddatafragments.org/"><img src="<%= assetsPath %>images/logo" alt="Linked Data Fragments" /></a>
1616
</figure>
1717
</header>
1818
<main>

test/controllers/AssetsController-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe('AssetsController', function () {
2929
});
3030

3131
it('should correctly serve SVG assets', function (done) {
32-
client.get('/assets/logo').expect(function (response) {
33-
var asset = fs.readFileSync(path.join(__dirname, '/../../assets/logo.svg'), 'utf8');
32+
client.get('/assets/images/logo').expect(function (response) {
33+
var asset = fs.readFileSync(path.join(__dirname, '/../../assets/images/logo.svg'), 'utf8');
3434
controller.next.should.not.have.been.called;
3535
response.should.have.property('statusCode', 200);
3636
response.headers.should.have.property('content-type', 'image/svg+xml');
@@ -40,8 +40,8 @@ describe('AssetsController', function () {
4040
});
4141

4242
it('should correctly serve CSS assets', function (done) {
43-
client.get('/assets/style').expect(function (response) {
44-
var asset = fs.readFileSync(path.join(__dirname, '/../../assets/style.css'), 'utf8');
43+
client.get('/assets/styles/ldf-server').expect(function (response) {
44+
var asset = fs.readFileSync(path.join(__dirname, '/../../assets/styles/ldf-server.css'), 'utf8');
4545
controller.next.should.not.have.been.called;
4646
response.should.have.property('statusCode', 200);
4747
response.headers.should.have.property('content-type', 'text/css;charset=utf-8');

0 commit comments

Comments
 (0)