refactor: introduce bindThis decorator to bind this automaticaly

This commit is contained in:
syuilo
2022-12-04 15:03:09 +09:00
parent e73581f715
commit bbb49457f9
199 changed files with 969 additions and 96 deletions

View File

@@ -14,6 +14,7 @@ import sharp from 'sharp';
import { encode } from 'blurhash';
import { createTempDir } from '@/misc/create-temp.js';
import { AiService } from '@/core/AiService.js';
import { bindThis } from '@/decorators.js';
const pipeline = util.promisify(stream.pipeline);
@@ -42,6 +43,7 @@ const TYPE_SVG = {
mime: 'image/svg+xml',
ext: 'svg',
};
@Injectable()
export class FileInfoService {
constructor(
@@ -52,6 +54,7 @@ export class FileInfoService {
/**
* Get file information
*/
@bindThis
public async getFileInfo(path: string, opts: {
skipSensitiveDetection: boolean;
sensitiveThreshold?: number;
@@ -135,6 +138,7 @@ export class FileInfoService {
};
}
@bindThis
private async detectSensitivity(source: string, mime: string, sensitiveThreshold: number, sensitiveThresholdForPorn: number, analyzeVideo: boolean): Promise<[sensitive: boolean, porn: boolean]> {
let sensitive = false;
let porn = false;
@@ -269,6 +273,7 @@ export class FileInfoService {
}
}
@bindThis
private exists(path: string): Promise<boolean> {
return fs.promises.access(path).then(() => true, () => false);
}
@@ -276,6 +281,7 @@ export class FileInfoService {
/**
* Detect MIME Type and extension
*/
@bindThis
public async detectType(path: string): Promise<{
mime: string;
ext: string | null;
@@ -312,6 +318,7 @@ export class FileInfoService {
/**
* Check the file is SVG or not
*/
@bindThis
public async checkSvg(path: string) {
try {
const size = await this.getFileSize(path);
@@ -325,6 +332,7 @@ export class FileInfoService {
/**
* Get file size
*/
@bindThis
public async getFileSize(path: string): Promise<number> {
const getStat = util.promisify(fs.stat);
return (await getStat(path)).size;
@@ -333,6 +341,7 @@ export class FileInfoService {
/**
* Calculate MD5 hash
*/
@bindThis
private async calcHash(path: string): Promise<string> {
const hash = crypto.createHash('md5').setEncoding('hex');
await pipeline(fs.createReadStream(path), hash);
@@ -342,6 +351,7 @@ export class FileInfoService {
/**
* Detect dimensions of image
*/
@bindThis
private async detectImageSize(path: string): Promise<{
width: number;
height: number;
@@ -358,6 +368,7 @@ export class FileInfoService {
/**
* Calculate average color of image
*/
@bindThis
private getBlurhash(path: string): Promise<string> {
return new Promise((resolve, reject) => {
sharp(path)