Mark-Lasfar commited on
Commit ·
d2f7fee
1
Parent(s): 8fddb3f
Fix: Optimize loadStore function, fix preview mode and follow-status
Browse files- .gitignore +2 -0
- server.js +111 -89
.gitignore
CHANGED
|
@@ -6,3 +6,5 @@ logs/
|
|
| 6 |
scripts/
|
| 7 |
routes/temp/
|
| 8 |
uploads/templates/
|
|
|
|
|
|
|
|
|
| 6 |
scripts/
|
| 7 |
routes/temp/
|
| 8 |
uploads/templates/
|
| 9 |
+
temp/
|
| 10 |
+
uploads/templates/
|
server.js
CHANGED
|
@@ -23582,7 +23582,7 @@ app.get('/api/templates/categories', async (req, res) => {
|
|
| 23582 |
});
|
| 23583 |
|
| 23584 |
// ============================================
|
| 23585 |
-
// GET /api/templates/:id - جلب تفاصيل قالب
|
| 23586 |
// ============================================
|
| 23587 |
app.get('/api/templates/:id', async (req, res) => {
|
| 23588 |
try {
|
|
@@ -23601,6 +23601,19 @@ app.get('/api/templates/:id', async (req, res) => {
|
|
| 23601 |
// زيادة عدد المشاهدات (غير متزامن)
|
| 23602 |
TemplateStore.incrementViews(template._id).catch(console.error);
|
| 23603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23604 |
// جلب قوالب مشابهة
|
| 23605 |
const similar = await TemplateStore.find({
|
| 23606 |
isActive: true,
|
|
@@ -23610,7 +23623,10 @@ app.get('/api/templates/:id', async (req, res) => {
|
|
| 23610 |
|
| 23611 |
res.json({
|
| 23612 |
success: true,
|
| 23613 |
-
template
|
|
|
|
|
|
|
|
|
|
| 23614 |
similar
|
| 23615 |
});
|
| 23616 |
} catch (error) {
|
|
@@ -23619,6 +23635,7 @@ app.get('/api/templates/:id', async (req, res) => {
|
|
| 23619 |
}
|
| 23620 |
});
|
| 23621 |
|
|
|
|
| 23622 |
// ============================================
|
| 23623 |
// POST /api/templates/:id/preview - معاينة قالب (بدون تثبيت)
|
| 23624 |
// ============================================
|
|
@@ -23959,7 +23976,7 @@ app.post('/api/templates/:id/apply', authenticateToken, async (req, res) => {
|
|
| 23959 |
|
| 23960 |
|
| 23961 |
// ============================================
|
| 23962 |
-
// GET /api/templates/serve/:slug
|
| 23963 |
// ============================================
|
| 23964 |
app.get('/api/templates/serve/:slug', async (req, res) => {
|
| 23965 |
try {
|
|
@@ -23968,40 +23985,58 @@ app.get('/api/templates/serve/:slug', async (req, res) => {
|
|
| 23968 |
|
| 23969 |
const template = await TemplateStore.findOne({ slug });
|
| 23970 |
|
| 23971 |
-
if (!template
|
| 23972 |
return res.status(404).send('Template not found');
|
| 23973 |
}
|
| 23974 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23975 |
if (isPreview) {
|
| 23976 |
-
//
|
| 23977 |
-
|
| 23978 |
-
let html = htmlResponse.data;
|
| 23979 |
-
|
| 23980 |
-
// إضافة شريط معاينة في أعلى الصفحة
|
| 23981 |
-
const previewBar = `
|
| 23982 |
-
<div style="position: fixed; top: 0; left: 0; right: 0; background: #8b5cf6; color: white; padding: 8px 16px; text-align: center; font-size: 14px; z-index: 10000; display: flex; justify-content: space-between; align-items: center;">
|
| 23983 |
-
<span><i class='bx bx-show-alt'></i> Preview Mode: ${template.name}</span>
|
| 23984 |
-
<div>
|
| 23985 |
-
<button onclick="window.parent.postMessage({ type: 'resize', height: document.body.scrollHeight }, '*')" style="background: rgba(255,255,255,0.2); border: none; color: white; padding: 4px 12px; border-radius: 20px; cursor: pointer; margin-right: 8px;">
|
| 23986 |
-
Update Size
|
| 23987 |
-
</button>
|
| 23988 |
-
<button onclick="window.parent.postMessage({ type: 'apply', templateId: '${template._id}' }, '*')" style="background: white; border: none; color: #8b5cf6; padding: 4px 16px; border-radius: 20px; cursor: pointer; font-weight: bold;">
|
| 23989 |
-
Apply Template
|
| 23990 |
-
</button>
|
| 23991 |
-
</div>
|
| 23992 |
-
</div>
|
| 23993 |
-
<div style="margin-top: 48px;">
|
| 23994 |
-
${html}
|
| 23995 |
-
</div>
|
| 23996 |
-
`;
|
| 23997 |
-
|
| 23998 |
-
// استبدال body بالمحتوى مع شريط المعاينة
|
| 23999 |
-
html = html.replace(/<body[^>]*>([\s\S]*?)<\/body>/i, `<body>${previewBar}</body>`);
|
| 24000 |
-
|
| 24001 |
-
res.send(html);
|
| 24002 |
} else {
|
| 24003 |
// إعادة توجيه عادية
|
| 24004 |
-
res.redirect(
|
| 24005 |
}
|
| 24006 |
|
| 24007 |
} catch (error) {
|
|
@@ -24010,15 +24045,15 @@ app.get('/api/templates/serve/:slug', async (req, res) => {
|
|
| 24010 |
}
|
| 24011 |
});
|
| 24012 |
|
|
|
|
| 24013 |
// ============================================
|
| 24014 |
-
// GET /api/templates/serve/:slug/* - خدمة ملفات القالب م
|
| 24015 |
// ============================================
|
| 24016 |
app.get('/api/templates/serve/:slug/*', async (req, res) => {
|
| 24017 |
try {
|
| 24018 |
const { slug } = req.params;
|
| 24019 |
const filePath = req.params[0] || 'index.html';
|
| 24020 |
|
| 24021 |
-
// جلب القالب من قاعدة البيانات
|
| 24022 |
const template = await TemplateStore.findOne({ slug });
|
| 24023 |
|
| 24024 |
if (!template) {
|
|
@@ -24026,79 +24061,66 @@ app.get('/api/templates/serve/:slug/*', async (req, res) => {
|
|
| 24026 |
}
|
| 24027 |
|
| 24028 |
// ============================================
|
| 24029 |
-
// 1. ال
|
| 24030 |
// ============================================
|
| 24031 |
-
|
| 24032 |
-
|
| 24033 |
-
|
| 24034 |
-
|
| 24035 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24036 |
|
| 24037 |
-
// البحث عن الملف
|
| 24038 |
-
const
|
| 24039 |
-
|
| 24040 |
-
|
| 24041 |
-
|
| 24042 |
-
|
| 24043 |
-
|
| 24044 |
-
|
| 24045 |
-
|
| 24046 |
-
|
| 24047 |
-
|
| 24048 |
-
|
| 24049 |
-
// البحث عن الملف المطلوب
|
| 24050 |
-
const foundFile = allFiles.find(f => {
|
| 24051 |
-
// تطابق تام للمسار
|
| 24052 |
-
if (f.name === filePath) return true;
|
| 24053 |
-
// تطابق نهاية المسار (للملفات في مجلدات فرعية)
|
| 24054 |
-
if (f.name.endsWith(filePath)) return true;
|
| 24055 |
-
// تطابق اسم الملف فقط
|
| 24056 |
-
if (f.name.split('/').pop() === filePath.split('/').pop()) return true;
|
| 24057 |
-
return false;
|
| 24058 |
-
});
|
| 24059 |
-
|
| 24060 |
-
if (foundFile && foundFile.url) {
|
| 24061 |
-
return res.redirect(foundFile.url);
|
| 24062 |
-
}
|
| 24063 |
}
|
| 24064 |
}
|
| 24065 |
|
| 24066 |
// ============================================
|
| 24067 |
-
// 2. Fallback: ال
|
| 24068 |
// ============================================
|
| 24069 |
-
let
|
| 24070 |
-
|
| 24071 |
-
|
| 24072 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24073 |
}
|
| 24074 |
|
| 24075 |
-
if (
|
| 24076 |
-
|
| 24077 |
-
const normalizedPath = path.normalize(fullPath);
|
| 24078 |
-
|
| 24079 |
-
if (normalizedPath.startsWith(templateBasePath) && fs.existsSync(normalizedPath)) {
|
| 24080 |
-
const ext = path.extname(filePath).toLowerCase();
|
| 24081 |
-
const contentTypes = {
|
| 24082 |
-
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
|
| 24083 |
-
'.json': 'application/json', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
|
| 24084 |
-
'.png': 'image/png', '.gif': 'image/gif', '.svg': 'image/svg+xml',
|
| 24085 |
-
'.ico': 'image/x-icon', '.webp': 'image/webp', '.woff': 'font/woff',
|
| 24086 |
-
'.woff2': 'font/woff2', '.ttf': 'font/ttf', '.eot': 'application/vnd.ms-fontobject'
|
| 24087 |
-
};
|
| 24088 |
-
|
| 24089 |
-
res.setHeader('Content-Type', contentTypes[ext] || 'application/octet-stream');
|
| 24090 |
-
return res.sendFile(normalizedPath);
|
| 24091 |
-
}
|
| 24092 |
}
|
| 24093 |
|
| 24094 |
// ============================================
|
| 24095 |
// 3. إذا لم يتم العثور على الملف
|
| 24096 |
// ============================================
|
| 24097 |
-
console.error(`File not found: ${filePath} for template ${slug}`);
|
| 24098 |
res.status(404).json({
|
| 24099 |
error: 'File not found',
|
| 24100 |
path: filePath,
|
| 24101 |
-
template: slug
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24102 |
});
|
| 24103 |
|
| 24104 |
} catch (error) {
|
|
|
|
| 23582 |
});
|
| 23583 |
|
| 23584 |
// ============================================
|
| 23585 |
+
// GET /api/templates/:id - جلب تفاصيل قالب (محدث)
|
| 23586 |
// ============================================
|
| 23587 |
app.get('/api/templates/:id', async (req, res) => {
|
| 23588 |
try {
|
|
|
|
| 23601 |
// زيادة عدد المشاهدات (غير متزامن)
|
| 23602 |
TemplateStore.incrementViews(template._id).catch(console.error);
|
| 23603 |
|
| 23604 |
+
// ✅ إضافة templateUrl للرد (للمعاينة)
|
| 23605 |
+
let templateUrl = null;
|
| 23606 |
+
if (template.themeData?.templateUrl) {
|
| 23607 |
+
templateUrl = template.themeData.templateUrl;
|
| 23608 |
+
} else if (template.themeData?.uploadedFiles?.htmlFiles?.length > 0) {
|
| 23609 |
+
const indexFile = template.themeData.uploadedFiles.htmlFiles.find(f =>
|
| 23610 |
+
f.name === 'index.html' || f.name.endsWith('/index.html')
|
| 23611 |
+
);
|
| 23612 |
+
if (indexFile && indexFile.url) {
|
| 23613 |
+
templateUrl = indexFile.url;
|
| 23614 |
+
}
|
| 23615 |
+
}
|
| 23616 |
+
|
| 23617 |
// جلب قوالب مشابهة
|
| 23618 |
const similar = await TemplateStore.find({
|
| 23619 |
isActive: true,
|
|
|
|
| 23623 |
|
| 23624 |
res.json({
|
| 23625 |
success: true,
|
| 23626 |
+
template: {
|
| 23627 |
+
...template.toObject(),
|
| 23628 |
+
templateUrl: templateUrl // ✅ أضف هذا
|
| 23629 |
+
},
|
| 23630 |
similar
|
| 23631 |
});
|
| 23632 |
} catch (error) {
|
|
|
|
| 23635 |
}
|
| 23636 |
});
|
| 23637 |
|
| 23638 |
+
|
| 23639 |
// ============================================
|
| 23640 |
// POST /api/templates/:id/preview - معاينة قالب (بدون تثبيت)
|
| 23641 |
// ============================================
|
|
|
|
| 23976 |
|
| 23977 |
|
| 23978 |
// ============================================
|
| 23979 |
+
// GET /api/templates/serve/:slug - خدمة معاينة القالب (محدث)
|
| 23980 |
// ============================================
|
| 23981 |
app.get('/api/templates/serve/:slug', async (req, res) => {
|
| 23982 |
try {
|
|
|
|
| 23985 |
|
| 23986 |
const template = await TemplateStore.findOne({ slug });
|
| 23987 |
|
| 23988 |
+
if (!template) {
|
| 23989 |
return res.status(404).send('Template not found');
|
| 23990 |
}
|
| 23991 |
|
| 23992 |
+
// ✅ البحث عن رابط القالب من مصادر متعددة
|
| 23993 |
+
let templateUrl = null;
|
| 23994 |
+
|
| 23995 |
+
// المصدر 1: themeData.templateUrl
|
| 23996 |
+
if (template.themeData?.templateUrl) {
|
| 23997 |
+
templateUrl = template.themeData.templateUrl;
|
| 23998 |
+
}
|
| 23999 |
+
|
| 24000 |
+
// المصدر 2: البحث عن index.html في uploadedFiles
|
| 24001 |
+
if (!templateUrl && template.themeData?.uploadedFiles?.htmlFiles) {
|
| 24002 |
+
const indexFile = template.themeData.uploadedFiles.htmlFiles.find(f =>
|
| 24003 |
+
f.name === 'index.html' || f.name.endsWith('/index.html')
|
| 24004 |
+
);
|
| 24005 |
+
if (indexFile && indexFile.url) {
|
| 24006 |
+
templateUrl = indexFile.url;
|
| 24007 |
+
}
|
| 24008 |
+
}
|
| 24009 |
+
|
| 24010 |
+
// المصدر 3: البحث في جميع الملفات (آخر حل)
|
| 24011 |
+
if (!templateUrl && template.themeData?.uploadedFiles) {
|
| 24012 |
+
const allFiles = [
|
| 24013 |
+
...(template.themeData.uploadedFiles.htmlFiles || []),
|
| 24014 |
+
...(template.themeData.uploadedFiles.cssFiles || []),
|
| 24015 |
+
...(template.themeData.uploadedFiles.jsFiles || [])
|
| 24016 |
+
];
|
| 24017 |
+
const htmlFile = allFiles.find(f =>
|
| 24018 |
+
f.name === 'index.html' ||
|
| 24019 |
+
f.name.endsWith('/index.html') ||
|
| 24020 |
+
f.name.match(/\.html$/)
|
| 24021 |
+
);
|
| 24022 |
+
if (htmlFile && htmlFile.url) {
|
| 24023 |
+
templateUrl = htmlFile.url;
|
| 24024 |
+
}
|
| 24025 |
+
}
|
| 24026 |
+
|
| 24027 |
+
if (!templateUrl) {
|
| 24028 |
+
console.error(`❌ No template URL found for ${slug}`);
|
| 24029 |
+
return res.status(404).send('Template preview not available - no URL found');
|
| 24030 |
+
}
|
| 24031 |
+
|
| 24032 |
+
console.log(`✅ Serving template ${slug} from: ${templateUrl.substring(0, 100)}...`);
|
| 24033 |
+
|
| 24034 |
if (isPreview) {
|
| 24035 |
+
// إعادة توجيه إلى رابط Cloudinary
|
| 24036 |
+
res.redirect(templateUrl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24037 |
} else {
|
| 24038 |
// إعادة توجيه عادية
|
| 24039 |
+
res.redirect(templateUrl);
|
| 24040 |
}
|
| 24041 |
|
| 24042 |
} catch (error) {
|
|
|
|
| 24045 |
}
|
| 24046 |
});
|
| 24047 |
|
| 24048 |
+
|
| 24049 |
// ============================================
|
| 24050 |
+
// GET /api/templates/serve/:slug/* - خدمة ملفات القالب (محدث)
|
| 24051 |
// ============================================
|
| 24052 |
app.get('/api/templates/serve/:slug/*', async (req, res) => {
|
| 24053 |
try {
|
| 24054 |
const { slug } = req.params;
|
| 24055 |
const filePath = req.params[0] || 'index.html';
|
| 24056 |
|
|
|
|
| 24057 |
const template = await TemplateStore.findOne({ slug });
|
| 24058 |
|
| 24059 |
if (!template) {
|
|
|
|
| 24061 |
}
|
| 24062 |
|
| 24063 |
// ============================================
|
| 24064 |
+
// 1. البحث عن الملف في Cloudinary
|
| 24065 |
// ============================================
|
| 24066 |
+
const uploadedFiles = template.themeData?.uploadedFiles;
|
| 24067 |
+
|
| 24068 |
+
if (uploadedFiles) {
|
| 24069 |
+
// جمع جميع الملفات
|
| 24070 |
+
const allFiles = [
|
| 24071 |
+
...(uploadedFiles.htmlFiles || []),
|
| 24072 |
+
...(uploadedFiles.cssFiles || []),
|
| 24073 |
+
...(uploadedFiles.jsFiles || []),
|
| 24074 |
+
...(uploadedFiles.imageFiles || []),
|
| 24075 |
+
...(uploadedFiles.fontFiles || [])
|
| 24076 |
+
];
|
| 24077 |
|
| 24078 |
+
// البحث عن الملف
|
| 24079 |
+
const foundFile = allFiles.find(f => {
|
| 24080 |
+
if (f.name === filePath) return true;
|
| 24081 |
+
if (f.name.endsWith(filePath)) return true;
|
| 24082 |
+
if (f.name.split('/').pop() === filePath.split('/').pop()) return true;
|
| 24083 |
+
return false;
|
| 24084 |
+
});
|
| 24085 |
+
|
| 24086 |
+
if (foundFile && foundFile.url) {
|
| 24087 |
+
console.log(`✅ Serving file ${filePath} from Cloudinary`);
|
| 24088 |
+
return res.redirect(foundFile.url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24089 |
}
|
| 24090 |
}
|
| 24091 |
|
| 24092 |
// ============================================
|
| 24093 |
+
// 2. Fallback: رابط القالب الرئيسي
|
| 24094 |
// ============================================
|
| 24095 |
+
let templateUrl = null;
|
| 24096 |
+
if (template.themeData?.templateUrl) {
|
| 24097 |
+
templateUrl = template.themeData.templateUrl;
|
| 24098 |
+
} else if (uploadedFiles?.htmlFiles?.length > 0) {
|
| 24099 |
+
const indexFile = uploadedFiles.htmlFiles.find(f =>
|
| 24100 |
+
f.name === 'index.html' || f.name.endsWith('/index.html')
|
| 24101 |
+
);
|
| 24102 |
+
if (indexFile && indexFile.url) {
|
| 24103 |
+
templateUrl = indexFile.url;
|
| 24104 |
+
}
|
| 24105 |
}
|
| 24106 |
|
| 24107 |
+
if (templateUrl && (filePath === 'index.html' || filePath === '')) {
|
| 24108 |
+
return res.redirect(templateUrl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24109 |
}
|
| 24110 |
|
| 24111 |
// ============================================
|
| 24112 |
// 3. إذا لم يتم العثور على الملف
|
| 24113 |
// ============================================
|
| 24114 |
+
console.error(`❌ File not found: ${filePath} for template ${slug}`);
|
| 24115 |
res.status(404).json({
|
| 24116 |
error: 'File not found',
|
| 24117 |
path: filePath,
|
| 24118 |
+
template: slug,
|
| 24119 |
+
availableFiles: {
|
| 24120 |
+
html: uploadedFiles?.htmlFiles?.map(f => f.name) || [],
|
| 24121 |
+
css: uploadedFiles?.cssFiles?.map(f => f.name) || [],
|
| 24122 |
+
js: uploadedFiles?.jsFiles?.map(f => f.name) || []
|
| 24123 |
+
}
|
| 24124 |
});
|
| 24125 |
|
| 24126 |
} catch (error) {
|