Repository navigation
Expand file tree
/
Copy pathurl.js
More file actions
56 lines (51 loc) · 1.78 KB
/
Copy pathurl.js
File metadata and controls
56 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var settings = require('../settings');
var Url = module.exports = {};
/**
* Returns a fully qualified url for a resource in the
* data directory.
*
* @param req
* @param path
* @param fileName (optional)
*/
Url.dataDirFileUrl = function (req, path, fileName) {
path = encodeURIComponent(path).replace(/%2F/g, '/'); // keep slashes
fileName = encodeURIComponent(fileName).replace(/%2F/g, '/'); // keep slashes
var url = base = req.protocol + '://' + req.headers.host + '/omk/data';
path[0] === '/' ? url += path : url += '/' + path;
if (typeof fileName === 'undefined' || fileName === null) return url;
path[path.length -1] === '/' ? url += fileName : url += '/' + fileName;
return url;
};
/**
* Returns a fully qualified base URL (protocol + host) for an API endpoint.
*
* @param req - http request that is pending
* @returns {string} - the base URL to the endpoint
*/
Url.baseUrl = function (req) {
var proto = req.headers['x-forwarded-proto'] ? req.headers['x-forwarded-proto'] : req.protocol;
var host = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host;
return proto + '://' + host;
}
/**
* Returns a fully qualified URL for an API endpoint.
*
* @param req - http request that is pending
* @param path - the API path from he base of the URL
* @returns {string} - the full URL to the endpoint
*/
Url.apiUrl = function (req, path) {
path = encodeURIComponent(path).replace(/%2F/g, '/'); // keep slashes
var base = Url.baseUrl(req);
return path[0] === '/' ? base + path : base + '/' + path;
};
/**
* Just makes sure a string ends with a /
*
* @param str - any string
* @returns {string} - string with / ending
*/
Url.endWithSlash = function (str) {
return str[str.length -1] === '/' ? str : str + '/';
};