From 6a39d100a7b7ed56227443b38f3a06ef295c4722 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Mon, 21 Oct 2024 23:39:54 -0600 Subject: [PATCH] make the file handler a class and change the name. update the footer to include a query counter --- .gitignore | 3 ++- lib/filehandler.js | 52 +++++++++++++++++++++++++++++++++++++++ lib/loadfiles.js | 39 ----------------------------- server.js | 35 +++++++++++++++++--------- views/partials/footer.ejs | 4 +-- 5 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 lib/filehandler.js delete mode 100644 lib/loadfiles.js diff --git a/.gitignore b/.gitignore index 55f2396..9ef3d86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -filelist.json \ No newline at end of file +filelist.json +queries.txt \ No newline at end of file diff --git a/lib/filehandler.js b/lib/filehandler.js new file mode 100644 index 0000000..9fc2903 --- /dev/null +++ b/lib/filehandler.js @@ -0,0 +1,52 @@ +import { readFile, writeFile } from "fs/promises"; +import fs from "fs"; + +export default class FileHandler { + static async parseJsonFile(filePath) { + try { + let data = await JSON.parse(await readFile(filePath, "utf8")); + return data; + } catch (err) { + console.error(err); + } + } + + static async saveJsonFile(filePath, fileArr) { + let data = await JSON.stringify(fileArr); + await writeFile(filePath, data, (err) => { + if (err) { + console.error(err); + } else { + console.log(`Successfully saved file list to ${filePath}.`); + } + }); + } + + static fileExists(filePath) { + return fs.existsSync(filePath); + } + + static async fileTime(filePath) { + try { + return fs.statSync(filePath).mtimeMs; + } catch (err) { + console.error(err); + } + } + + static async readFile(filePath) { + try { + return await readFile(filePath, "utf8"); + } catch (err) { + console.error(err); + } + } + + static async writeFile(filePath, data) { + await writeFile(filePath, data, (err) => { + if (err) { + console.error(err); + } + }); + } +} diff --git a/lib/loadfiles.js b/lib/loadfiles.js deleted file mode 100644 index e0ae85e..0000000 --- a/lib/loadfiles.js +++ /dev/null @@ -1,39 +0,0 @@ -import { readFile, writeFile } from 'fs/promises'; -import fs from 'fs' - -export async function parseJsonFile(filePath) { - try{ - let data = await JSON.parse(await readFile(filePath, "utf8")); - return data - } - catch(err){ - console.error(err) - } -} - -export async function saveJsonFile(filePath, fileArr){ - let data = await JSON.stringify(fileArr) - await writeFile(filePath, data, err => { - if(err){ - console.error(err) - } - else{ - console.log(`Successfully saved file list to ${filePath}.`) - } - } - ) -} - -export async function fileExists(filePath){ - return fs.existsSync(filePath) -} - -export async function fileTime(filePath){ - try{ - return fs.statSync(filePath).mtimeMs - } - catch(err){ - console.error(err) - } -} - \ No newline at end of file diff --git a/server.js b/server.js index 811230d..345df06 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,5 @@ import getAllFiles from './lib/dircrawl.js' -import {parseJsonFile, saveJsonFile, fileExists, fileTime} from './lib/loadfiles.js' +import FileHandler from './lib/filehandler.js' import Searcher from './lib/search.js' import cron from 'node-cron' import FileOlderThan from 'file-older-than' @@ -8,11 +8,19 @@ import express from 'express' import http from 'http' import sanitize from 'sanitize' + let fileListPath = './filelist.json' +let queryCountFile = './queries.txt' let categoryListPath = './lib/categories.json' -let categoryList = await parseJsonFile(categoryListPath) -//TO DO: add if exist to suppress an error -let crawlTime = await fileTime(fileListPath) +let categoryList = await FileHandler.parseJsonFile(categoryListPath) +let crawlTime = 0 +let queryCount = 0 +if(FileHandler.fileExists(fileListPath)){ + crawlTime = await FileHandler.fileTime(fileListPath) +} +if(FileHandler.fileExists(queryCountFile)){ + queryCount = parseInt(await FileHandler.readFile(queryCountFile)) +} let searchFields = ['filename', 'category', 'type', 'region'] @@ -42,19 +50,19 @@ let search //cheat so we can check before assignment async function getFilesJob(){ console.log('Updating the file list.') fileList = await getAllFiles(categoryList) - saveJsonFile(fileListPath, fileList) + await FileHandler.saveJsonFile(fileListPath, fileList) if(typeof search !== 'undefined'){ search.createIndex(fileList, searchFields) //recreate the search index } - crawlTime = await fileTime(fileListPath) + crawlTime = await FileHandler.fileTime(fileListPath) console.log(`Finished updating file list. ${fileList.length} found.`) } -if(process.env.FORCE_FILE_REBUILD == "1" || !fileExists(fileListPath) || FileOlderThan(fileListPath, '1w')){ +if(process.env.FORCE_FILE_REBUILD == "1" || !FileHandler.fileExists(fileListPath) || FileOlderThan(fileListPath, '1w')){ await getFilesJob() } else{ - fileList = await parseJsonFile(fileListPath) + fileList = await FileHandler.parseJsonFile(fileListPath) } search = new Searcher(fileList, searchFields) @@ -67,7 +75,8 @@ app.set('view engine', 'ejs') app.get('/', function(req, res) { res.render('pages/index', { page: 'search', - crawlTime: crawlTime + crawlTime: crawlTime, + queryCount: queryCount.toLocaleString() }) }) @@ -86,8 +95,11 @@ app.get('/search', async function(req, res) { query: query, results: results, crawlTime: crawlTime, - indexing: search.indexing + indexing: search.indexing, + queryCount: queryCount.toLocaleString() }) + queryCount += 1 + FileHandler.writeFile(queryCountFile, String(queryCount)) }) app.get("/lucky", async function(req, res) { @@ -109,7 +121,8 @@ app.get("/settings", function(req, res) { res.render('pages/index', { page: 'settings', crawlTime: crawlTime, - defaultSettings: defaultSettings + defaultSettings: defaultSettings, + queryCount: queryCount.toLocaleString() }) }) diff --git a/views/partials/footer.ejs b/views/partials/footer.ejs index 72a9a04..30757df 100644 --- a/views/partials/footer.ejs +++ b/views/partials/footer.ejs @@ -1,7 +1,7 @@
- +
\ No newline at end of file