/** نرمال‌سازی مسیر فایل‌های storage برای URL عمومی */ function normalizeStoragePath(input) { if (!input || typeof input !== 'string') return input let s = input.replace(/\\/g, '/') // مسیر مطلق سرور → نسبی از /storage const storageIdx = s.indexOf('/storage/') if (storageIdx >= 0) { s = s.slice(storageIdx + '/storage'.length) } // thumbnails قدیمی: .../posts/thumbnails/id.jpg یا .../thumbnails/id.jpg const thumbMatch = s.match(/\/posts\/thumbnails\/([^/?#]+)$/i) || s.match(/\/thumbnails\/([^/?#]+)$/i) if (thumbMatch) { return `/posts/thumbnails/${thumbMatch[1]}` } if (!s.startsWith('/') && !s.startsWith('http')) { s = `/${s}` } return s } function normalizePostMediaFields(post) { if (!post || typeof post !== 'object') return post const next = { ...post } if (Array.isArray(next.post_images)) { next.post_images = next.post_images.map(normalizeStoragePath).filter(Boolean) } if (next.post_image) { next.post_image = normalizeStoragePath(next.post_image) } if (next.post_video) { next.post_video = normalizeStoragePath(next.post_video) } if (Array.isArray(next.files)) { next.files = next.files.map((f) => f && f.path ? { ...f, path: normalizeStoragePath(f.path) } : f ) } return next } module.exports = { normalizeStoragePath, normalizePostMediaFields, }