Skip to content

Commit

Permalink
✔ Remove Support XML ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Nov 28, 2023
1 parent b004807 commit b89648a
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 128 deletions.
4 changes: 2 additions & 2 deletions src/api/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }
);

Expand Down
22 changes: 4 additions & 18 deletions src/api/controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
});
}
}

Expand Down
16 changes: 3 additions & 13 deletions src/api/controllers/crawl.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
});
}
}

Expand Down
22 changes: 4 additions & 18 deletions src/api/controllers/ddl-file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}

Expand Down Expand Up @@ -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);
}
});
}
}

Expand Down
30 changes: 5 additions & 25 deletions src/api/controllers/torrent.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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} 🤣`,
Expand All @@ -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);
}
});
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/api/filters/http-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
//
Expand All @@ -22,7 +20,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
const res = ctx.getResponse<Response>();

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();
Expand All @@ -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);
}

Expand Down
5 changes: 0 additions & 5 deletions src/api/interceptors/req-res.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand Down
10 changes: 1 addition & 9 deletions src/api/middlewares/cache.middleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand Down
21 changes: 0 additions & 21 deletions src/api/services/global.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '</' + prop + '>';
}
} else if (typeof obj[prop] === 'object') {
xml += this.OBJ2XML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : '</' + prop + '>';
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return `<root>${xml}</root>`;
}

htmlToText(htmlElementString: string): string {
if (htmlElementString) {
const stringText = stripHtml(htmlElementString);
Expand Down

0 comments on commit b89648a

Please sign in to comment.