summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/FileInfoService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-12-04 15:03:09 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2022-12-04 15:03:09 +0900
commitbbb49457f9fb5d46402e913c92ebf77722cad6ff (patch)
tree8ef285bcbab2c3a4a89d0a624a802d76a2864fed /packages/backend/src/core/FileInfoService.ts
parent:art: (diff)
downloadsharkey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.tar.gz
sharkey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.tar.bz2
sharkey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.zip
refactor: introduce bindThis decorator to bind this automaticaly
Diffstat (limited to 'packages/backend/src/core/FileInfoService.ts')
-rw-r--r--packages/backend/src/core/FileInfoService.ts11
1 files changed, 11 insertions, 0 deletions
diff --git a/packages/backend/src/core/FileInfoService.ts b/packages/backend/src/core/FileInfoService.ts
index fd8a4fdd3a..bea1b3402e 100644
--- a/packages/backend/src/core/FileInfoService.ts
+++ b/packages/backend/src/core/FileInfoService.ts
@@ -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)