From ad731ebc29c12b597fbb59739c2b3f45a03b4159 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Mon, 6 Jun 2022 13:54:29 +0200 Subject: [PATCH] feat(compare-files): initial commit --- compare-files/.gitignore | 1 + compare-files/README.md | 21 ++++++++++++++++++++ compare-files/README.md.license | 5 +++++ compare-files/files.js | 34 +++++++++++++++++++++++++++++++++ compare-files/index.js | 16 ++++++++++++++++ compare-files/package.json | 21 ++++++++++++++++++++ 6 files changed, 98 insertions(+) create mode 100644 compare-files/.gitignore create mode 100644 compare-files/README.md create mode 100644 compare-files/README.md.license create mode 100644 compare-files/files.js create mode 100644 compare-files/index.js create mode 100644 compare-files/package.json diff --git a/compare-files/.gitignore b/compare-files/.gitignore new file mode 100644 index 0000000..81a38be --- /dev/null +++ b/compare-files/.gitignore @@ -0,0 +1 @@ +.env* diff --git a/compare-files/README.md b/compare-files/README.md new file mode 100644 index 0000000..05edc4f --- /dev/null +++ b/compare-files/README.md @@ -0,0 +1,21 @@ +## Overview +Compare files + +## Preparation + +Run the following command in your favorite terminal to install dependenies. +``` +npm i +``` + +## Development setup + +Run the following command in your favorite terminal if you fancy log messages for debugging. +``` +export DEBUG=$DEBUG,debug +``` + +Run the following command in your favorite terminal to start the service in development mode. +``` +npm run dev +``` diff --git a/compare-files/README.md.license b/compare-files/README.md.license new file mode 100644 index 0000000..2e76dc1 --- /dev/null +++ b/compare-files/README.md.license @@ -0,0 +1,5 @@ +/* + * SPDX-FileCopyrightText: 2022 Stefan Begerad + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ diff --git a/compare-files/files.js b/compare-files/files.js new file mode 100644 index 0000000..8f00160 --- /dev/null +++ b/compare-files/files.js @@ -0,0 +1,34 @@ +const debug=require('debug')('files'); +const fs = require('fs'); +const fsp = fs.promises; + +//resolves to true or false +async function compare(fname1, fname2) { + debug('fname1: '+fname1); + let h1, h2; + try { + h1 = await fsp.open(fname1); + h2 = await fsp.open(fname2); + const [stat1, stat2] = await Promise.all([h1.stat(), h2.stat()]); + if (stat1.size !== stat2.size) { + debug('file size NOT equal'); + return false; + }else{ + debug('file size equal'); + return true; + } + } finally { + if (h1) { + debug('h1 to be closed'); + await h1.close(); + } + if (h2) { + debug('h2 to be closed'); + await h2.close(); + } + } +} + +module.exports={ + compare +} diff --git a/compare-files/index.js b/compare-files/index.js new file mode 100644 index 0000000..cf592ab --- /dev/null +++ b/compare-files/index.js @@ -0,0 +1,16 @@ +const debug=require('debug')('debug'); +const fs = require('fs'); +const files=require('./files'); +const path='/home/begerad/git/sib/sandbox-node/request'; +const filePathAbsOne=path+'/connect-top-dhid-1654510162870.zip'; +debug('filePathAbsOne: '+filePathAbsOne); +const filePathAbsTwo=path+'/connect-top-dhid-1654514173016.zip'; +debug('filePathAbsTwo: '+filePathAbsTwo); + +//sample usage +files.compare(filePathAbsOne,filePathAbsTwo).then(result => { + debug('result: '+result); +}).catch(err => { + console.log(err); +}); +debug('done.'); diff --git a/compare-files/package.json b/compare-files/package.json new file mode 100644 index 0000000..371ef88 --- /dev/null +++ b/compare-files/package.json @@ -0,0 +1,21 @@ +{ + "private": true, + "name": "compare-files", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "dev": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Stefan Begerad", + "license": "GPL-3.0", + "devDependencies": { + "nodemon": "^2.0.7" + }, + "dependencies": { + "fs": "0.0.1-security", + "http": "0.0.1-security", + "request": "^2.88.2" + } +}