fix
This commit is contained in:
parent
2fe019ec66
commit
78b73808e5
|
|
@ -40,24 +40,24 @@ const productSchema = yup.object({
|
||||||
});
|
});
|
||||||
|
|
||||||
const toPublicUrl = (img: any): ProductImage => {
|
const toPublicUrl = (img: any): ProductImage => {
|
||||||
const rawUrl: string = img?.url || img?.file_url || img?.fileUrl || '';
|
const rawUrl: string = img?.url || img?.file_url || img?.fileUrl || '';
|
||||||
const serveKey: string | undefined = img?.serve_key || img?.serveKey;
|
const serveKey: string | undefined = img?.serve_key || img?.serveKey;
|
||||||
const url = serveKey
|
const url = serveKey
|
||||||
? `${API_GATE_WAY}/${API_ROUTES.DOWNLOAD_FILE(serveKey)}`
|
? `${API_GATE_WAY}/${API_ROUTES.DOWNLOAD_FILE(serveKey)}`
|
||||||
: rawUrl?.startsWith('http')
|
: rawUrl?.startsWith('http')
|
||||||
? rawUrl
|
? rawUrl
|
||||||
: rawUrl
|
: rawUrl
|
||||||
? `${API_GATE_WAY}${rawUrl.startsWith('/') ? '' : '/'}${rawUrl}`
|
? `${API_GATE_WAY}${rawUrl.startsWith('/') ? '' : '/'}${rawUrl}`
|
||||||
: '';
|
: '';
|
||||||
return {
|
return {
|
||||||
id: (img?.id ?? img?.file_id ?? img?.FileID ?? img).toString(),
|
id: (img?.id ?? img?.file_id ?? img?.FileID ?? img).toString(),
|
||||||
url,
|
url,
|
||||||
alt: img?.alt || img?.original_name || '',
|
alt: img?.alt || img?.original_name || '',
|
||||||
order: img?.order ?? 0,
|
order: img?.order ?? 0,
|
||||||
type: img?.mime_type || img?.type || img?.file_type,
|
type: img?.mime_type || img?.type || img?.file_type,
|
||||||
mime_type: img?.mime_type || img?.file_type,
|
mime_type: img?.mime_type || img?.file_type,
|
||||||
size: img?.size,
|
size: img?.size,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const IMAGE_MAX_SIZE = 2 * 1024 * 1024;
|
const IMAGE_MAX_SIZE = 2 * 1024 * 1024;
|
||||||
|
|
@ -67,89 +67,89 @@ const isImageFile = (file: File) => file.type?.startsWith('image/');
|
||||||
const isVideoFile = (file: File) => file.type?.startsWith('video/');
|
const isVideoFile = (file: File) => file.type?.startsWith('video/');
|
||||||
|
|
||||||
const ensureSquareImage = (file: File) =>
|
const ensureSquareImage = (file: File) =>
|
||||||
new Promise<void>((resolve, reject) => {
|
new Promise<void>((resolve, reject) => {
|
||||||
const url = URL.createObjectURL(file);
|
const url = URL.createObjectURL(file);
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
if (img.width === img.height) {
|
if (img.width === img.height) {
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
reject(new Error('ابعاد تصویر Explorer باید ۱ در ۱ باشد'));
|
reject(new Error('ابعاد تصویر Explorer باید ۱ در ۱ باشد'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
img.onerror = () => {
|
img.onerror = () => {
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
reject(new Error('امکان بررسی ابعاد تصویر وجود ندارد'));
|
reject(new Error('امکان بررسی ابعاد تصویر وجود ندارد'));
|
||||||
};
|
};
|
||||||
img.src = url;
|
img.src = url;
|
||||||
});
|
});
|
||||||
|
|
||||||
const validateMediaFile = async (file: File, options?: { requireSquare?: boolean }) => {
|
const validateMediaFile = async (file: File, options?: { requireSquare?: boolean }) => {
|
||||||
if (isImageFile(file)) {
|
if (isImageFile(file)) {
|
||||||
if (file.size > IMAGE_MAX_SIZE) {
|
if (file.size > IMAGE_MAX_SIZE) {
|
||||||
throw new Error('حجم تصویر نباید بیشتر از ۲ مگابایت باشد');
|
throw new Error('حجم تصویر نباید بیشتر از ۲ مگابایت باشد');
|
||||||
|
}
|
||||||
|
if (options?.requireSquare) {
|
||||||
|
await ensureSquareImage(file);
|
||||||
|
}
|
||||||
|
} else if (isVideoFile(file)) {
|
||||||
|
if (file.size > VIDEO_MAX_SIZE) {
|
||||||
|
throw new Error('حجم ویدیو نباید بیشتر از ۲۵ مگابایت باشد');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error('فقط تصاویر یا ویدیو مجاز است');
|
||||||
}
|
}
|
||||||
if (options?.requireSquare) {
|
|
||||||
await ensureSquareImage(file);
|
|
||||||
}
|
|
||||||
} else if (isVideoFile(file)) {
|
|
||||||
if (file.size > VIDEO_MAX_SIZE) {
|
|
||||||
throw new Error('حجم ویدیو نباید بیشتر از ۲۵ مگابایت باشد');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Error('فقط تصاویر یا ویدیو مجاز است');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const validateExplorerFile = async (file: File) => {
|
const validateExplorerFile = async (file: File) => {
|
||||||
if (isImageFile(file)) {
|
if (isImageFile(file)) {
|
||||||
await ensureSquareImage(file);
|
await ensureSquareImage(file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isVideoFile(file)) {
|
if (isVideoFile(file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error('فقط تصاویر یا ویدیو مجاز است');
|
throw new Error('فقط تصاویر یا ویدیو مجاز است');
|
||||||
};
|
};
|
||||||
|
|
||||||
const mapExplorerFiles = (entries: any[]): ProductImage[] => {
|
const mapExplorerFiles = (entries: any[]): ProductImage[] => {
|
||||||
if (!entries || !Array.isArray(entries)) {
|
if (!entries || !Array.isArray(entries)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
|
||||||
return entries.map((entry, index) => {
|
|
||||||
if (entry?.file) {
|
|
||||||
const media = toPublicUrl(entry.file);
|
|
||||||
return { ...media, order: index };
|
|
||||||
}
|
}
|
||||||
if (entry?.file_id) {
|
return entries.map((entry, index) => {
|
||||||
const normalized = toPublicUrl({
|
if (entry?.file) {
|
||||||
...entry,
|
const media = toPublicUrl(entry.file);
|
||||||
id: entry.file_id,
|
return { ...media, order: index };
|
||||||
url: entry.file_url || entry.url,
|
}
|
||||||
file_url: entry.file_url || entry.url,
|
if (entry?.file_id) {
|
||||||
mime_type: entry.mime_type || entry.file_type,
|
const normalized = toPublicUrl({
|
||||||
type: entry.file_type || entry.type,
|
...entry,
|
||||||
original_name: entry.name || entry.original_name,
|
id: entry.file_id,
|
||||||
});
|
url: entry.file_url || entry.url,
|
||||||
return { ...normalized, order: index };
|
file_url: entry.file_url || entry.url,
|
||||||
}
|
mime_type: entry.mime_type || entry.file_type,
|
||||||
if (entry?.FileID) {
|
type: entry.file_type || entry.type,
|
||||||
const normalized = toPublicUrl({
|
original_name: entry.name || entry.original_name,
|
||||||
...entry,
|
});
|
||||||
id: entry.FileID,
|
return { ...normalized, order: index };
|
||||||
url: entry.file_url || entry.url,
|
}
|
||||||
file_url: entry.file_url || entry.url,
|
if (entry?.FileID) {
|
||||||
});
|
const normalized = toPublicUrl({
|
||||||
return { ...normalized, order: index };
|
...entry,
|
||||||
}
|
id: entry.FileID,
|
||||||
if (typeof entry === 'number' || typeof entry === 'string') {
|
url: entry.file_url || entry.url,
|
||||||
const normalized = toPublicUrl(entry);
|
file_url: entry.file_url || entry.url,
|
||||||
return { ...normalized, order: index };
|
});
|
||||||
}
|
return { ...normalized, order: index };
|
||||||
const normalized = toPublicUrl(entry);
|
}
|
||||||
return { ...normalized, order: index };
|
if (typeof entry === 'number' || typeof entry === 'string') {
|
||||||
});
|
const normalized = toPublicUrl(entry);
|
||||||
|
return { ...normalized, order: index };
|
||||||
|
}
|
||||||
|
const normalized = toPublicUrl(entry);
|
||||||
|
return { ...normalized, order: index };
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const ProductFormPage = () => {
|
const ProductFormPage = () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue