网页端:平台桥与部署修复(宝塔/Nginx)
- lib/platform:网页 Electron 统一 bridge/web/electron,子目录 API 基路径、返回首页跳站点根、路径末尾斜杠兼容 - index/design*.web.html:网页入口,web.js 加缓存参数避免旧脚本缓存 - api:PHP 读写 soon;文档说明目录权限与 Nginx - lib/design*、lib/index:与 platformBridge 对接及网页侧逻辑 - 公共资源 JsBarcode/jr-qrcode;文档与 package/README/.gitignore 等同步更新 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* SoonDesign 网页端文件保存接口
|
||||
* 接收 POST 请求,保存 .soon 文件到服务器
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
// 处理 OPTIONS 预检请求
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 只接受 POST 请求
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 获取保存目录(相对于网站根目录)
|
||||
// 如果 API 在 /api/ 目录,文件存储在 /design/files/
|
||||
$saveDir = dirname(__DIR__) . '/design/files/';
|
||||
// 如果上述路径不正确,请根据实际部署情况修改,例如:
|
||||
// $saveDir = '/www/wwwroot/soonWebsite/design/files/';
|
||||
|
||||
// 确保目录存在
|
||||
if (!is_dir($saveDir)) {
|
||||
mkdir($saveDir, 0755, true);
|
||||
}
|
||||
|
||||
// 获取文件名和内容
|
||||
$fileName = isset($_POST['fileName']) ? $_POST['fileName'] : '';
|
||||
$fileContent = isset($_POST['fileContent']) ? $_POST['fileContent'] : '';
|
||||
|
||||
// 验证文件名
|
||||
if (empty($fileName)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => '文件名不能为空']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 清理文件名,防止路径遍历攻击
|
||||
$fileName = basename($fileName);
|
||||
if (strpos($fileName, '..') !== false) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => '无效的文件名']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 确保文件名以 .soon 结尾
|
||||
if (substr($fileName, -5) !== '.soon') {
|
||||
$fileName = preg_replace('/\.soon$/i', '', $fileName) . '.soon';
|
||||
}
|
||||
|
||||
// 验证内容
|
||||
if (empty($fileContent)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => '文件内容不能为空']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
$filePath = $saveDir . $fileName;
|
||||
try {
|
||||
$result = file_put_contents($filePath, $fileContent);
|
||||
if ($result === false) {
|
||||
throw new Exception('文件写入失败');
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => '文件保存成功',
|
||||
'filePath' => '/design/files/' . $fileName,
|
||||
'fileName' => $fileName
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => '保存失败: ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user