Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save TrevorBasinger/f3f0a9cc87610f7c7345 to your computer and use it in GitHub Desktop.

Select an option

Save TrevorBasinger/f3f0a9cc87610f7c7345 to your computer and use it in GitHub Desktop.
var Future = require ('data.future'),
exec = require ('child_process').exec,
fs = require ('fs');
module.exports = function (config, app) {
var pdf = require ('../../../lib/pdfserver'),
db = require ('../../../lib/db')(config),
// insertFile :: Connection -> FilePath -> Hash -> Future FileId
insertFile = curry (function (db, filePath, hash) {
return pdf.insertFile (db, filePath, hash).map (get ('returnVal'));
}),
// getAllFiles = Future [File]
getAllFiles = function (req) { return pdf.getAllFiles (db); },
// getFile :: Req -> Future File
getFile = compose (pdf.getFileById (db), get ('id'), get ('params')),
// checkFormatAndHash :: String -> Future Hash
checkFormatAndHash = compose (chain (pdf.hashFile), pdf.checkFormat),
// maybeInsertAndReturnFid :: FilePath -> Hash -> Future FileId
maybeCompileAndReturnFid = curry (function (filePath, hash) {
return pdf.getFidFromHash (db, hash)
.chain (maybe (konst (insertFile (db, filePath, hash))), Future.of);
}),
// submitAndGetFile :: Req -> Future FileId
submitAndGetFile = function (req) {
var filePath = getFilePath (req),
force = req.param.compile;
return checkFormatAndHash (filePath)
.chain (maybeCompileAndReturnFid (filePath))
.chain (pdf.getFileById (db))
.map (last);
},
// pullFile :: Req -> Future Maybe FilePath
pullFile = function (req) {
return pdf.getFileById (db, req.params.id)
.map (compose (toMaybe, last))
.map (map (get ('path')));
},
// pullOriginal :: Req -> Future Maybe FilePath
pullOriginal = function (req) {
return pdf.getFileById (db, req.params.id)
.map (compose (toMaybe, last))
.map (map (get ('original')));
},
nil = null;
//--------------------------------------------------------------------------------
//-- Impure
//--------------------------------------------------------------------------------
app.get ('/api/v1/files', futureResult (getAllFiles ));
app.get ('/api/v1/files/:id', futureResult (getFile ));
app.get ('/api/v1/files/:id/pull', fileResult (pullFile ));
app.get ('/api/v1/files/:id/original', fileResult (pullOriginal ));
app.post ('/api/v1/files', compileResult (submitAndGetFile));
return app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment