|
9 | 9 | const mongoose = require('mongoose'); |
10 | 10 | const fs = require('fs'); |
11 | 11 | const path = require('path'); |
| 12 | +const { URL } = require('url'); |
12 | 13 | const User = require('../models/User'); |
13 | 14 | const Ad = require('../models/Ad'); |
14 | 15 | require('dotenv').config({ path: path.resolve(__dirname, '..', '.env') }); |
@@ -81,7 +82,37 @@ function generateCoordinates(provinceName) { |
81 | 82 | return { lat: parseFloat(lat.toFixed(6)), lng: parseFloat(lng.toFixed(6)) }; |
82 | 83 | } |
83 | 84 |
|
84 | | -// Get random local property images |
| 85 | +/** Same naming as frontend/download-images.cjs (property-01.jpg … property-30.jpg) */ |
| 86 | +function getDefaultRemotePropertyFilenames() { |
| 87 | + const names = []; |
| 88 | + for (let i = 1; i <= 30; i += 1) { |
| 89 | + names.push(`property-${String(i).padStart(2, '0')}.jpg`); |
| 90 | + } |
| 91 | + return names; |
| 92 | +} |
| 93 | + |
| 94 | +function parseRemoteFilenamesFromEnv() { |
| 95 | + const raw = process.env.SEED_PROPERTY_IMAGE_FILENAMES; |
| 96 | + if (!raw || !String(raw).trim()) return null; |
| 97 | + return String(raw) |
| 98 | + .split(',') |
| 99 | + .map((s) => s.trim()) |
| 100 | + .filter(Boolean); |
| 101 | +} |
| 102 | + |
| 103 | +/** Base URL for seeded property images on the server (no trailing slash), e.g. https://amlak.mmdcode.top/images/properties */ |
| 104 | +function getRemotePropertyImagesBaseUrl() { |
| 105 | + const u = process.env.SEED_PROPERTY_IMAGES_BASE_URL; |
| 106 | + if (!u || !String(u).trim()) return ''; |
| 107 | + return String(u).replace(/\/+$/, ''); |
| 108 | +} |
| 109 | + |
| 110 | +function joinRemotePropertyUrl(base, filename) { |
| 111 | + const baseWithSlash = base.endsWith('/') ? base : `${base}/`; |
| 112 | + return new URL(filename, baseWithSlash).href; |
| 113 | +} |
| 114 | + |
| 115 | +// Get random local property images, or full URLs from SEED_PROPERTY_IMAGES_BASE_URL when no local folder |
85 | 116 | function getRandomPropertyImages(count = 3) { |
86 | 117 | const candidateDirs = [ |
87 | 118 | // Preferred (this repo keeps demo images under frontend/public) |
@@ -115,11 +146,26 @@ function getRandomPropertyImages(count = 3) { |
115 | 146 | } |
116 | 147 | } |
117 | 148 |
|
118 | | - // If no local images, copy from Unsplash URLs (fallback) |
| 149 | + // Remote/CDN: same paths as static hosting (e.g. /images/properties on production domain) |
| 150 | + if (availableImages.length === 0) { |
| 151 | + const remoteBase = getRemotePropertyImagesBaseUrl(); |
| 152 | + if (remoteBase) { |
| 153 | + const filenames = parseRemoteFilenamesFromEnv() || getDefaultRemotePropertyFilenames(); |
| 154 | + const remoteUrls = filenames.map((name) => joinRemotePropertyUrl(remoteBase, name)); |
| 155 | + const shuffled = remoteUrls.sort(() => 0.5 - Math.random()); |
| 156 | + const picked = shuffled.slice(0, Math.min(count, shuffled.length)); |
| 157 | + console.log( |
| 158 | + ` 📎 Using ${picked.length} remote property image URL(s) from ${remoteBase}` |
| 159 | + ); |
| 160 | + return picked; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // If no local images and no remote base |
119 | 165 | if (availableImages.length === 0) { |
120 | 166 | console.log( |
121 | 167 | ' ⚠️ No local property images found in expected folders (frontend/public/images/properties / frontend/dist/images/properties / public/images/properties). ' + |
122 | | - 'Run: (cd frontend && npm run download:images)' |
| 168 | + 'Either run (cd frontend && npm run download:images), or set SEED_PROPERTY_IMAGES_BASE_URL to your hosted folder (e.g. https://yourdomain.com/images/properties).' |
123 | 169 | ); |
124 | 170 | return []; |
125 | 171 | } |
|
0 commit comments