mastercontroller 1.2.10 → 1.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MasterAction.js +68 -0
- package/MasterControl.js +4 -4
- package/examples/FileServingExample.js +88 -0
- package/package.json +2 -2
package/MasterAction.js
CHANGED
|
@@ -190,6 +190,74 @@ class MasterAction{
|
|
|
190
190
|
return false;
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
+
// Serves binary files with proper headers and MIME types
|
|
194
|
+
// options: {
|
|
195
|
+
// contentType: string (auto-detected if not provided),
|
|
196
|
+
// disposition: 'inline' | 'attachment' (default: 'attachment'),
|
|
197
|
+
// filename: string (defaults to original filename)
|
|
198
|
+
// }
|
|
199
|
+
returnFile(filePath, options){
|
|
200
|
+
options = options || {};
|
|
201
|
+
|
|
202
|
+
// Default options
|
|
203
|
+
var disposition = options.disposition || 'attachment';
|
|
204
|
+
var filename = options.filename || filePath.split('/').pop();
|
|
205
|
+
var contentType = options.contentType;
|
|
206
|
+
|
|
207
|
+
// Auto-detect content type if not provided
|
|
208
|
+
if (!contentType) {
|
|
209
|
+
var ext = filePath.split('.').pop().toLowerCase();
|
|
210
|
+
var mimeTypes = {
|
|
211
|
+
'pdf': 'application/pdf',
|
|
212
|
+
'jpg': 'image/jpeg',
|
|
213
|
+
'jpeg': 'image/jpeg',
|
|
214
|
+
'png': 'image/png',
|
|
215
|
+
'gif': 'image/gif',
|
|
216
|
+
'svg': 'image/svg+xml',
|
|
217
|
+
'zip': 'application/zip',
|
|
218
|
+
'csv': 'text/csv',
|
|
219
|
+
'txt': 'text/plain',
|
|
220
|
+
'xml': 'application/xml',
|
|
221
|
+
'json': 'application/json',
|
|
222
|
+
'mp4': 'video/mp4',
|
|
223
|
+
'mp3': 'audio/mpeg',
|
|
224
|
+
'wav': 'audio/wav',
|
|
225
|
+
'doc': 'application/msword',
|
|
226
|
+
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
227
|
+
'xls': 'application/vnd.ms-excel',
|
|
228
|
+
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
229
|
+
'ppt': 'application/vnd.ms-powerpoint',
|
|
230
|
+
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
|
231
|
+
};
|
|
232
|
+
contentType = mimeTypes[ext] || 'application/octet-stream';
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
// Read file as binary
|
|
237
|
+
var fileBuffer = fileserver.readFileSync(filePath);
|
|
238
|
+
|
|
239
|
+
// Build headers
|
|
240
|
+
var headers = {
|
|
241
|
+
'Content-Type': contentType,
|
|
242
|
+
'Content-Length': fileBuffer.length,
|
|
243
|
+
'Content-Disposition': disposition + '; filename="' + filename + '"'
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// Send response
|
|
247
|
+
if (!this.__response._headerSent) {
|
|
248
|
+
this.__response.writeHead(200, headers);
|
|
249
|
+
this.__response.end(fileBuffer);
|
|
250
|
+
}
|
|
251
|
+
} catch(error) {
|
|
252
|
+
// Handle file not found or read errors
|
|
253
|
+
if (!this.__response._headerSent) {
|
|
254
|
+
this.__response.writeHead(404, {'Content-Type': 'text/plain'});
|
|
255
|
+
this.__response.end('File not found: ' + filePath);
|
|
256
|
+
}
|
|
257
|
+
console.error('Error serving file:', error);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
193
261
|
|
|
194
262
|
}
|
|
195
263
|
|
package/MasterControl.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// MasterControl - by Alexander rich
|
|
2
|
-
// version 1.0.
|
|
2
|
+
// version 1.0.247
|
|
3
3
|
|
|
4
4
|
var url = require('url');
|
|
5
5
|
var fileserver = require('fs');
|
|
@@ -156,19 +156,19 @@ class MasterControl {
|
|
|
156
156
|
if(files && files.length > 0){
|
|
157
157
|
require(files[0]);
|
|
158
158
|
}else{
|
|
159
|
-
|
|
159
|
+
this.error.log(`Cannot find config file under ${rootFolderLocation}`, "error");
|
|
160
160
|
}
|
|
161
161
|
var routeFiles = globSearch.sync("**/*routes.js", { cwd: rootFolderLocation, absolute: true });
|
|
162
162
|
var route = routeFiles && routeFiles.length > 0 ? routeFiles[0] : null;
|
|
163
163
|
var routeObject = {
|
|
164
|
-
isComponent : true,
|
|
164
|
+
isComponent : true,
|
|
165
165
|
root : rootFolderLocation
|
|
166
166
|
}
|
|
167
167
|
this.router.setup(routeObject);
|
|
168
168
|
if(route){
|
|
169
169
|
require(route);
|
|
170
170
|
}else{
|
|
171
|
-
|
|
171
|
+
this.error.log(`Cannot find routes file under ${rootFolderLocation}`, "error");
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Example: Binary File Serving with MasterController
|
|
2
|
+
//
|
|
3
|
+
// This example demonstrates how to use the returnFile() method
|
|
4
|
+
// to serve binary files in your MasterController application.
|
|
5
|
+
|
|
6
|
+
class ExampleController extends MasterAction {
|
|
7
|
+
|
|
8
|
+
// Example 1: Serve a PDF file for download
|
|
9
|
+
downloadPdf(){
|
|
10
|
+
var filePath = master.root + '/storage/documents/report.pdf';
|
|
11
|
+
this.returnFile(filePath);
|
|
12
|
+
// Downloads as 'report.pdf' with auto-detected content-type
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Example 2: Display an image inline
|
|
16
|
+
showImage(){
|
|
17
|
+
var filePath = master.root + '/storage/images/photo.jpg';
|
|
18
|
+
this.returnFile(filePath, {
|
|
19
|
+
disposition: 'inline' // Display in browser instead of download
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Example 3: Serve file with custom filename
|
|
24
|
+
downloadReport(){
|
|
25
|
+
var filePath = master.root + '/storage/reports/monthly-2024-01.pdf';
|
|
26
|
+
this.returnFile(filePath, {
|
|
27
|
+
filename: 'January_Report.pdf' // Custom filename for download
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Example 4: Serve file with explicit content type
|
|
32
|
+
downloadCsv(){
|
|
33
|
+
var filePath = master.root + '/storage/exports/data.csv';
|
|
34
|
+
this.returnFile(filePath, {
|
|
35
|
+
contentType: 'text/csv',
|
|
36
|
+
filename: 'export.csv'
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Example 5: Serve dynamic file based on user request
|
|
41
|
+
downloadUserFile(){
|
|
42
|
+
var fileId = this.params.id;
|
|
43
|
+
var filePath = master.root + '/storage/user-files/' + fileId;
|
|
44
|
+
|
|
45
|
+
// You might want to add authentication/authorization checks here
|
|
46
|
+
// if (this.session.userId !== file.ownerId) { ... }
|
|
47
|
+
|
|
48
|
+
this.returnFile(filePath, {
|
|
49
|
+
filename: 'user-document.pdf'
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Example 6: Serve images inline (for img src tags)
|
|
54
|
+
serveUserAvatar(){
|
|
55
|
+
var userId = this.params.id;
|
|
56
|
+
var avatarPath = master.root + '/storage/avatars/' + userId + '.png';
|
|
57
|
+
this.returnFile(avatarPath, {
|
|
58
|
+
disposition: 'inline',
|
|
59
|
+
contentType: 'image/png'
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = ExampleController;
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// Supported MIME types (auto-detected by file extension):
|
|
69
|
+
//
|
|
70
|
+
// Images: jpg, jpeg, png, gif, svg
|
|
71
|
+
// Documents: pdf, doc, docx, xls, xlsx, ppt, pptx
|
|
72
|
+
// Data: csv, json, xml, txt
|
|
73
|
+
// Archives: zip
|
|
74
|
+
// Media: mp3, mp4, wav
|
|
75
|
+
//
|
|
76
|
+
// For other file types, use contentType option or files will be served as 'application/octet-stream'
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
// Route configuration example:
|
|
80
|
+
// In your routes.js:
|
|
81
|
+
//
|
|
82
|
+
// {
|
|
83
|
+
// "url" : "/download/pdf",
|
|
84
|
+
// "namespace" : "example",
|
|
85
|
+
// "controller" : "ExampleController",
|
|
86
|
+
// "action" : "downloadPdf",
|
|
87
|
+
// "type" : "GET"
|
|
88
|
+
// }
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"qs" : "^6.14.0",
|
|
4
4
|
"formidable": "^3.5.4",
|
|
5
5
|
"cookie": "^1.0.2",
|
|
6
|
-
"winston": "^3.
|
|
6
|
+
"winston": "^3.18.3",
|
|
7
7
|
"glob" :"^11.0.3"
|
|
8
8
|
},
|
|
9
9
|
"description": "A class library that makes using the Master Framework a breeze",
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
20
|
},
|
|
21
|
-
"version": "1.2.
|
|
21
|
+
"version": "1.2.12"
|
|
22
22
|
}
|