Skip to content
Merged
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
30 changes: 15 additions & 15 deletions lib/api/1.1/northbound/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
'use strict';

var di = require('di'),
express = require('express'),
router = express.Router();
express = require('express');

module.exports = filesRouterFactory;

di.annotate(filesRouterFactory, new di.Provide('Http.Api.Files'));
di.annotate(filesRouterFactory,
new di.Inject(
'Logger',
'Services.Configuration',
'fileService',
'Assert'
)
);

function filesRouterFactory (Logger, configuration, fileService, assert) {
function filesRouterFactory (
Logger,
fileService,
assert
) {
var logger = Logger.initialize(filesRouterFactory);

var router = express.Router();
/**
* @api {get} /api/1.1/files/:uuid GET /:uuid
* @apiVersion 1.1.0
Expand All @@ -40,7 +42,6 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {
uuid: req.params.uuid
});


return fileService.get(req.params.uuid)
.then(function(rdStream) {
rdStream.on('error', function(err){
Expand All @@ -53,7 +54,7 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {
})
.catch(function(err) {
logger.warning("Failure serving file request", { error: err });
if (err.name === '404') {
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
Expand Down Expand Up @@ -106,7 +107,7 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {
})
.catch(function(err) {
logger.error("Failure serving file request", { error: err });
if (err.name === '404') {
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
Expand Down Expand Up @@ -139,7 +140,6 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {
router.put('/files/:filename', function(req, res) {
logger.debug("Receiving file " + req.params.filename);


fileService.put(req, req.params.filename)
.then(function(streamObj) {

Expand Down Expand Up @@ -202,7 +202,7 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {
uuid: req.params.uuid,
error: error
});
if (error.name === '404') {
if (error.name === '404' || error.status === 404) {
res.status(404).json({
error: "File not found."
});
Expand Down Expand Up @@ -240,11 +240,11 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {

return fileService.verify(query)
.then(function(metadata) {
res.json(200, metadata);
res.status(200).json(metadata);
})
.catch(function(err) {
logger.error("Failure serving file request", { error: err });
if (err.name === '404') {
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
Expand Down Expand Up @@ -279,11 +279,11 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {

return fileService.verify(query)
.then(function(metadata) {
res.json(200, metadata[metadata.length - 1].md5);
res.status(200).json(metadata[metadata.length - 1].md5);
})
.catch(function(err) {
logger.warning("Failure serving file request", { error: err });
if (err.name === '404') {
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
Expand Down Expand Up @@ -317,7 +317,7 @@ function filesRouterFactory (Logger, configuration, fileService, assert) {

return fileService.list(req.body)
.then(function(files) {
res.json(200, files);
res.status(200).json(files);
})
.catch(function(err) {
logger.error("Failure serving file request", { error: err });
Expand Down
99 changes: 94 additions & 5 deletions lib/api/1.1/southbound/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
'use strict';

var di = require('di'),
express = require('express'),
router = express.Router();
express = require('express');

module.exports = filesRouterFactory;

Expand All @@ -23,7 +22,7 @@ function filesRouterFactory (
assert
) {
var logger = Logger.initialize(filesRouterFactory);

var router = express.Router();
/**
* @api {get} /api/1.1/files/:uuid GET /:uuid
* @apiVersion 1.1.0
Expand Down Expand Up @@ -56,7 +55,7 @@ function filesRouterFactory (
})
.catch(function(err) {
logger.warning("Failure serving file request", { error: err });
if (err.name === '404') {
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
Expand All @@ -70,6 +69,58 @@ function filesRouterFactory (

});

/**
* @api {get} /api/1.1/files/:filename/latest GET /:filename
* @apiVersion 1.1.0
* @apiDescription get the most recent version of a file by filename
* @apiName files-get
* @apiGroup files
* @apiError NotFound The file with the <code>uuid</code> was not found.
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "error": "File not found."
* }
*/

router.get('/files/:filename/latest', function(req, res) {
logger.debug("Received request for file by uuid", {
uuid: req.params.uuid
});

var query = req.params.filename;

return fileService.verify(query)
.then(function(metadata) {
return metadata[metadata.length - 1].uuid;
})
.then(function(uuid) {
return fileService.get(uuid);
})
.then(function(rdStream) {
rdStream.on('error', function(err){
logger.error("Failure serving file request", { error: err });
res.status(500).json({
error: "Failed to serve file request."
});
});
rdStream.pipe(res);
})
.catch(function(err) {
logger.error("Failure serving file request", { error: err });
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
} else {
res.status(500).json({
error: "Failed to serve file request."
});
}
});

});

/**
* @api {put} /api/1.1/files/:filename PUT /:filename
* @apiVersion 1.1.0
Expand Down Expand Up @@ -124,6 +175,44 @@ function filesRouterFactory (
});
});

return router;
/**
* @api {get} /api/1.1/files/md5/:filename/latest GET /md5/:filename/ltest
* @apiVersion 1.1.0
* @apiDescription get the md5sum for the most recent matching file from the
* global files root collection by filename
* @apiName files-global-get-md5-latest
* @apiGroup files
* @apiSuccess {json} files Most recent file with the <code>filename</code>.
* @apiError NotFound The file with the <code>filename</code> was not found.
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "error": "File not found."
* }
*/
router.get('/files/md5/:filename/latest', function(req, res) {
logger.debug("Received md5sum request for file" +
req.params.filename + " from the global files collection.");

var query = req.params.filename;

return fileService.verify(query)
.then(function(metadata) {
res.status(200).json(metadata[metadata.length - 1].md5);
})
.catch(function(err) {
logger.warning("Failure serving file request", { error: err });
if (err.name === '404' || err.status === 404) {
res.status(404).json({
error: 'File not found.'
});
} else {
res.status(500).json({
error: "Error retrieving metadata."
});
}
});
});

return router;
}
Loading