<commit_before>'use strict';
// =========================================================================
//
// Routes for Documents
//
// Does not use the normal crud routes, mostly special sauce
//
// =========================================================================
var DocumentClass  = require ('../controllers/core.document.controller');
var routes = require ('../../../core/server/controllers/core.routes.controller');
var policy = require ('../../../core/server/controllers/core.policy.controller');

module.exports = function (app) {
	//
	// get put new delete
	//
	routes.setCRUDRoutes (app, 'document', DocumentClass, policy, ['get','put','new', 'delete'], {all:'guest',get:'guest'});
	// Import via CSV
	app.route ('/api/documents/import')
		.all (policy ('guest'))
		.post ( routes.setAndRun (DocumentClass, function (model, request) {
				return model.loadDocuments(request.files.file, request);
			}));
	app.route ('/api/document/makeLatest/:document')
		.all (policy ('user'))
		.put (routes.setAndRun (DocumentClass, function (model, req) {
			return model.makeLatest (req.Document);
		}));
	//
	// getAllDocuments                 : '/api/documents'
	//
	app.route ('/api/documents')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.list ();
		}));
	//
	// getProjectDocuments             : '/api/documents/' + projectId
	//
	app.route ('/api/documents/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentsForProject (req.params.projectid, req.headers.reviewdocsonly);
		}));
	//
	// getProjectDocumentTypes         : '/api/documents/types/' + projectId
	//
	app.route ('/api/documents/types/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentTypesForProject (req.params.projectid, req.headers.reviewdocsonly);
		}));
	//
	// getProjectDocumentSubTypes      : '/api/documents/subtypes/' + projectId
	//
	app.route ('/api/documents/subtypes/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentSubTypesForProject (req.params.projectid);
		}));
	//
	// getProjectDocumentFolderNames   : '/api/documents/folderNames/' + projectId
	//
	app.route ('/api/documents/folderNames/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentFolderNamesForProject (req.params.projectid);
		}));
	//
	// getProjectDocumentFolderNames (for MEM)   : '/api/documents/memtypes/' + projectId
	//
	app.route ('/api/documents/memtypes/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentTypesForProjectMEM (req.params.projectid);
		}));
	//
	// getProjectDocumentVersions      : '/api/documents/versions/' + projectId
	//
	app.route ('/api/documents/versions/:document')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentVersions (req.Document);
		}));
	//
	// getDocumentsInList              : '/api/documentlist', data:documentList
	//
	app.route ('/api/documentlist')
		.all (policy ('guest'))
		.put (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getList (req.body);
		}));
	//
	// fetch a document (download multipart stream)
	//
	app.route ('/api/document/:document/fetch')
		.all (policy ('guest'))
		.get (function (req, res) {
			if (req.Document.internalURL.match (/^(http|ftp)/)) {
				res.redirect (req.Document.internalURL);
			} else {
				// ETL fixing - if the name was brought in without a filename, and we have their document
				// file format, affix the type as an extension to the original name so they have a better
				// chance and opening up the file on double-click.
				var name = req.Document.internalOriginalName;
				if (req.Document.documentFileFormat && !req.Document.internalOriginalName.endsWith(req.Document.documentFileFormat)) {
					name = req.Document.internalOriginalName + "." + req.Document.documentFileFormat;
				}
				routes.streamFile (res, {
					file : req.Document.internalURL,
					name : name,
					mime : req.Document.internalMime
				});
			}
		});
	//
	// upload comment document:  We do this to force the model as opposed to trusting the
	// 'headers' from an untrustworthy client.
	//
	app.route ('/api/commentdocument/:project/upload')
	.all (policy ('guest'))
		.post (routes.setAndRun (DocumentClass, function (model, req) {
			return new Promise (function (resolve, reject) {
				var file = req.files.file;
				if (file) {
					var opts = { oldPath: file.path, projectCode: req.Project.code};
					routes.moveFile (opts)
					.then (function (newFilePath) {
						return model.create ({
							// Metadata related to this specific document that has been uploaded.
							// See the document.model.js for descriptions of the parameters to supply.
							project                 : req.Project,
							//projectID             : req.Project._id,<commit_msg>Adding logging for server.<commit_after>'use strict';
// =========================================================================
//
// Routes for Documents
//
// Does not use the normal crud routes, mostly special sauce
//
// =========================================================================
var DocumentClass  = require ('../controllers/core.document.controller');
var routes = require ('../../../core/server/controllers/core.routes.controller');
var policy = require ('../../../core/server/controllers/core.policy.controller');

module.exports = function (app) {
	//
	// get put new delete
	//
	routes.setCRUDRoutes (app, 'document', DocumentClass, policy, ['get','put','new', 'delete'], {all:'guest',get:'guest'});
	// Import via CSV
	app.route ('/api/documents/import')
		.all (policy ('guest'))
		.post ( routes.setAndRun (DocumentClass, function (model, request) {
				return model.loadDocuments(request.files.file, request);
			}));
	app.route ('/api/document/makeLatest/:document')
		.all (policy ('user'))
		.put (routes.setAndRun (DocumentClass, function (model, req) {
			return model.makeLatest (req.Document);
		}));
	//
	// getAllDocuments                 : '/api/documents'
	//
	app.route ('/api/documents')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.list ();
		}));
	//
	// getProjectDocuments             : '/api/documents/' + projectId
	//
	app.route ('/api/documents/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentsForProject (req.params.projectid, req.headers.reviewdocsonly);
		}));
	//
	// getProjectDocumentTypes         : '/api/documents/types/' + projectId
	//
	app.route ('/api/documents/types/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentTypesForProject (req.params.projectid, req.headers.reviewdocsonly);
		}));
	//
	// getProjectDocumentSubTypes      : '/api/documents/subtypes/' + projectId
	//
	app.route ('/api/documents/subtypes/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentSubTypesForProject (req.params.projectid);
		}));
	//
	// getProjectDocumentFolderNames   : '/api/documents/folderNames/' + projectId
	//
	app.route ('/api/documents/folderNames/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentFolderNamesForProject (req.params.projectid);
		}));
	//
	// getProjectDocumentFolderNames (for MEM)   : '/api/documents/memtypes/' + projectId
	//
	app.route ('/api/documents/memtypes/:projectid')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentTypesForProjectMEM (req.params.projectid);
		}));
	//
	// getProjectDocumentVersions      : '/api/documents/versions/' + projectId
	//
	app.route ('/api/documents/versions/:document')
		.all (policy ('guest'))
		.get (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getDocumentVersions (req.Document);
		}));
	//
	// getDocumentsInList              : '/api/documentlist', data:documentList
	//
	app.route ('/api/documentlist')
		.all (policy ('guest'))
		.put (routes.setAndRun (DocumentClass, function (model, req) {
			return model.getList (req.body);
		}));
	//
	// fetch a document (download multipart stream)
	//
	app.route ('/api/document/:document/fetch')
		.all (policy ('guest'))
		.get (function (req, res) {
			if (req.Document.internalURL.match (/^(http|ftp)/)) {
				res.redirect (req.Document.internalURL);
			} else {
				// ETL fixing - if the name was brought in without a filename, and we have their document
				// file format, affix the type as an extension to the original name so they have a better
				// chance and opening up the file on double-click.
				console.log("fetching:",req.Document.internalOriginalName,":", req.Document.documentFileFormat);
				var name = req.Document.internalOriginalName;
				if (req.Document.documentFileFormat && !req.Document.internalOriginalName.endsWith(req.Document.documentFileFormat)) {
					name = req.Document.internalOriginalName + "." + req.Document.documentFileFormat;
				}
				routes.streamFile (res, {
					file : req.Document.internalURL,
					name : name,
					mime : req.Document.internalMime
				});
			}
		});
	//
	// upload comment document:  We do this to force the model as opposed to trusting the
	// 'headers' from an untrustworthy client.
	//
	app.route ('/api/commentdocument/:project/upload')
	.all (policy ('guest'))
		.post (routes.setAndRun (DocumentClass, function (model, req) {
			return new Promise (function (resolve, reject) {
				var file = req.files.file;
				if (file) {
					var opts = { oldPath: file.path, projectCode: req.Project.code};
					routes.moveFile (opts)
					.then (function (newFilePath) {
						return model.create ({
							// Metadata related to this specific document that has been uploaded.
							// See the document.model.js for descriptions of the parameters to supply.
							project                 : req.Project,
							//projectID             : req.Project._id,
{'fraction_non_alphanumeric': 0.1800575263662512, 'fraction_numerical': 0.0, 'mean_word_length': 3.3938500421229993, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4260468', 'n_tokens_mistral': 3498, 'n_tokens_neox': 3155, 'n_words': 742}