@seip/blue-bird 0.2.0 → 0.2.2
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/.env_example +11 -11
- package/LICENSE +21 -21
- package/README.md +80 -80
- package/backend/index.js +12 -12
- package/backend/routes/app.js +52 -40
- package/core/app.js +182 -182
- package/core/auth.js +69 -69
- package/core/cli/component.js +42 -42
- package/core/cli/init.js +117 -116
- package/core/cli/react.js +408 -393
- package/core/cli/route.js +42 -42
- package/core/config.js +41 -41
- package/core/logger.js +80 -80
- package/core/middleware.js +27 -27
- package/core/router.js +134 -134
- package/core/template.js +283 -220
- package/core/upload.js +76 -76
- package/core/validate.js +291 -291
- package/frontend/index.html +20 -0
- package/package.json +40 -43
package/core/upload.js
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import multer from "multer";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import Config from "./config.js";
|
|
5
|
-
|
|
6
|
-
const __dirname = Config.dirname();
|
|
7
|
-
const props = Config.props();
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Upload helper to manage file uploads using multer.
|
|
11
|
-
*/
|
|
12
|
-
class Upload {
|
|
13
|
-
/**
|
|
14
|
-
* Configures storage for uploaded files.
|
|
15
|
-
* @param {string} folder - The destination folder within the static path.
|
|
16
|
-
* @returns {import('multer').StorageEngine}
|
|
17
|
-
* @example
|
|
18
|
-
* const storage = Upload.storage("uploads");
|
|
19
|
-
*/
|
|
20
|
-
static storage(folder = "uploads") {
|
|
21
|
-
const dest = path.join(__dirname, props.static.path, folder);
|
|
22
|
-
|
|
23
|
-
if (!fs.existsSync(dest)) {
|
|
24
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return multer.diskStorage({
|
|
28
|
-
destination: (req, file, cb) => {
|
|
29
|
-
cb(null, dest);
|
|
30
|
-
},
|
|
31
|
-
filename: (req, file, cb) => {
|
|
32
|
-
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
|
|
33
|
-
cb(null, uniqueSuffix + path.extname(file.originalname));
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns a multer instance for single or multiple file uploads.
|
|
40
|
-
* @param {Object} options - Multer options.
|
|
41
|
-
* @param {string} [options.folder='uploads'] - Destination folder.
|
|
42
|
-
* @param {number} [options.fileSize=5000000] - Max file size in bytes (default 5MB).
|
|
43
|
-
* @param {Array<string>} [options.allowedTypes=[]] - Allowed mime types (e.g. ['image/png', 'image/jpeg']).
|
|
44
|
-
* @returns {import('multer').Multer}
|
|
45
|
-
* @example
|
|
46
|
-
* const upload = Upload.disk({ folder: "uploads", fileSize: 5000000, allowedTypes: ["image/png", "image/jpeg"] });
|
|
47
|
-
*/
|
|
48
|
-
static disk(options = {}) {
|
|
49
|
-
const { folder = "uploads", fileSize = 5000000, allowedTypes = [] } = options;
|
|
50
|
-
|
|
51
|
-
return multer({
|
|
52
|
-
storage: this.storage(folder),
|
|
53
|
-
limits: { fileSize },
|
|
54
|
-
fileFilter: (req, file, cb) => {
|
|
55
|
-
if (allowedTypes.length > 0 && !allowedTypes.includes(file.mimetype)) {
|
|
56
|
-
return cb(new Error("File type not allowed"), false);
|
|
57
|
-
}
|
|
58
|
-
cb(null, true);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Helper to get the public URL of an uploaded file.
|
|
65
|
-
* @param {string} filename - The name of the file.
|
|
66
|
-
* @param {string} [folder='uploads'] - The folder where the file is stored.
|
|
67
|
-
* @returns {string} The full public URL.
|
|
68
|
-
* @example
|
|
69
|
-
* const url = Upload.url("file.jpg", "uploads");
|
|
70
|
-
*/
|
|
71
|
-
static url(filename, folder = "uploads") {
|
|
72
|
-
return `${props.host}:${props.port}/public/${folder}/${filename}`;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export default Upload;
|
|
1
|
+
import multer from "multer";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import Config from "./config.js";
|
|
5
|
+
|
|
6
|
+
const __dirname = Config.dirname();
|
|
7
|
+
const props = Config.props();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Upload helper to manage file uploads using multer.
|
|
11
|
+
*/
|
|
12
|
+
class Upload {
|
|
13
|
+
/**
|
|
14
|
+
* Configures storage for uploaded files.
|
|
15
|
+
* @param {string} folder - The destination folder within the static path.
|
|
16
|
+
* @returns {import('multer').StorageEngine}
|
|
17
|
+
* @example
|
|
18
|
+
* const storage = Upload.storage("uploads");
|
|
19
|
+
*/
|
|
20
|
+
static storage(folder = "uploads") {
|
|
21
|
+
const dest = path.join(__dirname, props.static.path, folder);
|
|
22
|
+
|
|
23
|
+
if (!fs.existsSync(dest)) {
|
|
24
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return multer.diskStorage({
|
|
28
|
+
destination: (req, file, cb) => {
|
|
29
|
+
cb(null, dest);
|
|
30
|
+
},
|
|
31
|
+
filename: (req, file, cb) => {
|
|
32
|
+
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
|
|
33
|
+
cb(null, uniqueSuffix + path.extname(file.originalname));
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns a multer instance for single or multiple file uploads.
|
|
40
|
+
* @param {Object} options - Multer options.
|
|
41
|
+
* @param {string} [options.folder='uploads'] - Destination folder.
|
|
42
|
+
* @param {number} [options.fileSize=5000000] - Max file size in bytes (default 5MB).
|
|
43
|
+
* @param {Array<string>} [options.allowedTypes=[]] - Allowed mime types (e.g. ['image/png', 'image/jpeg']).
|
|
44
|
+
* @returns {import('multer').Multer}
|
|
45
|
+
* @example
|
|
46
|
+
* const upload = Upload.disk({ folder: "uploads", fileSize: 5000000, allowedTypes: ["image/png", "image/jpeg"] });
|
|
47
|
+
*/
|
|
48
|
+
static disk(options = {}) {
|
|
49
|
+
const { folder = "uploads", fileSize = 5000000, allowedTypes = [] } = options;
|
|
50
|
+
|
|
51
|
+
return multer({
|
|
52
|
+
storage: this.storage(folder),
|
|
53
|
+
limits: { fileSize },
|
|
54
|
+
fileFilter: (req, file, cb) => {
|
|
55
|
+
if (allowedTypes.length > 0 && !allowedTypes.includes(file.mimetype)) {
|
|
56
|
+
return cb(new Error("File type not allowed"), false);
|
|
57
|
+
}
|
|
58
|
+
cb(null, true);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Helper to get the public URL of an uploaded file.
|
|
65
|
+
* @param {string} filename - The name of the file.
|
|
66
|
+
* @param {string} [folder='uploads'] - The folder where the file is stored.
|
|
67
|
+
* @returns {string} The full public URL.
|
|
68
|
+
* @example
|
|
69
|
+
* const url = Upload.url("file.jpg", "uploads");
|
|
70
|
+
*/
|
|
71
|
+
static url(filename, folder = "uploads") {
|
|
72
|
+
return `${props.host}:${props.port}/public/${folder}/${filename}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default Upload;
|