feat(uploads): accept 15 MB / 8000px and every standard image format
- API ceiling 8 MB -> 15 MB, sharp limitInputPixels 64 MP (8000x8000) - accept AVIF, BMP, TIFF, HEIC/HEIF, JXL alongside JPEG/PNG/WebP/GIF/SVG/PDF; fall back to the file extension when the browser sends an empty or octet-stream type - convert formats browsers cannot paint (TIFF, HEIC, JXL) to WebP on upload - pre-warm 200/400/600/1600 variants so card thumbnails never wait on a render - nginx: client_max_body_size 20m, proxy_read_timeout 180s (the 1m default rejected every real photo with a bare 413, and a slow 8000px render became a 504 that left the card thumbnail broken while /uploads worked) - gallery tile falls back to the original file if a variant fails Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
072344f85e
commit
387222851c
@@ -5,6 +5,16 @@ server {
|
|||||||
root /usr/share/nginx/html;
|
root /usr/share/nginx/html;
|
||||||
index index.html;
|
index index.html;
|
||||||
|
|
||||||
|
# Uploads are capped at 15 MB by the API; nginx's 1 MB default would reject
|
||||||
|
# any real photo with a bare 413 before the request ever reached it.
|
||||||
|
client_max_body_size 20m;
|
||||||
|
|
||||||
|
# The first request for a given /api/img width renders it on demand; an
|
||||||
|
# 8000px source can take a while on a small VPS, and nginx's 60s default
|
||||||
|
# turned that into a 504 — a card thumbnail that stayed broken until the
|
||||||
|
# variant finished, while the original (/uploads) opened fine.
|
||||||
|
proxy_read_timeout 180s;
|
||||||
|
|
||||||
# Docker's embedded DNS. Without re-resolving at request time, nginx caches the
|
# Docker's embedded DNS. Without re-resolving at request time, nginx caches the
|
||||||
# api container's IP from startup and keeps returning 502 ("no live upstreams")
|
# api container's IP from startup and keeps returning 502 ("no live upstreams")
|
||||||
# after the api container restarts with a new IP.
|
# after the api container restarts with a new IP.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
-->
|
-->
|
||||||
<div class="gallery-upload">
|
<div class="gallery-upload">
|
||||||
<a-upload-dragger
|
<a-upload-dragger
|
||||||
accept="image/*"
|
accept="image/jpeg,image/png,image/webp,image/gif,image/avif,image/bmp,image/tiff,image/heic,image/heif,image/svg+xml"
|
||||||
multiple
|
multiple
|
||||||
:show-upload-list="false"
|
:show-upload-list="false"
|
||||||
:custom-request="doUpload"
|
:custom-request="doUpload"
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
class="tile"
|
class="tile"
|
||||||
:class="{ 'is-uploading': item.status === 'uploading' }"
|
:class="{ 'is-uploading': item.status === 'uploading' }"
|
||||||
>
|
>
|
||||||
<img :src="imageUrl(item.url, 400) ?? item.preview" alt="" />
|
<img :src="imageUrl(item.url, 400) ?? item.preview" alt="" @error="fallback($event, item)" />
|
||||||
|
|
||||||
<div v-if="item.status === 'uploading'" class="tile__progress">
|
<div v-if="item.status === 'uploading'" class="tile__progress">
|
||||||
<a-progress type="circle" :percent="item.percent" :size="40" :stroke-width="8" stroke-color="#fff" trail-color="rgba(255,255,255,.3)" :show-info="false" />
|
<a-progress type="circle" :percent="item.percent" :size="40" :stroke-width="8" stroke-color="#fff" trail-color="rgba(255,255,255,.3)" :show-info="false" />
|
||||||
@@ -196,6 +196,18 @@ function preview(item: Item) {
|
|||||||
previewVisible.value = true;
|
previewVisible.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A resized preview is a bonus, not a requirement: when the variant endpoint
|
||||||
|
* fails (for example while the server is still rendering a huge source image)
|
||||||
|
* fall back to the stored original so the tile never shows a broken image.
|
||||||
|
*/
|
||||||
|
function fallback(e: Event, item: Item) {
|
||||||
|
const img = e.target as HTMLImageElement;
|
||||||
|
if (item.url && img.src !== new URL(item.url, window.location.origin).href) {
|
||||||
|
img.src = item.url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
items.value.forEach((i) => i.preview && URL.revokeObjectURL(i.preview));
|
items.value.forEach((i) => i.preview && URL.revokeObjectURL(i.preview));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
:file-list="fileList"
|
:file-list="fileList"
|
||||||
list-type="picture-card"
|
list-type="picture-card"
|
||||||
:max-count="1"
|
:max-count="1"
|
||||||
accept="image/*"
|
accept="image/jpeg,image/png,image/webp,image/gif,image/avif,image/bmp,image/tiff,image/heic,image/heif,image/svg+xml"
|
||||||
:custom-request="doUpload"
|
:custom-request="doUpload"
|
||||||
:before-upload="beforeUpload"
|
:before-upload="beforeUpload"
|
||||||
@remove="handleRemove"
|
@remove="handleRemove"
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
// Checks and error wording shared by the image uploaders. The limits mirror
|
// Checks and error wording shared by the image uploaders. The limits mirror
|
||||||
// AdminUploadsService in apps/api.
|
// AdminUploadsService in apps/api: 15 MB, up to 8000×8000, every standard
|
||||||
export const MAX_UPLOAD_BYTES = 8 * 1024 * 1024;
|
// image format (plus PDF for document slots).
|
||||||
|
export const MAX_UPLOAD_BYTES = 15 * 1024 * 1024;
|
||||||
|
|
||||||
const IMAGE_TYPES = /^image\/(jpeg|png|gif|webp|svg\+xml)$/;
|
const IMAGE_TYPES =
|
||||||
|
/^image\/(jpeg|jpg|pjpeg|png|apng|gif|webp|svg\+xml|avif|bmp|x-ms-bmp|tiff|heic|heif|jxl)$/;
|
||||||
|
|
||||||
|
/** Same list by extension, for files the browser reports with an empty type. */
|
||||||
|
const IMAGE_EXTS = /\.(jpe?g|png|apng|gif|webp|svg|avif|bmp|tiff?|heic|heif|jxl)$/i;
|
||||||
|
|
||||||
/** Null when the file may be uploaded, otherwise the message to show. */
|
/** Null when the file may be uploaded, otherwise the message to show. */
|
||||||
export function checkImageFile(file: File): string | null {
|
export function checkImageFile(file: File): string | null {
|
||||||
if (!IMAGE_TYPES.test(file.type)) {
|
const known = file.type ? IMAGE_TYPES.test(file.type) : IMAGE_EXTS.test(file.name);
|
||||||
return `«${file.name}» پشتیبانی نمیشود؛ فقط تصویر JPG، PNG، WebP، GIF یا SVG`;
|
if (!known) {
|
||||||
|
return `«${file.name}» پشتیبانی نمیشود؛ تصویر JPG، PNG، WebP، GIF، SVG، AVIF، BMP، TIFF یا HEIC انتخاب کنید`;
|
||||||
}
|
}
|
||||||
if (file.size > MAX_UPLOAD_BYTES) {
|
if (file.size > MAX_UPLOAD_BYTES) {
|
||||||
return `حجم «${file.name}» بیشتر از ۸ مگابایت است؛ فایل کوچکتری انتخاب کنید`;
|
return `حجم «${file.name}» بیشتر از ۱۵ مگابایت است؛ فایل کوچکتری انتخاب کنید`;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ import { masterOtpCode, masterOtpEnabled } from './modules/customer-auth/custome
|
|||||||
// content), but a strict client or an <img> under some CSPs would not.
|
// content), but a strict client or an <img> under some CSPs would not.
|
||||||
const EXTRA_MIME_TYPES: Record<string, string> = {
|
const EXTRA_MIME_TYPES: Record<string, string> = {
|
||||||
'.avif': 'image/avif',
|
'.avif': 'image/avif',
|
||||||
|
'.bmp': 'image/bmp',
|
||||||
|
'.tif': 'image/tiff',
|
||||||
|
'.tiff': 'image/tiff',
|
||||||
|
'.heic': 'image/heic',
|
||||||
|
'.heif': 'image/heif',
|
||||||
|
'.jxl': 'image/jxl',
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -68,10 +68,24 @@ describe('AdminUploadsService.store', () => {
|
|||||||
|
|
||||||
await expect(svc.store(undefined, undefined, null)).rejects.toThrow('فایلی انتخاب نشده است');
|
await expect(svc.store(undefined, undefined, null)).rejects.toThrow('فایلی انتخاب نشده است');
|
||||||
await expect(
|
await expect(
|
||||||
svc.store(file({ buffer: Buffer.alloc(8 * 1024 * 1024 + 1) }), undefined, null),
|
svc.store(file({ buffer: Buffer.alloc(15 * 1024 * 1024 + 1) }), undefined, null),
|
||||||
).rejects.toThrow('بیشتر از ۸ مگابایت');
|
).rejects.toThrow('بیشتر از ۱۵ مگابایت');
|
||||||
await expect(
|
await expect(
|
||||||
svc.store(file({ originalname: 'a.heic', mimetype: 'image/heic' }), undefined, null),
|
svc.store(file({ originalname: 'setup.exe', mimetype: 'application/x-msdownload' }), undefined, null),
|
||||||
).rejects.toBeInstanceOf(BadRequestException);
|
).rejects.toBeInstanceOf(BadRequestException);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('accepts every standard image format, including HEIC and TIFF', async () => {
|
||||||
|
const { svc } = build(async () => null);
|
||||||
|
for (const [name, mime] of [
|
||||||
|
['photo.heic', 'image/heic'],
|
||||||
|
['scan.tiff', 'image/tiff'],
|
||||||
|
['art.bmp', 'image/bmp'],
|
||||||
|
['pic.avif', 'image/avif'],
|
||||||
|
['logo.svg', 'image/svg+xml'],
|
||||||
|
]) {
|
||||||
|
await expect(svc.store(file({ originalname: name, mimetype: mime }), undefined, null)).resolves
|
||||||
|
.toMatchObject({ originalName: name });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
|
|||||||
import { extname, join } from 'node:path';
|
import { extname, join } from 'node:path';
|
||||||
import { PrismaService } from '../../prisma/prisma.service';
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
import { ImageVariantService } from './image-variants.service';
|
import { ImageVariantService } from './image-variants.service';
|
||||||
import { getUploadDir } from './upload-path';
|
import { getUploadDir, mimeForUpload } from './upload-path';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* File uploads — ports legacy `POST file/create`
|
* File uploads — ports legacy `POST file/create`
|
||||||
@@ -30,11 +30,17 @@ export class AdminUploadsService {
|
|||||||
private readonly dir = getUploadDir();
|
private readonly dir = getUploadDir();
|
||||||
|
|
||||||
/** Mirrors legacy's `type` field: 1 = image, 2 = any other document. */
|
/** Mirrors legacy's `type` field: 1 = image, 2 = any other document. */
|
||||||
private static readonly IMAGE_MIME = /^image\/(jpeg|png|gif|webp|svg\+xml)$/;
|
private static readonly IMAGE_MIME =
|
||||||
private static readonly ALLOWED = /^(image\/(jpeg|png|gif|webp|svg\+xml)|application\/pdf)$/;
|
/^image\/(jpeg|jpg|pjpeg|png|apng|gif|webp|svg\+xml|avif|bmp|x-ms-bmp|tiff|heic|heif|jxl)$/;
|
||||||
|
private static readonly ALLOWED =
|
||||||
|
/^(image\/(jpeg|jpg|pjpeg|png|apng|gif|webp|svg\+xml|avif|bmp|x-ms-bmp|tiff|heic|heif|jxl)|application\/pdf)$/;
|
||||||
|
|
||||||
/** 8 MB. Legacy relied on PHP's upload_max_filesize; this makes it explicit. */
|
/**
|
||||||
private static readonly MAX_BYTES = 8 * 1024 * 1024;
|
* 15 MB. Legacy relied on PHP's upload_max_filesize; this makes it explicit.
|
||||||
|
* The ceiling is paired with ImageVariantService's `limitInputPixels`, which
|
||||||
|
* accepts up to 8000×8000 (64 MP) rasters.
|
||||||
|
*/
|
||||||
|
private static readonly MAX_BYTES = 15 * 1024 * 1024;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly prisma: PrismaService,
|
private readonly prisma: PrismaService,
|
||||||
@@ -47,14 +53,20 @@ export class AdminUploadsService {
|
|||||||
actorAdminUserId: string | null,
|
actorAdminUserId: string | null,
|
||||||
) {
|
) {
|
||||||
if (!file) throw new BadRequestException('فایلی انتخاب نشده است');
|
if (!file) throw new BadRequestException('فایلی انتخاب نشده است');
|
||||||
|
// Some browsers and OSes send an empty type or application/octet-stream
|
||||||
|
// for formats they do not have registered (HEIC, TIFF, JXL...). Fall back
|
||||||
|
// to the extension so those uploads are not rejected for a missing label.
|
||||||
|
const mime = mimeForUpload(file.originalname) ?? file.mimetype;
|
||||||
if (file.size > AdminUploadsService.MAX_BYTES) {
|
if (file.size > AdminUploadsService.MAX_BYTES) {
|
||||||
throw new BadRequestException('حجم فایل بیشتر از ۸ مگابایت است؛ فایل کوچکتری انتخاب کنید');
|
throw new BadRequestException('حجم فایل بیشتر از ۱۵ مگابایت است؛ فایل کوچکتری انتخاب کنید');
|
||||||
}
|
}
|
||||||
if (!AdminUploadsService.ALLOWED.test(file.mimetype)) {
|
if (!AdminUploadsService.ALLOWED.test(mime)) {
|
||||||
throw new BadRequestException('این نوع فایل پشتیبانی نمیشود؛ فقط JPG، PNG، WebP، GIF، SVG یا PDF');
|
throw new BadRequestException(
|
||||||
|
'این نوع فایل پشتیبانی نمیشود؛ تصویر JPG، PNG، WebP، GIF، SVG، AVIF، BMP، TIFF، HEIC یا سند PDF انتخاب کنید',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const compressed = await this.variants.compressUpload(file.buffer, file.mimetype, file.originalname);
|
const compressed = await this.variants.compressUpload(file.buffer, mime, file.originalname);
|
||||||
const buffer = compressed?.buffer ?? file.buffer;
|
const buffer = compressed?.buffer ?? file.buffer;
|
||||||
|
|
||||||
// legacy: md5(time() . original_name) . '.' . ext
|
// legacy: md5(time() . original_name) . '.' . ext
|
||||||
@@ -71,7 +83,7 @@ export class AdminUploadsService {
|
|||||||
data: {
|
data: {
|
||||||
url: `/uploads/${stored}`,
|
url: `/uploads/${stored}`,
|
||||||
originalName: file.originalname,
|
originalName: file.originalname,
|
||||||
mimeType: compressed?.mimeType ?? file.mimetype,
|
mimeType: compressed?.mimeType ?? mime,
|
||||||
sizeBytes: buffer.length,
|
sizeBytes: buffer.length,
|
||||||
purpose: purpose ?? null,
|
purpose: purpose ?? null,
|
||||||
uploadedByAdminId: actorAdminUserId,
|
uploadedByAdminId: actorAdminUserId,
|
||||||
|
|||||||
@@ -123,6 +123,18 @@ describe('ImageVariantService', () => {
|
|||||||
fakeSharp(svc, () => 4000 * KB);
|
fakeSharp(svc, () => 4000 * KB);
|
||||||
await expect(svc.compressUpload(big, 'image/webp', 'a.webp')).resolves.toBeNull();
|
await expect(svc.compressUpload(big, 'image/webp', 'a.webp')).resolves.toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('always converts formats browsers cannot display, even when small', async () => {
|
||||||
|
const svc = new ImageVariantService();
|
||||||
|
const calls = fakeSharp(svc, () => 10 * KB);
|
||||||
|
|
||||||
|
const out = await svc.compressUpload(Buffer.alloc(20 * KB), 'image/tiff', 'scan.tiff');
|
||||||
|
expect(calls).toHaveLength(5);
|
||||||
|
expect(out).toMatchObject({ ext: '.webp', mimeType: 'image/webp' });
|
||||||
|
|
||||||
|
const heic = await svc.compressUpload(Buffer.alloc(20 * KB), 'image/heic', 'photo.heic');
|
||||||
|
expect(heic).toMatchObject({ ext: '.webp' });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('falls back to the original when a render fails', async () => {
|
it('falls back to the original when a render fails', async () => {
|
||||||
|
|||||||
@@ -8,11 +8,35 @@ export const IMAGE_WIDTHS = [200, 400, 600, 800, 1200, 1600] as const;
|
|||||||
export type ImageWidth = (typeof IMAGE_WIDTHS)[number];
|
export type ImageWidth = (typeof IMAGE_WIDTHS)[number];
|
||||||
|
|
||||||
/** Rendered right after an upload so the first shopper never waits on a resize. */
|
/** Rendered right after an upload so the first shopper never waits on a resize. */
|
||||||
const WARM_WIDTHS: ImageWidth[] = [600, 1600];
|
const WARM_WIDTHS: ImageWidth[] = [200, 400, 600, 1600];
|
||||||
|
|
||||||
/** GIF is skipped so animations survive; SVG and PDF are not raster. */
|
/** GIF is skipped so animations survive; SVG and PDF are not raster. */
|
||||||
const RESIZABLE = new Set(['.jpg', '.jpeg', '.png', '.webp', '.avif']);
|
const RESIZABLE = new Set(['.jpg', '.jpeg', '.png', '.webp', '.avif', '.bmp', '.tif', '.tiff', '.heic', '.heif']);
|
||||||
const RESIZABLE_MIME = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/avif']);
|
const RESIZABLE_MIME = new Set([
|
||||||
|
'image/jpeg',
|
||||||
|
'image/jpg',
|
||||||
|
'image/pjpeg',
|
||||||
|
'image/png',
|
||||||
|
'image/apng',
|
||||||
|
'image/webp',
|
||||||
|
'image/avif',
|
||||||
|
'image/bmp',
|
||||||
|
'image/x-ms-bmp',
|
||||||
|
'image/tiff',
|
||||||
|
'image/heic',
|
||||||
|
'image/heif',
|
||||||
|
'image/jxl',
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a browser may fail to display at all (TIFF, HEIC/HEIF, JXL, some
|
||||||
|
* BMPs). Those are always re-encoded to WebP — even when the file is small —
|
||||||
|
* so a stored upload is never a card thumbnail the browser cannot paint.
|
||||||
|
*/
|
||||||
|
const BROWSER_UNSAFE_MIME = new Set(['image/tiff', 'image/heic', 'image/heif', 'image/jxl']);
|
||||||
|
|
||||||
|
/** 8000×8000, the largest upload the panel accepts. */
|
||||||
|
const MAX_INPUT_PIXELS = 8000 * 8000;
|
||||||
|
|
||||||
const WEBP_QUALITY = 78;
|
const WEBP_QUALITY = 78;
|
||||||
|
|
||||||
@@ -42,7 +66,10 @@ interface SharpPipeline {
|
|||||||
toFile(path: string): Promise<unknown>;
|
toFile(path: string): Promise<unknown>;
|
||||||
toBuffer(): Promise<Buffer>;
|
toBuffer(): Promise<Buffer>;
|
||||||
}
|
}
|
||||||
type SharpFactory = (input: string | Buffer, opts?: { failOn?: 'none' }) => SharpPipeline;
|
type SharpFactory = (
|
||||||
|
input: string | Buffer,
|
||||||
|
opts?: { failOn?: 'none'; limitInputPixels?: number },
|
||||||
|
) => SharpPipeline;
|
||||||
|
|
||||||
export interface CompressedUpload {
|
export interface CompressedUpload {
|
||||||
buffer: Buffer;
|
buffer: Buffer;
|
||||||
@@ -115,28 +142,33 @@ export class ImageVariantService {
|
|||||||
mimeType: string,
|
mimeType: string,
|
||||||
filename: string,
|
filename: string,
|
||||||
): Promise<CompressedUpload | null> {
|
): Promise<CompressedUpload | null> {
|
||||||
if (buffer.length <= ORIGINAL_TARGET_BYTES) return null;
|
|
||||||
if (!RESIZABLE_MIME.has(mimeType)) return null;
|
if (!RESIZABLE_MIME.has(mimeType)) return null;
|
||||||
|
// Small uploads are kept as sent — except formats browsers cannot paint,
|
||||||
|
// which are always converted so the card thumbnail is never broken.
|
||||||
|
const mustConvert = BROWSER_UNSAFE_MIME.has(mimeType);
|
||||||
|
if (buffer.length <= ORIGINAL_TARGET_BYTES && !mustConvert) return null;
|
||||||
const sharp = this.loadSharp();
|
const sharp = this.loadSharp();
|
||||||
if (!sharp) return null;
|
if (!sharp) return null;
|
||||||
|
|
||||||
let smallest: Buffer | null = null;
|
let smallest: Buffer | null = null;
|
||||||
try {
|
try {
|
||||||
for (const { side, quality } of ORIGINAL_ATTEMPTS) {
|
for (const { side, quality } of ORIGINAL_ATTEMPTS) {
|
||||||
const out = await sharp(buffer, { failOn: 'none' })
|
const out = await sharp(buffer, { failOn: 'none', limitInputPixels: MAX_INPUT_PIXELS })
|
||||||
.rotate()
|
.rotate()
|
||||||
.resize({ width: side, height: side, fit: 'inside', withoutEnlargement: true })
|
.resize({ width: side, height: side, fit: 'inside', withoutEnlargement: true })
|
||||||
.webp({ quality })
|
.webp({ quality })
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
if (!smallest || out.length < smallest.length) smallest = out;
|
if (!smallest || out.length < smallest.length) smallest = out;
|
||||||
if (out.length <= ORIGINAL_TARGET_BYTES) break;
|
if (out.length <= ORIGINAL_TARGET_BYTES && !mustConvert) break;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.warn(`compress failed for ${filename}: ${(err as Error).message}`);
|
this.logger.warn(`compress failed for ${filename}: ${(err as Error).message}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!smallest || smallest.length >= buffer.length) return null;
|
if (!smallest) return null;
|
||||||
|
// A conversion is mandatory for browser-unsafe formats even if larger.
|
||||||
|
if (!mustConvert && smallest.length >= buffer.length) return null;
|
||||||
if (smallest.length > ORIGINAL_CEILING_BYTES) {
|
if (smallest.length > ORIGINAL_CEILING_BYTES) {
|
||||||
this.logger.warn(`${filename} is still ${smallest.length} bytes after compression`);
|
this.logger.warn(`${filename} is still ${smallest.length} bytes after compression`);
|
||||||
}
|
}
|
||||||
@@ -168,7 +200,7 @@ export class ImageVariantService {
|
|||||||
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`;
|
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`;
|
||||||
try {
|
try {
|
||||||
await mkdir(dirname(target), { recursive: true });
|
await mkdir(dirname(target), { recursive: true });
|
||||||
await sharp(original, { failOn: 'none' })
|
await sharp(original, { failOn: 'none', limitInputPixels: MAX_INPUT_PIXELS })
|
||||||
.rotate()
|
.rotate()
|
||||||
.resize({ width, withoutEnlargement: true })
|
.resize({ width, withoutEnlargement: true })
|
||||||
.webp({ quality: WEBP_QUALITY })
|
.webp({ quality: WEBP_QUALITY })
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ const UPLOAD_MIME: Record<string, string> = {
|
|||||||
'.gif': 'image/gif',
|
'.gif': 'image/gif',
|
||||||
'.webp': 'image/webp',
|
'.webp': 'image/webp',
|
||||||
'.avif': 'image/avif',
|
'.avif': 'image/avif',
|
||||||
|
'.bmp': 'image/bmp',
|
||||||
|
'.tif': 'image/tiff',
|
||||||
|
'.tiff': 'image/tiff',
|
||||||
|
'.heic': 'image/heic',
|
||||||
|
'.heif': 'image/heif',
|
||||||
|
'.jxl': 'image/jxl',
|
||||||
'.svg': 'image/svg+xml',
|
'.svg': 'image/svg+xml',
|
||||||
'.pdf': 'application/pdf',
|
'.pdf': 'application/pdf',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,16 @@ server {
|
|||||||
root /usr/share/nginx/html;
|
root /usr/share/nginx/html;
|
||||||
index index.html;
|
index index.html;
|
||||||
|
|
||||||
|
# Uploads are capped at 15 MB by the API; nginx's 1 MB default would reject
|
||||||
|
# any real photo with a bare 413 before the request ever reached it.
|
||||||
|
client_max_body_size 20m;
|
||||||
|
|
||||||
|
# The first request for a given /api/img width renders it on demand; an
|
||||||
|
# 8000px source can take a while on a small VPS, and nginx's 60s default
|
||||||
|
# turned that into a 504 — a card thumbnail that stayed broken until the
|
||||||
|
# variant finished, while the original (/uploads) opened fine.
|
||||||
|
proxy_read_timeout 180s;
|
||||||
|
|
||||||
# Docker's embedded DNS. Without re-resolving at request time, nginx caches the
|
# Docker's embedded DNS. Without re-resolving at request time, nginx caches the
|
||||||
# api container's IP from startup and keeps returning 502 ("no live upstreams")
|
# api container's IP from startup and keeps returning 502 ("no live upstreams")
|
||||||
# after the api container restarts with a new IP.
|
# after the api container restarts with a new IP.
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<input
|
<input
|
||||||
ref="input"
|
ref="input"
|
||||||
type="file"
|
type="file"
|
||||||
accept="image/jpeg,image/png,image/webp,application/pdf"
|
accept="image/jpeg,image/png,image/webp,image/gif,image/avif,image/bmp,image/tiff,image/heic,image/heif,image/svg+xml,application/pdf"
|
||||||
class="siu__input"
|
class="siu__input"
|
||||||
@change="onPick"
|
@change="onPick"
|
||||||
/>
|
/>
|
||||||
@@ -58,9 +58,12 @@ const error = ref<string | null>(null)
|
|||||||
const fileId = ref<string | null>(null)
|
const fileId = ref<string | null>(null)
|
||||||
const preview = ref<string | null>(null)
|
const preview = ref<string | null>(null)
|
||||||
|
|
||||||
/** 8 MB — the same ceiling the API enforces, checked here for a fast message. */
|
/** 15 MB — the same ceiling the API enforces, checked here for a fast message. */
|
||||||
const MAX_BYTES = 8 * 1024 * 1024
|
const MAX_BYTES = 15 * 1024 * 1024
|
||||||
const ACCEPTED = /^(image\/(jpeg|png|webp)|application\/pdf)$/
|
const ACCEPTED =
|
||||||
|
/^(image\/(jpeg|jpg|pjpeg|png|apng|gif|webp|svg\+xml|avif|bmp|x-ms-bmp|tiff|heic|heif|jxl)|application\/pdf)$/
|
||||||
|
/** Same list by extension, for files the browser reports with an empty type. */
|
||||||
|
const ACCEPTED_EXT = /\.(jpe?g|png|apng|gif|webp|svg|avif|bmp|tiff?|heic|heif|jxl|pdf)$/i
|
||||||
|
|
||||||
function failureText(e: unknown): string {
|
function failureText(e: unknown): string {
|
||||||
if (e instanceof UnauthorizedError) return 'برای بارگذاری، دوباره وارد حساب شوید'
|
if (e instanceof UnauthorizedError) return 'برای بارگذاری، دوباره وارد حساب شوید'
|
||||||
@@ -79,7 +82,7 @@ async function onPick(event: Event) {
|
|||||||
|
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
if (!ACCEPTED.test(file.type)) {
|
if (!(file.type ? ACCEPTED.test(file.type) : ACCEPTED_EXT.test(file.name))) {
|
||||||
error.value = 'این نوع فایل پشتیبانی نمیشود؛ فقط تصویر JPG، PNG، WebP یا PDF'
|
error.value = 'این نوع فایل پشتیبانی نمیشود؛ فقط تصویر JPG، PNG، WebP یا PDF'
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ server {
|
|||||||
root /usr/share/nginx/html;
|
root /usr/share/nginx/html;
|
||||||
index index.html;
|
index index.html;
|
||||||
|
|
||||||
|
# Uploads are capped at 15 MB by the API; nginx's 1 MB default would reject
|
||||||
|
# any real photo with a bare 413 before the request ever reached it.
|
||||||
|
client_max_body_size 20m;
|
||||||
|
|
||||||
location /api/ {
|
location /api/ {
|
||||||
proxy_pass http://api:3000;
|
proxy_pass http://api:3000;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
|
|||||||
Reference in New Issue
Block a user