mirror of
https://github.com/misskey-dev/media-proxy.git
synced 2025-04-29 02:47:26 +09:00
Better Convertible Handling
This commit is contained in:
parent
28ac7a4341
commit
9b1da926e1
@ -79,8 +79,18 @@ async function proxyHandler(request, reply) {
|
|||||||
try {
|
try {
|
||||||
const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
|
const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
|
||||||
const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
|
const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
|
||||||
|
if ('emoji' in request.query ||
|
||||||
|
'avatar' in request.query ||
|
||||||
|
'static' in request.query ||
|
||||||
|
'preview' in request.query ||
|
||||||
|
'badge' in request.query) {
|
||||||
|
if (!isConvertibleImage) {
|
||||||
|
// 画像でないなら404でお茶を濁す
|
||||||
|
throw new StatusError('Unexpected mime', 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
let image = null;
|
let image = null;
|
||||||
if (('emoji' in request.query || 'avatar' in request.query) && isConvertibleImage) {
|
if ('emoji' in request.query || 'avatar' in request.query) {
|
||||||
if (!isAnimationConvertibleImage && !('static' in request.query)) {
|
if (!isAnimationConvertibleImage && !('static' in request.query)) {
|
||||||
image = {
|
image = {
|
||||||
data: fs.createReadStream(file.path),
|
data: fs.createReadStream(file.path),
|
||||||
@ -102,17 +112,13 @@ async function proxyHandler(request, reply) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ('static' in request.query && isConvertibleImage) {
|
else if ('static' in request.query) {
|
||||||
image = convertToWebpStream(file.path, 498, 280);
|
image = convertToWebpStream(file.path, 498, 280);
|
||||||
}
|
}
|
||||||
else if ('preview' in request.query && isConvertibleImage) {
|
else if ('preview' in request.query) {
|
||||||
image = convertToWebpStream(file.path, 200, 200);
|
image = convertToWebpStream(file.path, 200, 200);
|
||||||
}
|
}
|
||||||
else if ('badge' in request.query) {
|
else if ('badge' in request.query) {
|
||||||
if (!isConvertibleImage) {
|
|
||||||
// 画像でないなら404でお茶を濁す
|
|
||||||
throw new StatusError('Unexpected mime', 404);
|
|
||||||
}
|
|
||||||
const mask = sharp(file.path)
|
const mask = sharp(file.path)
|
||||||
.resize(96, 96, {
|
.resize(96, 96, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "misskey-media-proxy",
|
"name": "misskey-media-proxy",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"description": "The Media Proxy for Misskey",
|
"description": "The Media Proxy for Misskey",
|
||||||
"main": "built/index.js",
|
"main": "built/index.js",
|
||||||
"packageManager": "pnpm@7.26.0",
|
"packageManager": "pnpm@7.26.0",
|
||||||
|
25
src/index.ts
25
src/index.ts
@ -119,8 +119,22 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; };
|
|||||||
const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
|
const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
|
||||||
const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
|
const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
|
||||||
|
|
||||||
|
if (
|
||||||
|
'emoji' in request.query ||
|
||||||
|
'avatar' in request.query ||
|
||||||
|
'static' in request.query ||
|
||||||
|
'preview' in request.query ||
|
||||||
|
'badge' in request.query
|
||||||
|
) {
|
||||||
|
if (!isConvertibleImage) {
|
||||||
|
// 画像でないなら404でお茶を濁す
|
||||||
|
throw new StatusError('Unexpected mime', 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let image: IImageStreamable | null = null;
|
let image: IImageStreamable | null = null;
|
||||||
if (('emoji' in request.query || 'avatar' in request.query) && isConvertibleImage) {
|
|
||||||
|
if ('emoji' in request.query || 'avatar' in request.query) {
|
||||||
if (!isAnimationConvertibleImage && !('static' in request.query)) {
|
if (!isAnimationConvertibleImage && !('static' in request.query)) {
|
||||||
image = {
|
image = {
|
||||||
data: fs.createReadStream(file.path),
|
data: fs.createReadStream(file.path),
|
||||||
@ -141,16 +155,11 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; };
|
|||||||
type: 'image/webp',
|
type: 'image/webp',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else if ('static' in request.query && isConvertibleImage) {
|
} else if ('static' in request.query) {
|
||||||
image = convertToWebpStream(file.path, 498, 280);
|
image = convertToWebpStream(file.path, 498, 280);
|
||||||
} else if ('preview' in request.query && isConvertibleImage) {
|
} else if ('preview' in request.query) {
|
||||||
image = convertToWebpStream(file.path, 200, 200);
|
image = convertToWebpStream(file.path, 200, 200);
|
||||||
} else if ('badge' in request.query) {
|
} else if ('badge' in request.query) {
|
||||||
if (!isConvertibleImage) {
|
|
||||||
// 画像でないなら404でお茶を濁す
|
|
||||||
throw new StatusError('Unexpected mime', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
const mask = sharp(file.path)
|
const mask = sharp(file.path)
|
||||||
.resize(96, 96, {
|
.resize(96, 96, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user