Skip to content

Commit

Permalink
0.0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaina committed Mar 3, 2023
1 parent 969d27e commit 78cf246
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
4 changes: 2 additions & 2 deletions built/file-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function checkSvg(path) {
import { FILE_TYPE_BROWSERSAFE } from './const.js';
const dictionary = {
'safe-file': FILE_TYPE_BROWSERSAFE,
'sharp-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/avif', 'image/svg+xml'],
'sharp-animation-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml'],
'sharp-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/avif', 'image/svg+xml', 'image/x-icon', 'image/bmp'],
'sharp-animation-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml', 'image/x-icon', 'image/bmp'],
};
export const isMimeImage = (mime, type) => dictionary[type].includes(mime);
11 changes: 6 additions & 5 deletions built/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { dirname } from 'node:path';
import fastifyStatic from '@fastify/static';
import { createTemp } from './create-temp.js';
import { FILE_TYPE_BROWSERSAFE } from './const.js';
import { convertToWebpStream, webpDefault } from './image-processor.js';
import { convertToWebpStream, webpDefault, convertSharpToWebpStream } from './image-processor.js';
import { detectType, isMimeImage } from './file-info.js';
import sharp from 'sharp';
import { sharpBmp } from 'sharp-read-bmp';
import { StatusError } from './status-error.js';
import { defaultDownloadConfig, downloadUrl } from './download.js';
import { getAgents } from './http.js';
Expand Down Expand Up @@ -107,7 +108,7 @@ async function proxyHandler(request, reply) {
};
}
else {
const data = sharp(file.path, { animated: !('static' in request.query) })
const data = (await sharpBmp(file.path, file.mime, { animated: !('static' in request.query) }))
.resize({
height: 'emoji' in request.query ? 128 : 320,
withoutEnlargement: true,
Expand All @@ -121,13 +122,13 @@ async function proxyHandler(request, reply) {
}
}
else if ('static' in request.query) {
image = convertToWebpStream(file.path, 498, 280);
image = convertSharpToWebpStream(await sharpBmp(file.path, file.mime), 498, 280);
}
else if ('preview' in request.query) {
image = convertToWebpStream(file.path, 200, 200);
image = convertSharpToWebpStream(await sharpBmp(file.path, file.mime), 200, 200);
}
else if ('badge' in request.query) {
const mask = sharp(file.path)
const mask = (await sharpBmp(file.path, file.mime))
.resize(96, 96, {
fit: 'inside',
withoutEnlargement: false,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "misskey-media-proxy",
"version": "0.0.16",
"version": "0.0.17",
"description": "The Media Proxy for Misskey",
"main": "built/index.js",
"packageManager": "pnpm@7.26.0",
"packageManager": "pnpm@7.28.0",
"type": "module",
"files": [
"built",
Expand Down Expand Up @@ -47,6 +47,7 @@
"is-svg": "^4.3.2",
"private-ip": "^3.0.0",
"sharp": "^0.31.3",
"sharp-read-bmp": "github:misskey-dev/sharp-read-bmp",
"tmp": "^0.2.1"
}
}
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/file-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ import { FILE_TYPE_BROWSERSAFE } from './const.js';

const dictionary = {
'safe-file': FILE_TYPE_BROWSERSAFE,
'sharp-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/avif', 'image/svg+xml'],
'sharp-animation-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml'],
'sharp-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/avif', 'image/svg+xml', 'image/x-icon', 'image/bmp'],
'sharp-animation-convertible-image': ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml', 'image/x-icon', 'image/bmp'],
};

export const isMimeImage = (mime: string, type: keyof typeof dictionary): boolean => dictionary[type].includes(mime);
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { dirname } from 'node:path';
import fastifyStatic from '@fastify/static';
import { createTemp } from './create-temp.js';
import { FILE_TYPE_BROWSERSAFE } from './const.js';
import { IImageStreamable, convertToWebpStream, webpDefault } from './image-processor.js';
import { IImageStreamable, convertToWebpStream, webpDefault, convertSharpToWebpStream } from './image-processor.js';
import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify';
import { detectType, isMimeImage } from './file-info.js';
import sharp from 'sharp';
import { sharpBmp } from 'sharp-read-bmp';
import { StatusError } from './status-error.js';
import { DownloadConfig, defaultDownloadConfig, downloadUrl } from './download.js';
import { getAgents } from './http.js';
Expand Down Expand Up @@ -153,12 +154,12 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; };
type: file.mime,
};
} else {
const data = sharp(file.path, { animated: !('static' in request.query) })
.resize({
height: 'emoji' in request.query ? 128 : 320,
withoutEnlargement: true,
})
.webp(webpDefault);
const data = (await sharpBmp(file.path, file.mime, { animated: !('static' in request.query) }))
.resize({
height: 'emoji' in request.query ? 128 : 320,
withoutEnlargement: true,
})
.webp(webpDefault);

image = {
data,
Expand All @@ -167,11 +168,11 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; };
};
}
} else if ('static' in request.query) {
image = convertToWebpStream(file.path, 498, 280);
image = convertSharpToWebpStream(await sharpBmp(file.path, file.mime), 498, 280);
} else if ('preview' in request.query) {
image = convertToWebpStream(file.path, 200, 200);
image = convertSharpToWebpStream(await sharpBmp(file.path, file.mime), 200, 200);
} else if ('badge' in request.query) {
const mask = sharp(file.path)
const mask = (await sharpBmp(file.path, file.mime))
.resize(96, 96, {
fit: 'inside',
withoutEnlargement: false,
Expand Down

0 comments on commit 78cf246

Please sign in to comment.