@gov-cy/govcy-express-services 1.0.0-alpha.4 → 1.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -2
- package/src/index.mjs +9 -1
- package/src/middleware/cyLoginAuth.mjs +8 -0
- package/src/middleware/govcyCsrf.mjs +15 -1
- package/src/middleware/govcyHttpErrorHandler.mjs +4 -3
- package/src/middleware/govcyUpload.mjs +36 -0
- package/src/public/js/govcyFiles.js +46 -0
- package/src/resources/govcyResources.mjs +3 -3
- package/src/utils/govcyApiDetection.mjs +17 -0
- package/src/utils/govcyApiRequest.mjs +19 -2
- package/src/utils/govcyApiResponse.mjs +31 -0
- package/src/utils/govcyConstants.mjs +5 -1
- package/src/utils/govcyDataLayer.mjs +14 -0
- package/src/utils/govcyExpressions.mjs +1 -1
- package/src/utils/govcyHandleFiles.mjs +277 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gov-cy/govcy-express-services",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.6",
|
|
4
4
|
"description": "An Express-based system that dynamically renders services using @gov-cy/govcy-frontend-renderer and posts data to a submission API.",
|
|
5
5
|
"author": "DMRID - DSF Team",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,12 +48,14 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@gov-cy/dsf-email-templates": "^2.1.0",
|
|
51
|
-
"@gov-cy/govcy-frontend-renderer": "^1.
|
|
51
|
+
"@gov-cy/govcy-frontend-renderer": "^1.20.0",
|
|
52
52
|
"axios": "^1.9.0",
|
|
53
53
|
"cookie-parser": "^1.4.7",
|
|
54
54
|
"dotenv": "^16.3.1",
|
|
55
55
|
"express": "^4.18.2",
|
|
56
56
|
"express-session": "^1.17.3",
|
|
57
|
+
"form-data": "^4.0.4",
|
|
58
|
+
"multer": "^2.0.2",
|
|
57
59
|
"openid-client": "^6.3.4",
|
|
58
60
|
"puppeteer": "^24.6.0"
|
|
59
61
|
},
|
package/src/index.mjs
CHANGED
|
@@ -24,6 +24,7 @@ import { govcyManifestHandler } from './middleware/govcyManifestHandler.mjs';
|
|
|
24
24
|
import { govcyRoutePageHandler } from './middleware/govcyRoutePageHandler.mjs';
|
|
25
25
|
import { govcyServiceEligibilityHandler } from './middleware/govcyServiceEligibilityHandler.mjs';
|
|
26
26
|
import { govcyLoadSubmissionData } from './middleware/govcyLoadSubmissionData.mjs';
|
|
27
|
+
import { govcyUploadMiddleware } from './middleware/govcyUpload.mjs';
|
|
27
28
|
import { isProdOrStaging , getEnvVariable, whatsIsMyEnvironment } from './utils/govcyEnvVariables.mjs';
|
|
28
29
|
import { logger } from "./utils/govcyLogger.mjs";
|
|
29
30
|
|
|
@@ -147,10 +148,17 @@ export default function initializeGovCyExpressService(){
|
|
|
147
148
|
// 📥 -- ROUTE: Handle POST requests for review page. The `submit` action
|
|
148
149
|
app.post('/:siteId/review', serviceConfigDataMiddleware, requireAuth, naturalPersonPolicy, govcyServiceEligibilityHandler(), govcyReviewPostHandler());
|
|
149
150
|
|
|
151
|
+
// 🗃️ -- ROUTE: Handle POST requests for file uploads for a page.
|
|
152
|
+
app.post('/:siteId/:pageUrl/upload',
|
|
153
|
+
serviceConfigDataMiddleware,
|
|
154
|
+
requireAuth, // UNCOMMENT
|
|
155
|
+
naturalPersonPolicy, // UNCOMMENT
|
|
156
|
+
govcyServiceEligibilityHandler(true), // UNCOMMENT
|
|
157
|
+
govcyUploadMiddleware);
|
|
158
|
+
|
|
150
159
|
// 👀📥 -- ROUTE: Handle POST requests (Form Submissions) based on siteId and pageUrl, using govcyFormsPostHandler middleware
|
|
151
160
|
app.post('/:siteId/:pageUrl', serviceConfigDataMiddleware, requireAuth, naturalPersonPolicy, govcyServiceEligibilityHandler(true), govcyFormsPostHandler());
|
|
152
161
|
|
|
153
|
-
|
|
154
162
|
// post for /:siteId/review
|
|
155
163
|
|
|
156
164
|
// 🔹 Catch 404 errors (must be after all routes)
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
import { getLoginUrl, handleCallback, getLogoutUrl } from '../auth/cyLoginAuth.mjs';
|
|
8
8
|
import { logger } from "../utils/govcyLogger.mjs";
|
|
9
9
|
import { handleMiddlewareError } from "../utils/govcyUtils.mjs";
|
|
10
|
+
import { errorResponse } from "../utils/govcyApiResponse.mjs";
|
|
11
|
+
import { isApiRequest } from '../utils/govcyApiDetection.mjs';
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* Middleware to check if the user is authenticated. If not, redirect to the login page.
|
|
@@ -17,6 +19,12 @@ import { handleMiddlewareError } from "../utils/govcyUtils.mjs";
|
|
|
17
19
|
*/
|
|
18
20
|
export function requireAuth(req, res, next) {
|
|
19
21
|
if (!req.session.user) {
|
|
22
|
+
if (isApiRequest(req)) {
|
|
23
|
+
const err = new Error("Unauthorized: user not authenticated");
|
|
24
|
+
err.status = 401;
|
|
25
|
+
return next(err);
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
// Store the original URL before redirecting to login
|
|
21
29
|
req.session.redirectAfterLogin = req.originalUrl;
|
|
22
30
|
return res.redirect('/login');
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
import { handleMiddlewareError } from "../utils/govcyUtils.mjs";
|
|
3
|
+
import { errorResponse } from '../utils/govcyApiResponse.mjs';
|
|
4
|
+
import { isApiRequest } from '../utils/govcyApiDetection.mjs';
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
7
|
* Middleware to handle CSRF token generation and validation.
|
|
5
8
|
*
|
|
@@ -14,7 +17,18 @@ export function govcyCsrfMiddleware(req, res, next) {
|
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
req.csrfToken = () => req.session.csrfToken;
|
|
17
|
-
|
|
20
|
+
|
|
21
|
+
if (
|
|
22
|
+
req.method === 'POST' &&
|
|
23
|
+
req.headers['content-type']?.includes('multipart/form-data') &&
|
|
24
|
+
isApiRequest(req)) {
|
|
25
|
+
const tokenFromHeader = req.get('X-CSRF-Token');
|
|
26
|
+
// UNCOMMENT
|
|
27
|
+
if (!tokenFromHeader || tokenFromHeader !== req.session.csrfToken) {
|
|
28
|
+
return res.status(400).json(errorResponse(403, 'Invalid CSRF token'));
|
|
29
|
+
}
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
18
32
|
// Check token on POST requests
|
|
19
33
|
if (req.method === 'POST') {
|
|
20
34
|
const tokenFromBody = req.body._csrf;
|
|
@@ -3,6 +3,8 @@ import * as govcyResources from "../resources/govcyResources.mjs";
|
|
|
3
3
|
import * as dataLayer from "../utils/govcyDataLayer.mjs";
|
|
4
4
|
import { logger } from "../utils/govcyLogger.mjs";
|
|
5
5
|
import { whatsIsMyEnvironment } from '../utils/govcyEnvVariables.mjs';
|
|
6
|
+
import { errorResponse } from '../utils/govcyApiResponse.mjs';
|
|
7
|
+
import { isApiRequest } from '../utils/govcyApiDetection.mjs';
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* Middleware function to handle HTTP errors and render appropriate error pages.
|
|
@@ -49,9 +51,8 @@ export function govcyHttpErrorHandler(err, req, res, next) {
|
|
|
49
51
|
|
|
50
52
|
res.status(statusCode);
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return res.json({ error: message });
|
|
54
|
+
if (isApiRequest(req)) {
|
|
55
|
+
return res.status(statusCode).json(errorResponse(statusCode, message));
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
// Render an error page for non-JSON requests
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import multer from 'multer';
|
|
2
|
+
import { logger } from '../utils/govcyLogger.mjs';
|
|
3
|
+
import { successResponse, errorResponse } from '../utils/govcyApiResponse.mjs';
|
|
4
|
+
import { ALLOWED_MULTER_FILE_SIZE_MB } from "../utils/govcyConstants.mjs";
|
|
5
|
+
import { handleFileUpload } from "../utils/govcyHandleFiles.mjs";
|
|
6
|
+
|
|
7
|
+
// Configure multer to store the file in memory (not disk) and limit the size to 10MB
|
|
8
|
+
const upload = multer({
|
|
9
|
+
storage: multer.memoryStorage(),
|
|
10
|
+
limits: { fileSize: ALLOWED_MULTER_FILE_SIZE_MB * 1024 * 1024 } // 10MB
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export const govcyUploadMiddleware = [
|
|
17
|
+
upload.single('file'), // multer parses the uploaded file and stores it in req.file
|
|
18
|
+
|
|
19
|
+
async function govcyUploadHandler(req, res) {
|
|
20
|
+
const result = await handleFileUpload({
|
|
21
|
+
service: req.serviceData,
|
|
22
|
+
store: req.session,
|
|
23
|
+
siteId: req.params.siteId,
|
|
24
|
+
pageUrl: req.params.pageUrl,
|
|
25
|
+
elementName: req.body?.elementName,
|
|
26
|
+
file: req.file
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (result.status !== 200) {
|
|
30
|
+
logger.error("Upload failed", result);
|
|
31
|
+
return res.status(result.status).json(errorResponse(result.status, result.errorMessage || 'File upload failed'));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return res.json(successResponse(result.data));
|
|
35
|
+
}
|
|
36
|
+
];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// 🔍 Select all file inputs that have the .govcy-file-upload class
|
|
2
|
+
const fileInputs = document.querySelectorAll('input[type="file"].govcy-file-upload');
|
|
3
|
+
|
|
4
|
+
// 🔐 Get the CSRF token from a hidden input field (generated by your backend)
|
|
5
|
+
const csrfToken = document.querySelector('input[type="hidden"][name="_csrf"]')?.value;
|
|
6
|
+
|
|
7
|
+
// 🔧 Define siteId and pageUrl (you can dynamically extract these later)
|
|
8
|
+
const siteId = 'test';
|
|
9
|
+
const pageUrl = 'data-entry-all';
|
|
10
|
+
|
|
11
|
+
// 🔁 Loop over each file input and attach a change event listener
|
|
12
|
+
fileInputs.forEach(input => {
|
|
13
|
+
input.addEventListener('change', async (event) => {
|
|
14
|
+
// 📦 Grab the selected file
|
|
15
|
+
const file = event.target.files[0];
|
|
16
|
+
const elementName = input.name; // Form field's `name` attribute
|
|
17
|
+
|
|
18
|
+
if (!file) return; // Exit if no file was selected
|
|
19
|
+
|
|
20
|
+
// 🧵 Prepare form-data payload for the API
|
|
21
|
+
const formData = new FormData();
|
|
22
|
+
formData.append('file', file); // Attach the actual file
|
|
23
|
+
formData.append('elementName', elementName); // Attach the field name for backend lookup
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
// 🚀 Send file to the backend upload API
|
|
27
|
+
const response = await axios.post(`/${siteId}/${pageUrl}/upload`, formData, {
|
|
28
|
+
headers: {
|
|
29
|
+
'X-CSRF-Token': csrfToken // 🔐 Pass CSRF token in custom header
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const { sha256, fileId } = response.data.Data;
|
|
34
|
+
|
|
35
|
+
// 📝 Store returned metadata in hidden fields for submission with the form
|
|
36
|
+
document.querySelector(`[name="${elementName}Attachment[fileId]"`).value = fileId;
|
|
37
|
+
document.querySelector(`[name="${elementName}Attachment[sha256]"`).value = sha256;
|
|
38
|
+
|
|
39
|
+
alert('✅ File uploaded successfully');
|
|
40
|
+
|
|
41
|
+
} catch (err) {
|
|
42
|
+
// ⚠️ Show an error message if upload fails
|
|
43
|
+
alert('❌ Upload failed: ' + (err.response?.data?.error || err.message));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -113,9 +113,9 @@ export const staticResources = {
|
|
|
113
113
|
element: "htmlElement",
|
|
114
114
|
params: {
|
|
115
115
|
text: {
|
|
116
|
-
en: `<script src="/js/govcyForms.js"></script>`,
|
|
117
|
-
el: `<script src="/js/govcyForms.js"></script>`,
|
|
118
|
-
tr: `<script src="/js/govcyForms.js"></script>`
|
|
116
|
+
en: `<script src="https://cdn.jsdelivr.net/npm/axios@1.6.2/dist/axios.min.js"></script><script src="/js/govcyForms.js"></script><script src="/js/govcyFiles.js"></script>`,
|
|
117
|
+
el: `<script src="https://cdn.jsdelivr.net/npm/axios@1.6.2/dist/axios.min.js"></script><script src="/js/govcyForms.js"></script><script src="/js/govcyFiles.js"></script>`,
|
|
118
|
+
tr: `<script src="https://cdn.jsdelivr.net/npm/axios@1.6.2/dist/axios.min.js"></script><script src="/js/govcyForms.js"></script><script src="/js/govcyFiles.js"></script>`
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
},
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if a request is targeting an API endpoint.
|
|
3
|
+
* Currently matches:
|
|
4
|
+
* - Accept header with application/json
|
|
5
|
+
* - URLs ending with /upload or /download under a site/page structure
|
|
6
|
+
*
|
|
7
|
+
* @param {object} req - Express request object
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
export function isApiRequest(req) {
|
|
11
|
+
const acceptJson = (req.headers?.accept || "").toLowerCase().includes("application/json");
|
|
12
|
+
|
|
13
|
+
const apiUrlPattern = /^\/[^/]+\/[^/]+\/(upload|download)$/;
|
|
14
|
+
const isStructuredApiUrl = apiUrlPattern.test(req.originalUrl || req.url);
|
|
15
|
+
|
|
16
|
+
return acceptJson || isStructuredApiUrl;
|
|
17
|
+
}
|
|
@@ -5,7 +5,7 @@ import { logger } from "./govcyLogger.mjs";
|
|
|
5
5
|
* Utility to handle API communication with retry logic
|
|
6
6
|
* @param {string} method - HTTP method (e.g., 'post', 'get', etc.)
|
|
7
7
|
* @param {string} url - API endpoint URL
|
|
8
|
-
* @param {object} inputData - Payload for the request (optional)
|
|
8
|
+
* @param {object|FormData} inputData - Payload for the request (optional)
|
|
9
9
|
* @param {boolean} useAccessTokenAuth - Whether to use Authorization header with Bearer token
|
|
10
10
|
* @param {object} user - User object containing access_token (optional)
|
|
11
11
|
* @param {object} headers - Custom headers (optional)
|
|
@@ -39,6 +39,14 @@ export async function govcyApiRequest(
|
|
|
39
39
|
requestHeaders['Authorization'] = `Bearer ${user.access_token}`;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// If inputData is FormData, for attachments
|
|
43
|
+
if (inputData instanceof (await import('form-data')).default) {
|
|
44
|
+
requestHeaders = {
|
|
45
|
+
...requestHeaders,
|
|
46
|
+
...inputData.getHeaders(), // includes boundary in content-type
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
while (attempt < retries) {
|
|
43
51
|
try {
|
|
44
52
|
logger.debug(`📤 Sending API request (Attempt ${attempt + 1})`, { method, url, inputData, requestHeaders });
|
|
@@ -47,13 +55,22 @@ export async function govcyApiRequest(
|
|
|
47
55
|
const axiosConfig = {
|
|
48
56
|
method,
|
|
49
57
|
url,
|
|
50
|
-
|
|
58
|
+
...(inputData instanceof (await import('form-data')).default // If inputData is FormData, for attachments
|
|
59
|
+
? { data: inputData }
|
|
60
|
+
: { [method?.toLowerCase() === 'get' ? 'params' : 'data']: inputData }),
|
|
51
61
|
headers: requestHeaders,
|
|
52
62
|
timeout: 10000, // 10 seconds timeout
|
|
53
63
|
// ✅ Treat only these statuses as "resolved" (no throw)
|
|
54
64
|
validateStatus: (status) => allowedHTTPStatusCodes.includes(status),
|
|
55
65
|
};
|
|
56
66
|
|
|
67
|
+
// If inputData is FormData, for attachments
|
|
68
|
+
if (inputData instanceof (await import('form-data')).default) {
|
|
69
|
+
axiosConfig.maxContentLength = Infinity;
|
|
70
|
+
axiosConfig.maxBodyLength = Infinity;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
57
74
|
// Add httpsAgent if NOT production to allow self-signed certificates
|
|
58
75
|
// Use per-call config for self-signed certs
|
|
59
76
|
if (allowSelfSignedCerts) {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The successResponse function creates a standardized success response object.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} data - The data to be included in the response.
|
|
5
|
+
* @returns {Object} - The success response object.
|
|
6
|
+
*/
|
|
7
|
+
export function successResponse(data = null) {
|
|
8
|
+
return {
|
|
9
|
+
Succeeded: true,
|
|
10
|
+
ErrorCode: 0,
|
|
11
|
+
ErrorMessage: '',
|
|
12
|
+
Data: data
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The errorResponse function creates a standardized error response object.
|
|
18
|
+
*
|
|
19
|
+
* @param {int} code - The error code to be included in the response.
|
|
20
|
+
* @param {string} message - The error message to be included in the response.
|
|
21
|
+
* @param {Object} data - Additional data to be included in the response.
|
|
22
|
+
* @returns {Object} - The error response object.
|
|
23
|
+
*/
|
|
24
|
+
export function errorResponse(code = 1, message = 'Unknown error', data = null) {
|
|
25
|
+
return {
|
|
26
|
+
Succeeded: false,
|
|
27
|
+
ErrorCode: code,
|
|
28
|
+
ErrorMessage: message,
|
|
29
|
+
Data: data
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared constants for allowed form elements.
|
|
3
3
|
*/
|
|
4
|
-
export const ALLOWED_FORM_ELEMENTS = ["textInput", "textArea", "select", "radios", "checkboxes", "datePicker", "dateInput"];
|
|
4
|
+
export const ALLOWED_FORM_ELEMENTS = ["textInput", "textArea", "select", "radios", "checkboxes", "datePicker", "dateInput"];
|
|
5
|
+
export const ALLOWED_FILE_MIME_TYPES = ['application/pdf', 'image/jpeg', 'image/png'];
|
|
6
|
+
export const ALLOWED_FILE_EXTENSIONS = ['pdf', 'jpg', 'jpeg', 'png'];
|
|
7
|
+
export const ALLOWED_FILE_SIZE_MB = 5; // Maximum file size in MB
|
|
8
|
+
export const ALLOWED_MULTER_FILE_SIZE_MB = 10; // Maximum file size in MB
|
|
@@ -328,6 +328,20 @@ export function getSiteLoadData(store, siteId) {
|
|
|
328
328
|
return null;
|
|
329
329
|
}
|
|
330
330
|
|
|
331
|
+
/**
|
|
332
|
+
* Get the site's reference number from load data from the store
|
|
333
|
+
*
|
|
334
|
+
* @param {object} store The session store
|
|
335
|
+
* @param {string} siteId The site ID
|
|
336
|
+
* @returns {string|null} The reference number or null if not available
|
|
337
|
+
*/
|
|
338
|
+
export function getSiteLoadDataReferenceNumber(store, siteId) {
|
|
339
|
+
const ref = store?.siteData?.[siteId]?.loadData?.referenceValue;
|
|
340
|
+
|
|
341
|
+
return typeof ref === 'string' && ref.trim() !== '' ? ref : null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
331
345
|
/**
|
|
332
346
|
* Get the site's input data from the store
|
|
333
347
|
*
|
|
@@ -124,7 +124,7 @@ export function evaluateExpressionWithFlattening(expression, object, prefix = ''
|
|
|
124
124
|
* @returns {{ result: true } | { result: false, redirect: string }}
|
|
125
125
|
* An object indicating whether to render the page or redirect
|
|
126
126
|
*/
|
|
127
|
-
export function evaluatePageConditions(page, store, siteKey, req) {
|
|
127
|
+
export function evaluatePageConditions(page, store, siteKey, req = {}) {
|
|
128
128
|
// Get conditions array from nested page structure
|
|
129
129
|
const conditions = page?.pageData?.conditions;
|
|
130
130
|
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import FormData from 'form-data';
|
|
2
|
+
import { getPageConfigData } from "./govcyLoadConfigData.mjs";
|
|
3
|
+
import { evaluatePageConditions } from "./govcyExpressions.mjs";
|
|
4
|
+
import { getEnvVariable, getEnvVariableBool } from "./govcyEnvVariables.mjs";
|
|
5
|
+
import { ALLOWED_FILE_MIME_TYPES, ALLOWED_FILE_SIZE_MB } from "./govcyConstants.mjs";
|
|
6
|
+
import { govcyApiRequest } from "./govcyApiRequest.mjs";
|
|
7
|
+
import * as dataLayer from "./govcyDataLayer.mjs";
|
|
8
|
+
import { logger } from './govcyLogger.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Handles the logic for uploading a file to the configured Upload API.
|
|
12
|
+
* Does not send a response — just returns a standard object to be handled by middleware.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} opts - Input parameters
|
|
15
|
+
* @param {object} opts.service - The service config object
|
|
16
|
+
* @param {object} opts.store - Session store (req.session)
|
|
17
|
+
* @param {string} opts.siteId - Site ID
|
|
18
|
+
* @param {string} opts.pageUrl - Page URL
|
|
19
|
+
* @param {string} opts.elementName - Name of file input
|
|
20
|
+
* @param {object} opts.file - File object from multer (req.file)
|
|
21
|
+
* @returns {Promise<{ status: number, data?: object, errorMessage?: string }>}
|
|
22
|
+
*/
|
|
23
|
+
export async function handleFileUpload({ service, store, siteId, pageUrl, elementName, file }) {
|
|
24
|
+
try {
|
|
25
|
+
// Validate essentials
|
|
26
|
+
// Early exit if key things are missing
|
|
27
|
+
if (!file || !elementName) {
|
|
28
|
+
return {
|
|
29
|
+
status: 400,
|
|
30
|
+
errorMessage: 'Missing file or element name'
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Get the upload configuration
|
|
35
|
+
const uploadCfg = service?.site?.fileUploadAPIEndpoint;
|
|
36
|
+
// Check if upload configuration is available
|
|
37
|
+
if (!uploadCfg?.url || !uploadCfg?.clientKey || !uploadCfg?.serviceId) {
|
|
38
|
+
return {
|
|
39
|
+
status: 400,
|
|
40
|
+
errorMessage: 'Missing upload configuration'
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Environment vars
|
|
45
|
+
const allowSelfSignedCerts = getEnvVariableBool("ALLOW_SELF_SIGNED_CERTIFICATES", false);
|
|
46
|
+
let url = getEnvVariable(uploadCfg.url || "", false);
|
|
47
|
+
const clientKey = getEnvVariable(uploadCfg.clientKey || "", false);
|
|
48
|
+
const serviceId = getEnvVariable(uploadCfg.serviceId || "", false);
|
|
49
|
+
const dsfGtwKey = getEnvVariable(uploadCfg?.dsfgtwApiKey || "", "");
|
|
50
|
+
const method = (uploadCfg?.method || "PUT").toLowerCase();
|
|
51
|
+
|
|
52
|
+
// Check if the upload API is configured correctly
|
|
53
|
+
if (!url || !clientKey) {
|
|
54
|
+
return {
|
|
55
|
+
status: 400,
|
|
56
|
+
errorMessage: 'Missing environment variables for upload'
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Construct the URL with tag being the elementName
|
|
61
|
+
const tag = encodeURIComponent(elementName.trim());
|
|
62
|
+
url += `/${tag}`;
|
|
63
|
+
|
|
64
|
+
// Get the page configuration using utility (safely extracts the correct page)
|
|
65
|
+
const page = getPageConfigData(service, pageUrl);
|
|
66
|
+
// Check if the page template is valid
|
|
67
|
+
if (!page?.pageTemplate) {
|
|
68
|
+
return {
|
|
69
|
+
status: 400,
|
|
70
|
+
errorMessage: 'Invalid page configuration'
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ----- Conditional logic comes here
|
|
75
|
+
// Respect conditional logic: If the page is skipped due to conditions, abort
|
|
76
|
+
const conditionResult = evaluatePageConditions(page, store, siteId);
|
|
77
|
+
if (conditionResult.result === false) {
|
|
78
|
+
return {
|
|
79
|
+
status: 403,
|
|
80
|
+
errorMessage: 'This page is skipped by conditional logic'
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// deep copy the page template to avoid modifying the original
|
|
85
|
+
const pageTemplateCopy = JSON.parse(JSON.stringify(page.pageTemplate));
|
|
86
|
+
// Validate the field: Only allow upload if the page contains a fileInput with the given name
|
|
87
|
+
const isAllowed = pageContainsFileInput(pageTemplateCopy, elementName);
|
|
88
|
+
if (!isAllowed) {
|
|
89
|
+
return {
|
|
90
|
+
status: 403,
|
|
91
|
+
errorMessage: `File input [${elementName}] not allowed on this page`
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Empty file check
|
|
96
|
+
if (file.size === 0) {
|
|
97
|
+
return {
|
|
98
|
+
status: 400,
|
|
99
|
+
errorMessage: 'Uploaded file is empty'
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// file type checks
|
|
104
|
+
// 1. Check declared mimetype
|
|
105
|
+
if (!ALLOWED_FILE_MIME_TYPES.includes(file.mimetype)) {
|
|
106
|
+
return {
|
|
107
|
+
status: 400,
|
|
108
|
+
errorMessage: 'Invalid file type (MIME not allowed)'
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 2. Check actual file content (magic bytes) matches claimed MIME type
|
|
113
|
+
if (!isMagicByteValid(file.buffer, file.mimetype)) {
|
|
114
|
+
return {
|
|
115
|
+
status: 400,
|
|
116
|
+
errorMessage: 'Invalid file type (magic byte mismatch)'
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// File size check
|
|
121
|
+
if (file.size > ALLOWED_FILE_SIZE_MB * 1024 * 1024) {
|
|
122
|
+
return {
|
|
123
|
+
status: 400,
|
|
124
|
+
errorMessage: 'File exceeds allowed size'
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Prepare FormData
|
|
129
|
+
const form = new FormData();
|
|
130
|
+
form.append('file', file.buffer, {
|
|
131
|
+
filename: file.originalname,
|
|
132
|
+
contentType: file.mimetype,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
logger.debug("Prepared FormData with file:", {
|
|
136
|
+
filename: file.originalname,
|
|
137
|
+
mimetype: file.mimetype,
|
|
138
|
+
size: file.size
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Get the user
|
|
142
|
+
const user = dataLayer.getUser(store);
|
|
143
|
+
// Perform the upload request
|
|
144
|
+
const response = await govcyApiRequest(
|
|
145
|
+
method,
|
|
146
|
+
url,
|
|
147
|
+
form,
|
|
148
|
+
true,
|
|
149
|
+
user,
|
|
150
|
+
{
|
|
151
|
+
accept: "text/plain",
|
|
152
|
+
"client-key": clientKey,
|
|
153
|
+
"service-id": serviceId,
|
|
154
|
+
...(dsfGtwKey !== "" && { "dsfgtw-api-key": dsfGtwKey })
|
|
155
|
+
},
|
|
156
|
+
3,
|
|
157
|
+
allowSelfSignedCerts
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
// If not succeeded, handle error
|
|
161
|
+
if (!response?.Succeeded) {
|
|
162
|
+
return {
|
|
163
|
+
status: 500,
|
|
164
|
+
errorMessage: `${response?.ErrorCode} - ${response?.ErrorMessage} - fileUploadAPIEndpoint returned succeeded false`
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Check if the response contains the expected data
|
|
169
|
+
if (!response?.Data?.fileId || !response?.Data?.sha256) {
|
|
170
|
+
return {
|
|
171
|
+
status: 500,
|
|
172
|
+
errorMessage: 'Missing fileId or sha256 in response'
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ✅ Success
|
|
177
|
+
logger.debug("File upload successful", response.Data);
|
|
178
|
+
logger.info(`File uploaded successfully for element ${elementName} on page ${pageUrl} for site ${siteId}`);
|
|
179
|
+
return {
|
|
180
|
+
status: 200,
|
|
181
|
+
data: {
|
|
182
|
+
sha: response.Data.sha256,
|
|
183
|
+
filename: response.Data.fileName || '',
|
|
184
|
+
fileId: response.Data.fileId,
|
|
185
|
+
mimeType: response.Data.contentType || '',
|
|
186
|
+
sha256: response.Data.fileSize || ''
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
} catch (err) {
|
|
191
|
+
return {
|
|
192
|
+
status: 500,
|
|
193
|
+
errorMessage: 'Upload failed' + (err.message ? `: ${err.message}` : ''),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
//--------------------------------------------------------------------------
|
|
199
|
+
// Helper Functions
|
|
200
|
+
/**
|
|
201
|
+
* Recursively checks whether any element (or its children) is a fileInput
|
|
202
|
+
* with the matching elementName.
|
|
203
|
+
*
|
|
204
|
+
* Supports:
|
|
205
|
+
* - Top-level fileInput
|
|
206
|
+
* - Nested `params.elements` (used in groups, conditionals, etc.)
|
|
207
|
+
* - Conditional radios/checkboxes with `items[].conditionalElements`
|
|
208
|
+
*
|
|
209
|
+
* @param {Array} elements - The array of elements to search
|
|
210
|
+
* @param {string} targetName - The name of the file input to check
|
|
211
|
+
* @returns {boolean} True if a matching fileInput is found, false otherwise
|
|
212
|
+
*/
|
|
213
|
+
function containsFileInput(elements = [], targetName) {
|
|
214
|
+
for (const el of elements) {
|
|
215
|
+
// ✅ Direct file input match
|
|
216
|
+
if (el.element === 'fileInput' && el.params?.name === targetName) {
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
// 🔁 Recurse into nested elements (e.g. groups, conditionals)
|
|
220
|
+
if (Array.isArray(el?.params?.elements)) {
|
|
221
|
+
if (containsFileInput(el.params.elements, targetName)) return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 🎯 Special case: conditional radios/checkboxes
|
|
225
|
+
if (
|
|
226
|
+
(el.element === 'radios' || el.element === 'checkboxes') &&
|
|
227
|
+
Array.isArray(el?.params?.items)
|
|
228
|
+
) {
|
|
229
|
+
for (const item of el.params.items) {
|
|
230
|
+
if (Array.isArray(item?.conditionalElements)) {
|
|
231
|
+
if (containsFileInput(item.conditionalElements, targetName)) return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Checks whether the specified page contains a valid fileInput for this element ID
|
|
241
|
+
* under any <form> element in its sections
|
|
242
|
+
*
|
|
243
|
+
* @param {object} pageTemplate The page template object
|
|
244
|
+
* @param {string} elementName The name of the element to check
|
|
245
|
+
* @return {boolean} True if a fileInput exists, false otherwise
|
|
246
|
+
*/
|
|
247
|
+
function pageContainsFileInput(pageTemplate, elementName) {
|
|
248
|
+
const sections = pageTemplate?.sections || [];
|
|
249
|
+
return sections.some(section =>
|
|
250
|
+
section?.elements?.some(el =>
|
|
251
|
+
el.element === 'form' &&
|
|
252
|
+
containsFileInput(el.params?.elements, elementName)
|
|
253
|
+
)
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Validates magic bytes against expected mimetype
|
|
259
|
+
* @param {Buffer} buffer
|
|
260
|
+
* @param {string} mimetype
|
|
261
|
+
* @returns {boolean}
|
|
262
|
+
*/
|
|
263
|
+
function isMagicByteValid(buffer, mimetype) {
|
|
264
|
+
const signatures = {
|
|
265
|
+
'application/pdf': [0x25, 0x50, 0x44, 0x46], // %PDF
|
|
266
|
+
'image/png': [0x89, 0x50, 0x4E, 0x47], // PNG
|
|
267
|
+
'image/jpeg': [0xFF, 0xD8, 0xFF], // JPG/JPEG
|
|
268
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': [0x50, 0x4B, 0x03, 0x04], // DOCX
|
|
269
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': [0x50, 0x4B, 0x03, 0x04], // XLSX
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const expected = signatures[mimetype];
|
|
273
|
+
if (!expected) return false; // unknown type
|
|
274
|
+
|
|
275
|
+
const actual = Array.from(buffer.slice(0, expected.length));
|
|
276
|
+
return expected.every((byte, i) => actual[i] === byte);
|
|
277
|
+
}
|