-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
151 lines (134 loc) · 4.18 KB
/
file.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs');
const path = require('path');
const sep = path.sep;
const {
actionMetaSkip404,
actionMetaDoFnAndKeepConfigs,
MiddlewareDefault404,
} = require('./common');
// maps file extention to MIME typere
const mimeMap = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
function actionGetFileRelativePathFromMeta(meta) {
return meta.relativePath ? meta.relativePath : meta._url_path;
}
function MiddlewareDefaultFileError(request, response, meta) {
return function (e, pathname) {
console.log(e);
if (e && e.code && (e.code === 'ENOENT' || e.code === 'EISDIR')) {
MiddlewareDefault404(request, response, meta)(false);
} else {
response.statusCode = 500;
response.end(`Error getting the file: ${pathname}.`);
}
return e;
}
}
function defMiddlewareServeFileStatic(baseDir, doRawExceptionReturn) {
baseDir = baseDir ? baseDir : '.';
return function (request, response, meta) {
const pathname = actionGetFileRelativePathFromMeta(meta);
const ext = path.parse(pathname).ext;
const finalPath = `${process.cwd()}${sep}${baseDir}${sep}${pathname}`;
/*
var exist = fs.existsSync(finalPath);
if(!exist) {
actionMetaSkip404(meta, true);
return;
}
*/
actionMetaSkip404(meta);
let promise = new Promise(function(resolve, reject) {
// read file from file
var stream = fs.createReadStream(finalPath);
let hasHeaderWritten = false;
stream.on('data', (chunk) => {
if (!hasHeaderWritten) {
hasHeaderWritten = true;
response.writeHead(200, {
'Transfer-Encoding': 'chunked',
'Content-type': mimeMap[ext] || 'text/plain',
'X-Content-Type-Options': 'nosniff',
});
}
response.write(chunk);
});
stream.on('error', function(err){
reject(err);
});
stream.on('end', () => {
response.end();
resolve(true);
});
});
if (doRawExceptionReturn) {
return promise;
}
return promise.catch((e)=>{
MiddlewareDefaultFileError(request, response, meta)(e, pathname);
return e;
});
}
}
function MiddlewareResponseFolderFileList(request, response, meta) {
return (baseDir, pathname) => {
const finalPath = `${process.cwd()}${sep}${baseDir}${sep}${pathname}`;
fs.readdir(finalPath, (e, files) => {
if (e) {
MiddlewareDefaultFileError(request, response, meta)(e, pathname);
} else {
try {
response.setHeader('Content-Type', 'text/html; charset=utf-8');
} catch (e) {
console.log(e);
}
response.write(
`
<html lang="en">
<head><meta charset="utf-8"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"></head>
<body>
<ul class="list-group">
${files.map((file)=>`<li class="list-group-item"></li><a href="${meta._url_path}/${file}">${file}</a></li>`).join('')}
</ul>
</body>
</html>
`
);
response.end();
}
})
};
}
module.exports = {
mimeMap,
defMiddlewareServeFileStatic,
defMiddlewareServeFileStaticWithDirList: function (baseDir, doRawExceptionReturn) {
return function (request, response, meta) {
return defMiddlewareServeFileStatic(baseDir, true)(request, response, meta).catch((e) => {
const pathname = actionGetFileRelativePathFromMeta(meta);
// const ext = path.parse(pathname).ext;
const finalPath = `${process.cwd()}${sep}${baseDir}${sep}${pathname}`;
if (e && e.code === 'EISDIR') {
console.log(`${pathname} is a folder`);
MiddlewareResponseFolderFileList(response, response, meta)(baseDir, pathname);
return true;
} else {
MiddlewareDefaultFileError(request, response, meta)(e, pathname);
return e;
}
});
};
},
};