summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/ImageProcessingService.ts
diff options
context:
space:
mode:
authortamaina <tamaina@hotmail.co.jp>2023-01-26 16:06:29 +0900
committerGitHub <noreply@github.com>2023-01-26 16:06:29 +0900
commit4735ae64516a82b525a8dfd16a04c4b91d1f23c0 (patch)
treec0281b6cfd941e315a7b61e8b1737ff21882a5c0 /packages/backend/src/core/ImageProcessingService.ts
parenttweak custom emoji handling (diff)
downloadsharkey-4735ae64516a82b525a8dfd16a04c4b91d1f23c0.tar.gz
sharkey-4735ae64516a82b525a8dfd16a04c4b91d1f23c0.tar.bz2
sharkey-4735ae64516a82b525a8dfd16a04c4b91d1f23c0.zip
refactor: /proxyをFileServerServiceに統合し、/proxyのurlで/filesが指定されていた場合は直接ファイルを解決するようにする (#9709)
* wip? * clean up * Implement? HttpFetchService * :v: * remove node-fetch * fix * refactor * fix * gateway timeout * UndiciFetcherクラスを追加 (仮コミット, ビルドもstartもさせていない) * fix * add logger and fix url preview * fix ip check * enhance logger and error handling * fix * fix * clean up * Use custom fetcher for ApRequest / ApResolver * bypassProxyはproxyBypassHostsに判断を委譲するように * set maxRedirections (default 3, ApRequest/ApResolver: 0) * fix * wip???? * wip * :v: * set .node-version * clean up * refactor * clean up * refactor * refactor detectRequestType * rename detectResponseType * :v: * fix * wip * clean up * no got * remove got * wip * :v: * fix * clean up * remove unnnecessary const * good cleanup * no stream * Revert "no stream" This reverts commit 636f9192fcd2b17e71bbf6b5b106b490e0f66244. * fix * cache-control: max-age=300 to error * refactor cleanup
Diffstat (limited to 'packages/backend/src/core/ImageProcessingService.ts')
-rw-r--r--packages/backend/src/core/ImageProcessingService.ts32
1 files changed, 31 insertions, 1 deletions
diff --git a/packages/backend/src/core/ImageProcessingService.ts b/packages/backend/src/core/ImageProcessingService.ts
index 312189eea4..fbc02f504b 100644
--- a/packages/backend/src/core/ImageProcessingService.ts
+++ b/packages/backend/src/core/ImageProcessingService.ts
@@ -9,6 +9,14 @@ export type IImage = {
type: string;
};
+export type IImageStream = {
+ data: Readable;
+ ext: string | null;
+ type: string;
+};
+
+export type IImageStreamable = IImage | IImageStream;
+
export const webpDefault: sharp.WebpOptions = {
quality: 85,
alphaQuality: 95,
@@ -19,6 +27,7 @@ export const webpDefault: sharp.WebpOptions = {
};
import { bindThis } from '@/decorators.js';
+import { Readable } from 'node:stream';
@Injectable()
export class ImageProcessingService {
@@ -64,7 +73,7 @@ export class ImageProcessingService {
*/
@bindThis
public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
- return this.convertSharpToWebp(await sharp(path), width, height, options);
+ return this.convertSharpToWebp(sharp(path), width, height, options);
}
@bindThis
@@ -85,6 +94,27 @@ export class ImageProcessingService {
};
}
+ @bindThis
+ public convertToWebpStream(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ return this.convertSharpToWebpStream(sharp(path), width, height, options);
+ }
+
+ @bindThis
+ public convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ const data = sharp
+ .resize(width, height, {
+ fit: 'inside',
+ withoutEnlargement: true,
+ })
+ .rotate()
+ .webp(options)
+
+ return {
+ data,
+ ext: 'webp',
+ type: 'image/webp',
+ };
+ }
/**
* Convert to PNG
* with resize, remove metadata, resolve orientation, stop animation