@blocklet/pages-kit-block-studio 0.0.12 → 0.0.14
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/lib/cjs/middlewares/init-block-studio-router.js +81 -0
- package/lib/cjs/middlewares/init-uploader-router.js +2 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/helper.js +8 -1
- package/lib/esm/middlewares/init-block-studio-router.js +75 -0
- package/lib/esm/middlewares/init-uploader-router.js +2 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/helper.js +6 -0
- package/lib/types/middlewares/init-block-studio-router.d.ts +2 -0
- package/lib/types/middlewares/init-uploader-router.d.ts +0 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/helper.d.ts +2 -0
- package/package.json +13 -9
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.initBlockStudioRouter = void 0;
|
|
16
|
+
const express_1 = require("express");
|
|
17
|
+
const fs_1 = __importDefault(require("fs"));
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const helper_1 = require("../utils/helper");
|
|
20
|
+
exports.initBlockStudioRouter = (0, express_1.Router)();
|
|
21
|
+
const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.svg'];
|
|
22
|
+
exports.initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
if (!helper_1.isDev) {
|
|
24
|
+
return res.status(403).json({ error: 'Only available in development mode' });
|
|
25
|
+
}
|
|
26
|
+
const filePath = req.query.path;
|
|
27
|
+
if (!filePath) {
|
|
28
|
+
return res.status(400).json({ error: 'Path parameter is required' });
|
|
29
|
+
}
|
|
30
|
+
if (!(0, helper_1.isPathSafe)(filePath)) {
|
|
31
|
+
return res.status(403).json({ error: 'Invalid path' });
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
35
|
+
return res.json(null);
|
|
36
|
+
}
|
|
37
|
+
const ext = path_1.default.extname(filePath).toLowerCase();
|
|
38
|
+
if (BINARY_EXTENSIONS.includes(ext)) {
|
|
39
|
+
// For images, stream the file directly
|
|
40
|
+
const mimeType = `image/${ext.slice(1)}`;
|
|
41
|
+
res.setHeader('Content-Type', mimeType);
|
|
42
|
+
return fs_1.default.createReadStream(filePath).pipe(res);
|
|
43
|
+
}
|
|
44
|
+
// For text files, try to parse as JSON
|
|
45
|
+
const content = fs_1.default.readFileSync(filePath, 'utf-8');
|
|
46
|
+
try {
|
|
47
|
+
return res.json(JSON.parse(content));
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
// If JSON parsing fails, return as plain text
|
|
51
|
+
return res.json(content);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
return res.status(500).json({ error: 'Failed to read file' });
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
exports.initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
if (!helper_1.isDev) {
|
|
60
|
+
return res.status(403).json({ error: 'Only available in development mode' });
|
|
61
|
+
}
|
|
62
|
+
const { path: filePath, content } = req.body;
|
|
63
|
+
if (!filePath || content === undefined) {
|
|
64
|
+
return res.status(400).json({ error: 'Path and content are required' });
|
|
65
|
+
}
|
|
66
|
+
if (!(0, helper_1.isPathSafe)(filePath)) {
|
|
67
|
+
return res.status(403).json({ error: 'Invalid path' });
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const dir = path_1.default.dirname(filePath);
|
|
71
|
+
if (!fs_1.default.existsSync(dir)) {
|
|
72
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
73
|
+
}
|
|
74
|
+
fs_1.default.writeFileSync(filePath, JSON.stringify(content, null, 2));
|
|
75
|
+
return res.json({ success: true });
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return res.status(500).json({ error: 'Failed to write file' });
|
|
79
|
+
}
|
|
80
|
+
}));
|
|
81
|
+
exports.default = exports.initBlockStudioRouter;
|