Skip to content

Commit 61737e5

Browse files
author
nicola
committed
decoupling get handler
1 parent 2f1c74f commit 61737e5

2 files changed

Lines changed: 89 additions & 65 deletions

File tree

‎handlers/get.js‎

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -67,84 +67,52 @@ function get(req, res, includeBody) {
6767
header.addLink(res, aclLink, 'acl');
6868
header.addLink(res, metaLink, 'describedBy');
6969

70-
ldp.stat(filename, function(err, stats) {
71-
// File does not exist
70+
ldp.get(filename, uri, includeBody, function(err, data, container) {
71+
72+
// This should be implemented in LDP.prototype.get
73+
if (err && err.status === 404 && glob.hasMagic(filename)) {
74+
debug("GET/HEAD -- Glob request");
75+
return globHandler(req, res);
76+
}
77+
7278
if (err) {
73-
// Check in case it is a pattern, e.g. /*.js
74-
if (glob.hasMagic(filename)) {
75-
debug("GET/HEAD -- Glob request");
76-
return globHandler(req, res);
77-
}
7879
debug('GET/HEAD -- Read error: ' + err);
7980
return res
80-
.status(404)
81-
.send("Can't read file: " + err);
81+
.status(err.status)
82+
.send(err.message);
8283
}
8384

84-
// Just return resource exists
85-
if (!includeBody) {
85+
// Just return that file exists
86+
// data is of `typeof 'string'` if file is empty, but exists!
87+
if (data === undefined) {
8688
return res.sendStatus(200);
8789
}
8890

89-
// Found a container
90-
if (stats.isDirectory()) {
91-
return ldp.readContainerMeta(filename, function(err, data) {
92-
if (err) {
93-
debug('GET/HEAD -- Read error:' + err);
94-
return res
95-
.status(err.status)
96-
.send(err.message);
97-
}
98-
ldp.listContainer(filename, uri, data, function (err, data) {
99-
if (err) {
100-
debug('GET/HEAD -- Read error:' + err);
101-
return res
102-
.status(err.status)
103-
.send(err.message);
104-
}
105-
// TODO this should be added as a middleware in the routes
106-
res.locals.turtleData = data;
107-
return parseLinkedData(req, res);
108-
});
109-
});
110-
}
111-
else {
112-
return ldp.readFile(filename, function (err, data) {
113-
// Error when reading
114-
if (err) {
115-
debug('GET/HEAD -- Read error:' + err);
116-
return res
117-
.status(err.status)
118-
.send(err.message);
119-
}
120-
121-
// File retrieved
122-
debug('GET/HEAD -- Read Ok. Bytes read: ' + data.length);
123-
124-
var contentType = mime.lookup(filename);
125-
res.set('content-type', contentType);
126-
debug('GET/HEAD -- content-type: ' + contentType);
127-
128-
// Consider acl and meta files text/turtle
129-
if (path.extname(filename) === ldp.suffixAcl ||
130-
path.basename(filename) === turtleExtension ||
131-
path.basename(filename) === metaExtension) {
132-
contentType = 'text/turtle';
133-
}
134-
135-
// if data is text/turtle, parse it
136-
if (contentType === 'text/turtle') {
137-
// TODO this should be added as a middleware in the routes
138-
res.locals.turtleData = data;
139-
return parseLinkedData(req, res);
140-
}
91+
// Container/resource retrieved
92+
if (!container) {
93+
var contentType = mime.lookup(filename);
94+
res.set('content-type', contentType);
95+
debug('GET/HEAD -- content-type: ' + contentType);
96+
97+
// Consider acl and meta files text/turtle
98+
if (path.extname(filename) === ldp.suffixAcl ||
99+
path.basename(filename) === turtleExtension ||
100+
path.basename(filename) === metaExtension) {
101+
contentType = 'text/turtle';
102+
}
141103

142-
// Otherwise, just send the data
104+
// if data is not text/turtle, just send it
105+
if (contentType !== 'text/turtle') {
143106
return res
144107
.status(200)
145108
.send(data);
146-
});
109+
}
147110
}
111+
112+
// TODO this should be added as a middleware in the routes
113+
res.locals.turtleData = data;
114+
return parseLinkedData(req, res);
115+
148116
});
149117

150118
}

‎ldp.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,62 @@ LDP.prototype.writeFile = function (filePath, contents, cb) {
251251
});
252252
};
253253

254+
LDP.prototype.get = function(filename, uri, includeBody, callback) {
255+
var ldp = this;
256+
fs.stat(filename, function(err, stats) {
257+
// File does not exist
258+
if (err) {
259+
return callback({
260+
status: 404,
261+
message:"Can't read file: " + err
262+
});
263+
}
264+
265+
// Just return, since resource exists
266+
if (!includeBody) {
267+
return callback(null);
268+
}
269+
270+
// Found a container
271+
if (stats.isDirectory()) {
272+
return ldp.readContainerMeta(filename, function(err, data) {
273+
if (err) {
274+
debug.handlers('GET/HEAD -- Read error:' + err);
275+
return callback({
276+
status: err.status,
277+
message: err.message
278+
});
279+
}
280+
ldp.listContainer(filename, uri, data, function (err, data) {
281+
if (err) {
282+
debug.handlers('GET/HEAD -- Read error:' + err);
283+
return callback({
284+
status: err.status,
285+
message: err.message
286+
});
287+
}
288+
// The ending `true`, specifies it is a container
289+
return callback(null, data, true);
290+
});
291+
});
292+
}
293+
else {
294+
return ldp.readFile(filename, function (err, data) {
295+
// Error when reading
296+
if (err) {
297+
debug.handlers('GET/HEAD -- Read error:' + err);
298+
return callback({
299+
status: err.status,
300+
message: err.message
301+
});
302+
}
303+
debug.handlers('GET/HEAD -- Read Ok. Bytes read: ' + data.length);
304+
return callback(null, data);
305+
});
306+
}
307+
});
308+
};
309+
254310
LDP.prototype.delete = function(filename, callback) {
255311
var ldp = this;
256312
ldp.stat(filename, function(err, stats) {

0 commit comments

Comments
 (0)