From dbff436bc25aca1bdeed09ddc28b9605ddf50112 Mon Sep 17 00:00:00 2001 From: rishabjasrotia Date: Thu, 9 Nov 2023 17:53:34 +0530 Subject: [PATCH 1/5] #146 - Fix issue of build & Add local File upload support --- .gitignore | 3 +- Makefile | 6 +++ apps/OpenSign/Dockerfile | 2 +- apps/OpenSign/public/mfbuild/.gitkeep | 0 .../cloud/customRoute/uploadFile.js | 54 ++++++++++++++----- 5 files changed, 51 insertions(+), 14 deletions(-) delete mode 100644 apps/OpenSign/public/mfbuild/.gitkeep diff --git a/.gitignore b/.gitignore index 8e62b9ce7..b6dcebdf3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ npm-debug.log* yarn-debug.log* yarn-error.log* apps/OpenSign/public/mfbuild/* -microfrontends/SignDocuments/build/* \ No newline at end of file +microfrontends/SignDocuments/build/* +apps/OpenSignServer/files/files/* \ No newline at end of file diff --git a/Makefile b/Makefile index 35fa55901..7cf392883 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,9 @@ build: + cp .env.local_dev .env + rm -rf apps/OpenSign/public/mfbuild + cd microfrontends/SignDocuments && npm install && npm run build + docker compose up -d + +run: cp .env.local_dev .env docker compose up -d \ No newline at end of file diff --git a/apps/OpenSign/Dockerfile b/apps/OpenSign/Dockerfile index 7d8542bf6..57c815846 100644 --- a/apps/OpenSign/Dockerfile +++ b/apps/OpenSign/Dockerfile @@ -21,5 +21,5 @@ EXPOSE 3000 # ENV NODE_ENV production # Run the application -CMD ["npm", "start"] +ENTRYPOINT npm run start-dev diff --git a/apps/OpenSign/public/mfbuild/.gitkeep b/apps/OpenSign/public/mfbuild/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/apps/OpenSignServer/cloud/customRoute/uploadFile.js b/apps/OpenSignServer/cloud/customRoute/uploadFile.js index 34abc14d0..6a87c386d 100644 --- a/apps/OpenSignServer/cloud/customRoute/uploadFile.js +++ b/apps/OpenSignServer/cloud/customRoute/uploadFile.js @@ -50,16 +50,28 @@ async function uploadFile(req, res) { region: process.env.DO_REGION, }); - // const s3 = new aws.S3(); - const upload = multer({ - fileFilter: function (req, file, cb) { - if (accepted_extensions.some(ext => file.originalname.toLowerCase().endsWith('.' + ext))) { - return cb(null, true); + const parseBaseUrl = process.env.REACT_APP_SERVERURL; + const parseAppId = process.env.APP_ID; + + if (process.env.USE_LOCAL == "TRUE") { + var fileStorage = multer.diskStorage({ + destination: function(req, file, cb) { + cb(null, "files/files"); + }, + metadata: function (req, file, cb) { + cb(null, { fieldName: 'OPENSIGN_METADATA' }); + }, + filename: function(req, file, cb) { + let filename = file.originalname; + let filenam = filename.split('.')[0]; + let extension = filename.split('.')[1]; + filenam = filenam + '_' + new Date().toISOString() + '.' + extension; + console.log(filenam); + cb(null, filenam); } - // otherwise, return error - return cb('Only ' + accepted_extensions.join(', ') + ' files are allowed!'); - }, - storage: multerS3({ + }); + } else { + var fileStorage = multerS3({ acl: 'public-read', s3, bucket: DO_SPACE, @@ -74,9 +86,20 @@ async function uploadFile(req, res) { filenam = filenam + '_' + new Date().toISOString() + '.' + extension; console.log(filenam); cb(null, filenam); - }, - }), + } + }); + } + // const s3 = new aws.S3(); + const upload = multer({ + fileFilter: function (req, file, cb) { + if (accepted_extensions.some(ext => file.originalname.toLowerCase().endsWith('.' + ext))) { + return cb(null, true); + } + // otherwise, return error + return cb('Only ' + accepted_extensions.join(', ') + ' files are allowed!'); + }, + storage: fileStorage, limits: { fileSize: size }, }).single('file'); @@ -93,7 +116,14 @@ async function uploadFile(req, res) { const status = 'Success'; //res.header("Access-Control-Allow-Headers", "Content-Type"); //res.setHeader("Access-Control-Allow-Origin", "*"); - return res.json({ status, imageUrl: req.file.location }); + if (process.env.USE_LOCAL == "TRUE") { + console.log(req.file); + var fileUrl = `${parseBaseUrl}/files/${parseAppId}/${req.file.filename}`; + } else { + var fileUrl = req.file.location; + } + + return res.json({ status, imageUrl: fileUrl }); }); } catch (err) { console.log('Exeption in query ' + err.stack); From 3b93f9647990d351a54498a5d4b5605cae529706 Mon Sep 17 00:00:00 2001 From: rishabjasrotia Date: Thu, 9 Nov 2023 18:05:34 +0530 Subject: [PATCH 2/5] Fix makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7cf392883..bf7f219d3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ build: cp .env.local_dev .env rm -rf apps/OpenSign/public/mfbuild - cd microfrontends/SignDocuments && npm install && npm run build + cd microfrontends/SignDocuments && npm install && npm run build docker compose up -d run: From ade39c2f46df61c107b545aa908150c1007a11f9 Mon Sep 17 00:00:00 2001 From: rishabjasrotia Date: Thu, 9 Nov 2023 18:36:18 +0530 Subject: [PATCH 3/5] Doc Updated --- INSTALLATION.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/INSTALLATION.md b/INSTALLATION.md index 6cebe94b8..86a9fb8e6 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -143,5 +143,8 @@ In order to address this, your document storage system must be instructed to acc # Build Local Environment -Below are the steps to follow - +Command to build project - - Execute `make build` + +Command to run project - +- Execute `make run` \ No newline at end of file From 405557478577ce2acd9a43d50d611a4985b9b2ff Mon Sep 17 00:00:00 2001 From: prafull-opensignlabs <93375423+prafull-opensignlabs@users.noreply.github.com> Date: Thu, 9 Nov 2023 20:48:53 +0530 Subject: [PATCH 4/5] sanitize File Name in upload file --- apps/OpenSignServer/cloud/customRoute/uploadFile.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/OpenSignServer/cloud/customRoute/uploadFile.js b/apps/OpenSignServer/cloud/customRoute/uploadFile.js index 6a87c386d..bb1a84a8f 100644 --- a/apps/OpenSignServer/cloud/customRoute/uploadFile.js +++ b/apps/OpenSignServer/cloud/customRoute/uploadFile.js @@ -4,6 +4,12 @@ import multerS3 from 'multer-s3'; import aws from 'aws-sdk'; import dotenv from 'dotenv'; dotenv.config(); + +function sanitizeFileName(fileName) { + // Remove spaces and invalid characters + return fileName.replace(/[^a-zA-Z0-9._-]/g, ''); +} + async function uploadFile(req, res) { try { //--size extended to 100 mb @@ -50,7 +56,7 @@ async function uploadFile(req, res) { region: process.env.DO_REGION, }); - const parseBaseUrl = process.env.REACT_APP_SERVERURL; + const parseBaseUrl = process.env.SERVER_URL; const parseAppId = process.env.APP_ID; if (process.env.USE_LOCAL == "TRUE") { @@ -65,7 +71,7 @@ async function uploadFile(req, res) { let filename = file.originalname; let filenam = filename.split('.')[0]; let extension = filename.split('.')[1]; - filenam = filenam + '_' + new Date().toISOString() + '.' + extension; + filenam = sanitizeFileName(filenam + '_' + new Date().toISOString() + '.' + extension) console.log(filenam); cb(null, filenam); } @@ -83,7 +89,7 @@ async function uploadFile(req, res) { let filename = file.originalname; let filenam = filename.split('.')[0]; let extension = filename.split('.')[1]; - filenam = filenam + '_' + new Date().toISOString() + '.' + extension; + filenam = sanitizeFileName(filenam + '_' + new Date().toISOString() + '.' + extension) console.log(filenam); cb(null, filenam); } From a92188d8e2212f311ed1f018418f8ab18b25aa31 Mon Sep 17 00:00:00 2001 From: prafull-opensignlabs <93375423+prafull-opensignlabs@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:18:36 +0530 Subject: [PATCH 5/5] change variable name --- .../cloud/customRoute/uploadFile.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/OpenSignServer/cloud/customRoute/uploadFile.js b/apps/OpenSignServer/cloud/customRoute/uploadFile.js index bb1a84a8f..b0152e1bd 100644 --- a/apps/OpenSignServer/cloud/customRoute/uploadFile.js +++ b/apps/OpenSignServer/cloud/customRoute/uploadFile.js @@ -69,11 +69,11 @@ async function uploadFile(req, res) { }, filename: function(req, file, cb) { let filename = file.originalname; - let filenam = filename.split('.')[0]; + let newFileName = filename.split('.')[0]; let extension = filename.split('.')[1]; - filenam = sanitizeFileName(filenam + '_' + new Date().toISOString() + '.' + extension) - console.log(filenam); - cb(null, filenam); + newFileName = sanitizeFileName(newFileName + '_' + new Date().toISOString() + '.' + extension) + console.log(newFileName); + cb(null, newFileName); } }); } else { @@ -87,11 +87,11 @@ async function uploadFile(req, res) { key: function (req, file, cb) { //console.log(file); let filename = file.originalname; - let filenam = filename.split('.')[0]; + let newFileName = filename.split('.')[0]; let extension = filename.split('.')[1]; - filenam = sanitizeFileName(filenam + '_' + new Date().toISOString() + '.' + extension) - console.log(filenam); - cb(null, filenam); + newFileName = sanitizeFileName(newFileName + '_' + new Date().toISOString() + '.' + extension) + console.log(newFileName); + cb(null, newFileName); } }); }