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

@ -71,6 +71,7 @@ type UploadFromUrlArgs = {
requestIp?: string | null;
requestHeaders?: Record<string, string> | null;
};
import { bindThis } from '@/decorators.js';
@Injectable()
export class DriveService {
@ -122,6 +123,7 @@ export class DriveService {
* @param hash Hash for original
* @param size Size for original
*/
@bindThis
private async save(file: DriveFile, path: string, name: string, type: string, hash: string, size: number): Promise<DriveFile> {
// thunbnail, webpublic を必要なら生成
const alts = await this.generateAlts(path, type, !file.uri);
@ -242,6 +244,7 @@ export class DriveService {
* @param type Content-Type for original
* @param generateWeb Generate webpublic or not
*/
@bindThis
public async generateAlts(path: string, type: string, generateWeb: boolean) {
if (type.startsWith('video/')) {
try {
@ -345,6 +348,7 @@ export class DriveService {
/**
* Upload to ObjectStorage
*/
@bindThis
private async upload(key: string, stream: fs.ReadStream | Buffer, type: string, filename?: string) {
if (type === 'image/apng') type = 'image/png';
if (!FILE_TYPE_BROWSERSAFE.includes(type)) type = 'application/octet-stream';
@ -372,6 +376,7 @@ export class DriveService {
if (result) this.registerLogger.debug(`Uploaded: ${result.Bucket}/${result.Key} => ${result.Location}`);
}
@bindThis
private async deleteOldFile(user: IRemoteUser) {
const q = this.driveFilesRepository.createQueryBuilder('file')
.where('file.userId = :userId', { userId: user.id })
@ -398,6 +403,7 @@ export class DriveService {
* Add file to drive
*
*/
@bindThis
public async addFile({
user,
path,
@ -601,6 +607,7 @@ export class DriveService {
return file;
}
@bindThis
public async deleteFile(file: DriveFile, isExpired = false) {
if (file.storedInternal) {
this.internalStorageService.del(file.accessKey!);
@ -627,6 +634,7 @@ export class DriveService {
this.deletePostProcess(file, isExpired);
}
@bindThis
public async deleteFileSync(file: DriveFile, isExpired = false) {
if (file.storedInternal) {
this.internalStorageService.del(file.accessKey!);
@ -657,6 +665,7 @@ export class DriveService {
this.deletePostProcess(file, isExpired);
}
@bindThis
private async deletePostProcess(file: DriveFile, isExpired = false) {
// リモートファイル期限切れ削除後は直リンクにする
if (isExpired && file.userHost !== null && file.uri != null) {
@ -683,6 +692,7 @@ export class DriveService {
}
}
@bindThis
public async deleteObjectStorageFile(key: string) {
const meta = await this.metaService.fetch();
@ -694,6 +704,7 @@ export class DriveService {
}).promise();
}
@bindThis
public async uploadFromUrl({
url,
user,