From b89648ae484624f83785a7191f2e24cdce8f145c Mon Sep 17 00:00:00 2001 From: Bifeldy Date: Tue, 28 Nov 2023 19:17:41 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=94=20Remove=20Support=20XML=20~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/app.module.ts | 4 +-- src/api/controllers/attachment.controller.ts | 22 +++----------- src/api/controllers/crawl.controller.ts | 16 ++-------- src/api/controllers/ddl-file.controller.ts | 22 +++----------- src/api/controllers/torrent.controller.ts | 30 ++++--------------- src/api/filters/http-exception.filter.ts | 11 +------ src/api/interceptors/req-res.interceptor.ts | 5 ---- src/api/middlewares/cache.middleware.ts | 10 +------ ...l.middleware.ts => url-path.middleware.ts} | 9 ++---- src/api/services/global.service.ts | 21 ------------- 10 files changed, 22 insertions(+), 128 deletions(-) rename src/api/middlewares/{url-xml.middleware.ts => url-path.middleware.ts} (67%) diff --git a/src/api/app.module.ts b/src/api/app.module.ts index b67c92dd4..38009bbe1 100644 --- a/src/api/app.module.ts +++ b/src/api/app.module.ts @@ -79,7 +79,7 @@ import { UserController } from './controllers/user.controller'; import { VerifyController } from './controllers/verify.controller'; import { VerifySosmedController } from './controllers/verify-/verify-sosmed.controller'; -import { UrlXmlMiddleware } from './middlewares/url-xml.middleware'; +import { UrlPathMiddleware } from './middlewares/url-path.middleware'; import { ApiKeyMiddleware } from './middlewares/api-key.middleware'; import { BannedMiddleware } from './middlewares/banned.middleware'; import { LoginMiddleware } from './middlewares/login.middleware'; @@ -320,7 +320,7 @@ export class AppModule { // https://stackoverflow.com/questions/61152975/nestjs-middleware-for-all-routes-except-auth configure(mc: MiddlewareConsumer) { - mc.apply(UrlXmlMiddleware).forRoutes( + mc.apply(UrlPathMiddleware).forRoutes( { path: '*', method: RequestMethod.ALL } ); diff --git a/src/api/controllers/attachment.controller.ts b/src/api/controllers/attachment.controller.ts index bbffd7eab..5db832776 100644 --- a/src/api/controllers/attachment.controller.ts +++ b/src/api/controllers/attachment.controller.ts @@ -210,19 +210,12 @@ export class AttachmentController { delete ddlFile.user_.updated_at; } } - const body: any = { + res.status(HttpStatus.OK).json({ info: `😅 200 - Attachment API :: DDL List 🤣`, results: ddlFiles, count, pages: 1 - }; - res.status(HttpStatus.OK); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } else if (attachment.google_drive) { const gdrive = await this.gdrive.gDrive(); const dfile = await gdrive.files.get( @@ -269,19 +262,12 @@ export class AttachmentController { throw new Error('Lampiran Tidak Ditemukan!'); } } catch (error) { - const body: any = { + res.status(HttpStatus.NOT_FOUND).json({ info: `🙄 404 - Attachment API :: Gagal Mencari Lampiran ${req.params['id']} 😪`, result: { message: 'Lampiran Tidak Ditemukan!' } - }; - res.status(HttpStatus.NOT_FOUND); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } } diff --git a/src/api/controllers/crawl.controller.ts b/src/api/controllers/crawl.controller.ts index 94c5468c6..842572391 100644 --- a/src/api/controllers/crawl.controller.ts +++ b/src/api/controllers/crawl.controller.ts @@ -14,8 +14,6 @@ import { environment } from '../../environments/api/environment'; import { FilterApiKeyAccess } from '../decorators/filter-api-key-access.decorator'; -import { GlobalService } from '../services/global.service'; - @ApiExcludeController() @Controller('/crawl') export class CrawlController { @@ -44,8 +42,7 @@ export class CrawlController { browser: Browser = null; constructor( - @Inject(CACHE_MANAGER) private cm: Cache, - private gs: GlobalService + @Inject(CACHE_MANAGER) private cm: Cache ) { puppeteer.use(StealthPlugin()); this.initialize(); @@ -126,19 +123,12 @@ export class CrawlController { if (page) { await page.close(); } - const body = { + res.status(HttpStatus.BAD_REQUEST).json({ info: '🙄 400 - Crawl API :: UR[I/L] Tidak Valid 😪', result: { message: 'Data Tidak Lengkap!' } - }; - res.status(HttpStatus.BAD_REQUEST); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } } diff --git a/src/api/controllers/ddl-file.controller.ts b/src/api/controllers/ddl-file.controller.ts index 2b42f4d89..c18d160e9 100644 --- a/src/api/controllers/ddl-file.controller.ts +++ b/src/api/controllers/ddl-file.controller.ts @@ -85,19 +85,12 @@ export class DdlPartController { `, [ddlFile.msg_parent, ddlFile.msg_parent]); }).pipe(res); } catch (error) { - const body: any = { + res.status(HttpStatus.NOT_FOUND).json({ info: `🙄 404 - DDL File API :: Gagal Mencari Lampiran ${req.params['id']} 😪`, result: { message: 'Lampiran Tidak Ditemukan!' } - }; - res.status(HttpStatus.NOT_FOUND); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } } @@ -216,19 +209,12 @@ export class DdlSeekController { `, [ddlFile.msg_parent, ddlFile.msg_parent]); }).pipe(res); } catch (error) { - const body: any = { + res.status(HttpStatus.NOT_FOUND).json({ info: `🙄 404 - DDL File API :: Gagal Mencari Lampiran ${req.params['id']} 😪`, result: { message: 'Lampiran Tidak Ditemukan!' } - }; - res.status(HttpStatus.NOT_FOUND); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } } diff --git a/src/api/controllers/torrent.controller.ts b/src/api/controllers/torrent.controller.ts index e6581fffb..9c644cd8e 100644 --- a/src/api/controllers/torrent.controller.ts +++ b/src/api/controllers/torrent.controller.ts @@ -9,17 +9,11 @@ import { environment } from '../../environments/api/environment'; import { FilterApiKeyAccess } from '../decorators/filter-api-key-access.decorator'; -import { GlobalService } from '../services/global.service'; - @ApiExcludeController() @Controller('/torrent') export class TorrentController { - constructor( - private gs: GlobalService - ) { - // - } + constructor() { } @Post('/') @HttpCode(201) @@ -32,19 +26,12 @@ export class TorrentController { timeout: req.body.trackTimeout || 1234 }, (err, data) => { if (err) { - const body: any = { + res.status(HttpStatus.BAD_REQUEST).json({ info: `🙄 400 - Torrent Tracker API :: Gagal Mendapatkan Torrent 😪`, result: { message: err.message } - }; - res.status(HttpStatus.BAD_REQUEST); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } else { res.status(HttpStatus.CREATED).json({ info: `😅 200 - Torrent Tracker API :: Berhasil Mendapatkan ${req.body.magnetHash} 🤣`, @@ -56,19 +43,12 @@ export class TorrentController { throw new Error('Data Tidak Lengkap!'); } } catch (error) { - const body: any = { + res.status(HttpStatus.BAD_REQUEST).json({ info: '🙄 400 - Torrent Tracker API :: Gagal Mendapatkan Torrent 😪', result: { message: 'Data Tidak Lengkap!' } - }; - res.status(HttpStatus.BAD_REQUEST); - if (res.locals['xml']) { - res.set('Content-Type', 'application/xml'); - res.send(this.gs.OBJ2XML(body)); - } else { - res.json(body); - } + }); } } diff --git a/src/api/filters/http-exception.filter.ts b/src/api/filters/http-exception.filter.ts index 137dddaec..4ca3f4741 100644 --- a/src/api/filters/http-exception.filter.ts +++ b/src/api/filters/http-exception.filter.ts @@ -3,14 +3,12 @@ import { Request, Response } from 'express'; import { environment } from '../../environments/api/environment'; -import { GlobalService } from '../services/global.service'; import { SocketIoService } from '../services/socket-io.service'; @Catch(HttpException) export class HttpExceptionFilter implements ExceptionFilter { constructor( - private gs: GlobalService, private sis: SocketIoService ) { // @@ -22,7 +20,7 @@ export class HttpExceptionFilter implements ExceptionFilter { const res = ctx.getResponse(); const statusCode = exception.getStatus(); - let body: any = exception.getResponse(); + const body: any = exception.getResponse(); if (statusCode === HttpStatus.UNAUTHORIZED) { const socketId = (req.headers['x-socket-id'] || '').toString(); @@ -41,13 +39,6 @@ export class HttpExceptionFilter implements ExceptionFilter { }); } - this.gs.log(`[HTTP_EXCEPTION-RESPONSE_HEADER_${statusCode}] 🏹`, res.getHeaders()); - if (res.locals['xml']) { - this.gs.log('[HTTP_EXCEPTION-RESPONSE_BODY_JSON_2_XML] 🏹', body); - res.set('Content-Type', 'application/xml'); - body = this.gs.OBJ2XML(body); - } - this.gs.log(`[HTTP_EXCEPTION-RESPONSE_BODY_${statusCode}] 🏹`, body); res.status(statusCode).send(body); } diff --git a/src/api/interceptors/req-res.interceptor.ts b/src/api/interceptors/req-res.interceptor.ts index 1fe3ecebe..63dba7693 100644 --- a/src/api/interceptors/req-res.interceptor.ts +++ b/src/api/interceptors/req-res.interceptor.ts @@ -68,11 +68,6 @@ export class ReqResInterceptor implements NestInterceptor { return next.handle().pipe( map(body => { this.gs.log(`[REQ_RES_INTERCEPTOR-RESPONSE_HEADER_${res.statusCode}] 🏹`, res.getHeaders()); - if (res.locals['xml']) { - this.gs.log('[REQ_RES_INTERCEPTOR-RESPONSE_BODY_JSON_2_XML] 🏹', req.body); - res.set('Content-Type', 'application/xml'); - body = this.gs.OBJ2XML(body); - } this.gs.log(`[REQ_RES_INTERCEPTOR-RESPONSE_BODY_${res.statusCode}] 🏹`, body); return body; }) diff --git a/src/api/middlewares/cache.middleware.ts b/src/api/middlewares/cache.middleware.ts index f020e981a..d2b77bf3e 100644 --- a/src/api/middlewares/cache.middleware.ts +++ b/src/api/middlewares/cache.middleware.ts @@ -1,6 +1,3 @@ -// NodeJS Library -import { Buffer } from 'node:buffer'; - import { CACHE_MANAGER, Inject, Injectable, NestMiddleware, Next, Req, Res } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; import { Cache } from 'cache-manager'; @@ -23,12 +20,7 @@ export class CacheMiddleware implements NestMiddleware { const cacheData: ResponseCache = await this.cm.get(req.originalUrl); if (cacheData) { this.gs.log(`[CACHE_MIDDLEWARE-${req.originalUrl}] ✨`, cacheData); - let body = cacheData.body; - if (res.locals['xml'] && !Buffer.isBuffer(body)) { - res.set('Content-Type', 'application/xml'); - body = this.gs.OBJ2XML(cacheData.body); - } - res.status(cacheData.status).send(body); + res.status(cacheData.status).send(cacheData.body); } else { return next(); } diff --git a/src/api/middlewares/url-xml.middleware.ts b/src/api/middlewares/url-path.middleware.ts similarity index 67% rename from src/api/middlewares/url-xml.middleware.ts rename to src/api/middlewares/url-path.middleware.ts index a799f7a69..0dede56a5 100644 --- a/src/api/middlewares/url-xml.middleware.ts +++ b/src/api/middlewares/url-path.middleware.ts @@ -4,7 +4,7 @@ import { Request, Response, NextFunction } from 'express'; import { GlobalService } from '../services/global.service'; @Injectable() -export class UrlXmlMiddleware implements NestMiddleware { +export class UrlPathMiddleware implements NestMiddleware { constructor( private gs: GlobalService @@ -18,14 +18,9 @@ export class UrlXmlMiddleware implements NestMiddleware { req.originalUrl = urlPath[0]; } else { let urlQuery = urlPath[1].split('&').sort(); - if (req.query['xml'] === 'true') { - res.locals['xml'] = true; - delete req.query['xml']; - urlQuery = urlQuery.filter(x => !x.includes('xml=true')); - } req.originalUrl = urlPath[0] + '?' + urlQuery.join('&'); } - this.gs.log(`[XML_MIDDLEWARE-${req.query['xml']}] ⚡`, req.originalUrl); + this.gs.log(`[URL_PATH_MIDDLEWARE] ⚡`, req.originalUrl); return next(); } diff --git a/src/api/services/global.service.ts b/src/api/services/global.service.ts index 968bb4b89..65598c4fb 100644 --- a/src/api/services/global.service.ts +++ b/src/api/services/global.service.ts @@ -45,27 +45,6 @@ export class GlobalService { } } - OBJ2XML(obj: object): string { - var xml = ''; - for (var prop in obj) { - xml += obj[prop] instanceof Array ? '' : '<' + prop + '>'; - if (obj[prop] instanceof Array) { - for (var array in obj[prop]) { - xml += '<' + prop + '>'; - xml += this.OBJ2XML(new Object(obj[prop][array])); - xml += ''; - } - } else if (typeof obj[prop] === 'object') { - xml += this.OBJ2XML(new Object(obj[prop])); - } else { - xml += obj[prop]; - } - xml += obj[prop] instanceof Array ? '' : ''; - } - var xml = xml.replace(/<\/?[0-9]{1,}>/g, ''); - return `${xml}`; - } - htmlToText(htmlElementString: string): string { if (htmlElementString) { const stringText = stripHtml(htmlElementString);