feat(media): implement nextjs media proxy API routes (/api/uploads & /api/media) with server-side caching and streaming
This commit is contained in:
1 parent
d9c693c41b
commit
fa1a35c835
19 files changed
+13806
-13030
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import api from './api';
|
||||
import { PRODUCTS, Product, PetType } from "../data/products";
|
||||
import { Banner } from "../types";
|
||||
import { getMediaUrl } from "../media";
|
||||
|
||||
const CATEGORY_SLUG_TO_NAME: Record<string, string> = {
|
||||
'joints': 'مفاصل و استخوان',
|
||||
@@ -136,20 +137,12 @@ export class ProductService {
|
||||
feedingAdvice: data.dosageLogic || data.feedingAdvice || '',
|
||||
slug: data.slug,
|
||||
image: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.imageUrl) return local?.image || '/assets/images/hero-section-image.png';
|
||||
if (data.imageUrl.startsWith('http://') || data.imageUrl.startsWith('https://')) return data.imageUrl;
|
||||
if (data.imageUrl.startsWith('/uploads/')) return `${apiBase}${data.imageUrl}`;
|
||||
if (data.imageUrl.startsWith('/products/')) return local?.image || '/assets/images/hero-section-image.png';
|
||||
return local?.image || data.imageUrl || '/assets/images/hero-section-image.png';
|
||||
return getMediaUrl(data.imageUrl) || local?.image || '/assets/images/hero-section-image.png';
|
||||
})(),
|
||||
images: Array.isArray(data.images) && data.images.length > 0
|
||||
? data.images.map((img: string) => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (img.startsWith('http://') || img.startsWith('https://')) return img;
|
||||
if (img.startsWith('/uploads/')) return `${apiBase}${img}`;
|
||||
return img;
|
||||
})
|
||||
? data.images.map((img: string) => getMediaUrl(img)).filter(Boolean)
|
||||
: (local?.images || []),
|
||||
|
||||
main_ingredients: data.ingredientList?.map(i => i.ingredient) || data.ingredients?.split(/[،,-]/).map(s => s.trim()).filter(Boolean) || [],
|
||||
@@ -160,56 +153,20 @@ export class ProductService {
|
||||
specialist: safeParse<Product['specialist']>(data.specialist, defaultSpecialist),
|
||||
|
||||
calculateDosage: local?.calculateDosage,
|
||||
podcastUrl: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.podcastUrl) return undefined;
|
||||
if (data.podcastUrl.startsWith('http://') || data.podcastUrl.startsWith('https://')) return data.podcastUrl;
|
||||
if (data.podcastUrl.startsWith('/uploads/')) return `${apiBase}${data.podcastUrl}`;
|
||||
return data.podcastUrl;
|
||||
})(),
|
||||
podcastUrl: data.podcastUrl ? getMediaUrl(data.podcastUrl) : undefined,
|
||||
podcastTitle: data.podcastTitle || undefined,
|
||||
podcastDescription: data.podcastDescription || undefined,
|
||||
podcastCover: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.podcastCover) return undefined;
|
||||
if (data.podcastCover.startsWith('http://') || data.podcastCover.startsWith('https://')) return data.podcastCover;
|
||||
if (data.podcastCover.startsWith('/uploads/')) return `${apiBase}${data.podcastCover}`;
|
||||
return data.podcastCover;
|
||||
})(),
|
||||
podcastCover: data.podcastCover ? getMediaUrl(data.podcastCover) : undefined,
|
||||
|
||||
videoUrl: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.videoUrl) return undefined;
|
||||
if (data.videoUrl.startsWith('http://') || data.videoUrl.startsWith('https://')) return data.videoUrl;
|
||||
if (data.videoUrl.startsWith('/uploads/')) return `${apiBase}${data.videoUrl}`;
|
||||
return data.videoUrl;
|
||||
})(),
|
||||
videoUrl: data.videoUrl ? getMediaUrl(data.videoUrl) : undefined,
|
||||
videoTitle: data.videoTitle || undefined,
|
||||
videoDescription: data.videoDescription || undefined,
|
||||
videoCover: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.videoCover) return undefined;
|
||||
if (data.videoCover.startsWith('http://') || data.videoCover.startsWith('https://')) return data.videoCover;
|
||||
if (data.videoCover.startsWith('/uploads/')) return `${apiBase}${data.videoCover}`;
|
||||
return data.videoCover;
|
||||
})(),
|
||||
videoCover: data.videoCover ? getMediaUrl(data.videoCover) : undefined,
|
||||
|
||||
pdfUrl: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.pdfUrl) return undefined;
|
||||
if (data.pdfUrl.startsWith('http://') || data.pdfUrl.startsWith('https://')) return data.pdfUrl;
|
||||
if (data.pdfUrl.startsWith('/uploads/')) return `${apiBase}${data.pdfUrl}`;
|
||||
return data.pdfUrl;
|
||||
})(),
|
||||
pdfUrl: data.pdfUrl ? getMediaUrl(data.pdfUrl) : undefined,
|
||||
pdfTitle: data.pdfTitle || undefined,
|
||||
pdfDescription: data.pdfDescription || undefined,
|
||||
pdfCover: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
if (!data.pdfCover) return undefined;
|
||||
if (data.pdfCover.startsWith('http://') || data.pdfCover.startsWith('https://')) return data.pdfCover;
|
||||
if (data.pdfCover.startsWith('/uploads/')) return `${apiBase}${data.pdfCover}`;
|
||||
return data.pdfCover;
|
||||
})(),
|
||||
pdfCover: data.pdfCover ? getMediaUrl(data.pdfCover) : undefined,
|
||||
barcode: (inputData.barcode as string) || undefined,
|
||||
metaTitle: (inputData.metaTitle as string) || undefined,
|
||||
metaDescription: (inputData.metaDescription as string) || undefined,
|
||||
@@ -218,14 +175,7 @@ export class ProductService {
|
||||
stockStatus: (inputData.stockStatus as 'IN_STOCK' | 'OUT_OF_STOCK' | 'PRE_ORDER' | 'DISCONTINUED') || 'IN_STOCK',
|
||||
noIndex: Boolean(inputData.noIndex),
|
||||
noFollow: Boolean(inputData.noFollow),
|
||||
ogImage: (() => {
|
||||
const apiBase = (process.env.NEXT_PUBLIC_API_URL || '').replace(/\/api$/, '');
|
||||
const og = inputData.ogImage as string;
|
||||
if (!og) return undefined;
|
||||
if (og.startsWith('http://') || og.startsWith('https://')) return og;
|
||||
if (og.startsWith('/uploads/')) return `${apiBase}${og}`;
|
||||
return og;
|
||||
})(),
|
||||
ogImage: inputData.ogImage ? getMediaUrl(inputData.ogImage as string) : undefined,
|
||||
featuredImageAlt: (inputData.featuredImageAlt as string) || undefined,
|
||||
updatedAt: (inputData.updatedAt as string) || (inputData.createdAt as string) || undefined,
|
||||
reviews: Array.isArray(inputData.reviews) ? inputData.reviews as Product['reviews'] : undefined,
|
||||
@@ -372,7 +322,11 @@ export class ProductService {
|
||||
public async getBanners(): Promise<Banner[]> {
|
||||
try {
|
||||
const response = await api.get('/banners');
|
||||
return response.data || [];
|
||||
const data = response.data || [];
|
||||
return data.map((b: Banner) => ({
|
||||
...b,
|
||||
imageUrl: getMediaUrl(b.imageUrl),
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("[ProductService] Failed to fetch banners:", error);
|
||||
return [];
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import api from './api';
|
||||
import { getMediaUrl } from '../media';
|
||||
|
||||
export interface Video {
|
||||
id: string;
|
||||
@@ -12,16 +13,31 @@ export interface Video {
|
||||
isFeatured?: boolean;
|
||||
}
|
||||
|
||||
function normalizeVideo(v: any): Video {
|
||||
return {
|
||||
id: String(v.id || ''),
|
||||
title: String(v.title || ''),
|
||||
doctor: String(v.doctor || 'کادر علمی کنینا'),
|
||||
duration: String(v.duration || '۳:۰۰'),
|
||||
thumbnail: getMediaUrl(v.thumbnail || v.coverUrl || '/assets/images/hero-section-image.png'),
|
||||
videoUrl: getMediaUrl(v.videoUrl || ''),
|
||||
description: String(v.description || ''),
|
||||
viewsCount: Number(v.viewsCount) || 0,
|
||||
isFeatured: Boolean(v.isFeatured),
|
||||
};
|
||||
}
|
||||
|
||||
export const videoService = {
|
||||
getVideos: async (params?: { featured?: boolean; limit?: number; search?: string }) => {
|
||||
getVideos: async (params?: { featured?: boolean; limit?: number; search?: string }): Promise<Video[]> => {
|
||||
try {
|
||||
const res = await api.get('/videos', { params });
|
||||
const raw = res.data;
|
||||
if (Array.isArray(raw?.videos)) return raw.videos;
|
||||
if (Array.isArray(raw?.data?.videos)) return raw.data.videos;
|
||||
if (Array.isArray(raw?.data)) return raw.data;
|
||||
if (Array.isArray(raw)) return raw;
|
||||
return [];
|
||||
let items: any[] = [];
|
||||
if (Array.isArray(raw?.videos)) items = raw.videos;
|
||||
else if (Array.isArray(raw?.data?.videos)) items = raw.data.videos;
|
||||
else if (Array.isArray(raw?.data)) items = raw.data;
|
||||
else if (Array.isArray(raw)) items = raw;
|
||||
return items.map(normalizeVideo);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch videos from API:', error);
|
||||
return [];
|
||||
|
||||
Reference in new issue
Block a user