增加files路由,修复生产环境下新增图片后需要重启jsite服务器才能显示图片的问题
This commit is contained in:
parent
f52cb5f6a8
commit
fb0041bb19
68
app/files/[...path]/route.js
Normal file
68
app/files/[...path]/route.js
Normal file
@ -0,0 +1,68 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
const BACKEND_SERVER_URL = process.env.BACKEND_SERVER_URL || '';
|
||||
const PUBLIC_FILES_DIR = path.join(process.cwd(), 'public/files');
|
||||
|
||||
function getContentType(filePath) {
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
switch (ext) {
|
||||
case '.jpg':
|
||||
case '.jpeg':
|
||||
return 'image/jpeg';
|
||||
case '.png':
|
||||
return 'image/png';
|
||||
case '.gif':
|
||||
return 'image/gif';
|
||||
case '.svg':
|
||||
return 'image/svg+xml';
|
||||
case '.webp':
|
||||
return 'image/webp';
|
||||
case '.mp4':
|
||||
return 'video/mp4';
|
||||
case '.pdf':
|
||||
return 'application/pdf';
|
||||
default:
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(req, { params }) {
|
||||
try {
|
||||
const parts = params.path || [];
|
||||
const fileName = parts.join('/');
|
||||
if (!fileName) return new NextResponse('Not Found', { status: 404 });
|
||||
|
||||
const localPath = path.join(PUBLIC_FILES_DIR, fileName);
|
||||
|
||||
if (fs.existsSync(localPath)) {
|
||||
const data = fs.readFileSync(localPath);
|
||||
return new NextResponse(data, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': getContentType(localPath) }
|
||||
});
|
||||
}
|
||||
|
||||
const remoteUrl = `${BACKEND_SERVER_URL}/files/${fileName}`;
|
||||
const res = await fetch(remoteUrl);
|
||||
if (!res.ok) {
|
||||
return new NextResponse('Not Found', { status: 404 });
|
||||
}
|
||||
const arrayBuffer = await res.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
// 确保目录存在并写入本地
|
||||
fs.mkdirSync(path.dirname(localPath), { recursive: true });
|
||||
fs.writeFileSync(localPath, buffer);
|
||||
|
||||
return new NextResponse(buffer, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': getContentType(localPath) }
|
||||
});
|
||||
} catch (e) {
|
||||
return new NextResponse('Server Error', { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,182 @@
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-crossbody-bag-for-trendy-women</loc>
|
||||
<loc>http://192.168.2.200:3001/products</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/womens-handbags/exquisite-original-fashion-womens-handbag-4</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/chic-leather-crossbody-bag-4</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/chic-leather-crossbody-bag-2</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/contact</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags/chic-leather-crossbody-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/chic-envelope-crossbody-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags/exquisite-original-fashion-womens-handbag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/the-latest-trends-in-original-fashionable-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/stylish-pursuits-unraveling-the-charm-of-our-original-fashion-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/explore-the-world-of-original-fashionable-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/chic-leather-crossbody-bag-3</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/tote-bags/elegant-leather-crossbody-bag-4</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/elegant-leather-crossbody-bag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags/chic-leather-crossbody-bag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/elegant-leather-crossbody-bag-5</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/elegant-leather-crossbody-bag-2</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/the-artistry-behind-our-original-fashionable-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/elegant-leather-crossbody-bag-3</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/exquisite-original-fashion-womens-handbag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/chic-leather-crossbody-bag-5</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/elegant-pink-leather-shoulder-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/exquisite-original-fashion-womens-handbag-2</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/the-allure-of-our-exclusive-original-fashion-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/about-us</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/the-enchanting-journey-of-our-original-fashionable-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags/elegant-leather-crossbody-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/discover-the-timeless-elegance-of-our-original-fashion-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/chic-pursuits-our-original-fashion-womens-handbags-collection</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/exquisite-original-fashion-womens-handbag-3</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/elevate-your-style-with-our-exquisite-original-fashion-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/the-secret-to-choosing-the-perfect-original-fashion-womens-handbag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/blog/accessorize-with-confidence-our-handpicked-original-fashion-womens-bags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
@ -17,47 +192,17 @@
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-leather-crossbody-bag-2</loc>
|
||||
<loc>http://192.168.2.200:3001/products/crossbody-bags/chic-leather-crossbody-bag-6</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-leather-crossbody-bag-2</loc>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/elegant-ladys-tote-bag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-ladys-tote-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/exquisite-womens-original-fashion-handbag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-leather-crossbody-bag-4</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/fashion-handbags/elegant-pink-leather-handbag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-leather-crossbody-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-leather-tote-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-pink-leather-crossbody-bag</loc>
|
||||
<loc>http://192.168.2.200:3001/blog/unleash-your-style-with-our-trendy-original-fashion-womens-handbags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
@ -67,37 +212,27 @@
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/exquisite-ladys-handbag</loc>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags/elegant-ladys-tote-bag</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/exquisite-fashionable-womens-handbag-1</loc>
|
||||
<loc>http://192.168.2.200:3001/blog/unveiling-the-allure-of-handcrafted-original-fashion-womens-bags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-leather-crossbody-bag-5</loc>
|
||||
<loc>http://192.168.2.200:3001/products/shoulder-bags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-leather-crossbody-bag-1</loc>
|
||||
<loc>http://192.168.2.200:3001/products/tote-bags/exquisite-fashionable-womens-handbag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/chic-leather-crossbody-bag-1</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>http://192.168.2.200:3001/products/elegant-leather-crossbody-bag-4</loc>
|
||||
<loc>http://192.168.2.200:3001/products/tote-bags</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
|
||||
@ -136,8 +136,8 @@ export async function processDataItem(item, downloadFiles) {
|
||||
}
|
||||
downloadTasks.push(...htmlTasks);
|
||||
|
||||
// 并发执行所有下载任务
|
||||
await Promise.all(downloadTasks);
|
||||
// 并发执行所有下载任务并获取结果
|
||||
const results = await Promise.all(downloadTasks);
|
||||
|
||||
// 更新主表字段
|
||||
if (downloadMap.has('image')) item.image = downloadMap.get('image');
|
||||
@ -148,7 +148,7 @@ export async function processDataItem(item, downloadFiles) {
|
||||
|
||||
// 更新attachments字段
|
||||
if (item.attachments && Array.isArray(item.attachments)) {
|
||||
const attachmentResults = downloadTasks.filter(task =>
|
||||
const attachmentResults = results.filter(task =>
|
||||
task && typeof task === 'object' && 'index' in task
|
||||
);
|
||||
for (const result of attachmentResults) {
|
||||
@ -160,7 +160,7 @@ export async function processDataItem(item, downloadFiles) {
|
||||
|
||||
// 更新items字段
|
||||
if (item.items && Array.isArray(item.items)) {
|
||||
const itemResults = downloadTasks.filter(task =>
|
||||
const itemResults = results.filter(task =>
|
||||
task && typeof task === 'object' && 'itemIndex' in task
|
||||
);
|
||||
for (const result of itemResults) {
|
||||
@ -173,7 +173,7 @@ export async function processDataItem(item, downloadFiles) {
|
||||
}
|
||||
|
||||
// 更新HTML内容
|
||||
const htmlResults = downloadTasks.filter(task =>
|
||||
const htmlResults = results.filter(task =>
|
||||
task && typeof task === 'object' && 'key' in task
|
||||
);
|
||||
for (const result of htmlResults) {
|
||||
@ -224,8 +224,9 @@ export async function getPageData({
|
||||
data = await processDataItem(data, downloadFiles);
|
||||
}
|
||||
|
||||
// 返回处理后的数据,确保前端能拿到本地化后的图片地址
|
||||
return {
|
||||
data: message.data,
|
||||
data,
|
||||
total: message.total,
|
||||
page_info: message.page_info,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user