Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 159 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"dependencies": {
"cheerio": "^1.0.0",
"express": "^4.21.1",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.7"
},
"devDependencies": {
"@types/cheerio": "^0.22.35",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.13",
"@types/multer": "^1.4.12",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/processHtmlController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextFunction, Request, Response } from "express";
import { ProcessHtmlService } from "../services/processHtmlService";
import path from "path";
import { CssCode } from "../types";

const service = new ProcessHtmlService();
Expand All @@ -10,10 +9,11 @@ export async function processHtmlInputController(
res: Response,
next: NextFunction,
) {
const cssCode = req.body as CssCode;
const htmlCode = path.resolve(__dirname, path.join("../data/index.html"));
console.log(JSON.parse(req.body.data));
const cssCode = JSON.parse(req.body.data) as CssCode;
try {
const response = await service.extractData(htmlCode, cssCode);
const htmlContent = req.file!.buffer.toString();
const response = await service.extractData(htmlContent, cssCode);
res.json(response);
} catch (err) {
next(err);
Expand Down
17 changes: 17 additions & 0 deletions src/middlewares/validateFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const validateFile = (
req: Express.Request,
file: Express.Multer.File,
cb: Function,
) => {
const filetypes = /html|htm/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(
file.originalname.split(".").pop()!.toLowerCase(),
);

if (mimetype && extname) {
return cb(null, true);
} else {
return cb(new Error("Only HTML files are allowed!"), false);
}
};
11 changes: 10 additions & 1 deletion src/routes/processHtmlRouter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Router } from "express";
import { processHtmlInputController } from "../controllers/processHtmlController";
import { validateFile } from "../middlewares/validateFile";

import multer from "multer";

const r = Router();

r.post("/extract", processHtmlInputController);
const storage = multer.memoryStorage();
const upload = multer({
storage,
fileFilter: validateFile,
});

r.post("/extract", upload.single("htmlFile"), processHtmlInputController);

export default r;
10 changes: 7 additions & 3 deletions src/services/processHtmlService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import { ProcessHtmlService } from "../services/processHtmlService";
import {readFile} from "fs/promises";

describe("Process html api", () => {
test("Extract html: Example 1", async () => {
Expand All @@ -13,7 +14,8 @@ describe("Process html api", () => {
__dirname,
path.join("../data/index.html"),
);
const r = await service.extractData(htmlCode, cssCode);
const html = await readFile(htmlCode, "utf-8");
const r = await service.extractData(html, cssCode);
expect(r).toBeTruthy();
expect(r).toEqual({
title: "This is the title of the page",
Expand All @@ -34,7 +36,8 @@ describe("Process html api", () => {
__dirname,
path.join("../data/index.html"),
);
const r = await service.extractData(htmlCode, cssCode);
const html = await readFile(htmlCode, "utf-8");
const r = await service.extractData(html, cssCode);
expect(r).toBeTruthy();
expect(r).toEqual({
firstParagraph:
Expand Down Expand Up @@ -65,7 +68,8 @@ describe("Process html api", () => {
__dirname,
path.join("../data/index.html"),
);
const r = await service.extractData(htmlCode, cssCode);
const html = await readFile(htmlCode, "utf-8");
const r = await service.extractData(html, cssCode);
expect(r).toBeTruthy();
expect(r).toEqual({
title: "This is the title of the page",
Expand Down
Loading