-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tweak(tupaiaWeb): RN-1383: Make raw data downloads use one time link #5848
base: dev
Are you sure you want to change the base?
Changes from 3 commits
640d6f8
b9fe795
8e5545c
5e700da
25adc2b
264e69b
15a813c
60f5f9d
bb6d681
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,22 +3,11 @@ | |
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { requireEnv } from '@tupaia/utils'; | ||
|
||
enum EmailExportFileModes { | ||
ATTACHMENT = 'attachment', | ||
DOWNLOAD_LINK = 'downloadLink', | ||
} | ||
|
||
const createDownloadLink = (filePath: string) => { | ||
const fileName = path.basename(filePath); | ||
return `${process.env.ADMIN_PANEL_SERVER_URL}/v1/export/download/${encodeURIComponent(fileName)}`; | ||
}; | ||
|
||
const generateAttachments = async (filePath: string) => { | ||
const createDownloadLink = (filePath: string, url: string) => { | ||
const fileName = path.basename(filePath); | ||
const buffer = await fs.readFileSync(filePath); | ||
return [{ filename: fileName, content: buffer }]; | ||
return `${url}/export/download/${encodeURIComponent(fileName)}`; | ||
}; | ||
|
||
type ResponseBody = { | ||
|
@@ -28,13 +17,13 @@ type ResponseBody = { | |
|
||
type Req = { | ||
query: { | ||
emailExportFileMode?: EmailExportFileModes; | ||
platform?: 'tupaia' | 'adminPanel' | 'datatrak'; | ||
}; | ||
}; | ||
|
||
export const constructExportEmail = async (responseBody: ResponseBody, req: Req) => { | ||
const { emailExportFileMode = EmailExportFileModes.DOWNLOAD_LINK } = req.query; | ||
const { error, filePath } = responseBody; | ||
const { platform } = req.query; | ||
const subject = 'Your export from Tupaia'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the subject should change depending on the platform? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good idea |
||
if (error) { | ||
return { | ||
|
@@ -48,18 +37,36 @@ ${error}`, | |
throw new Error('No filePath in export response body'); | ||
} | ||
|
||
if (emailExportFileMode === EmailExportFileModes.ATTACHMENT) { | ||
return { | ||
subject, | ||
message: 'Please find your requested export attached to this email.', | ||
attachments: await generateAttachments(filePath), | ||
}; | ||
const platformKey = platform || 'adminPanel'; | ||
|
||
/** | ||
* We don't yet have single sign-on across all platforms, so we need to know which platform the user is on so the correct session is used, from where they requested the export. We also can't use central-server api url for this directly because there needs to be an auth header in the request. | ||
*/ | ||
const PLATFORM_SETTINGS = { | ||
tupaia: { | ||
url: requireEnv('TUPAIA_WEB_SERVER_API_URL'), | ||
friendlyName: 'Tupaia', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the term friendlyName :) |
||
}, | ||
adminPanel: { | ||
url: requireEnv('ADMIN_PANEL_SERVER_API_URL'), | ||
friendlyName: 'the Admin Panel', | ||
}, | ||
datatrak: { | ||
url: requireEnv('DATATRAK_WEB_SERVER_API_URL'), | ||
friendlyName: 'DataTrak', | ||
}, | ||
}; | ||
|
||
if (!PLATFORM_SETTINGS[platformKey]) { | ||
throw new Error(`No API config found for platform: ${platformKey}`); | ||
} | ||
|
||
const downloadLink = createDownloadLink(filePath); | ||
const { friendlyName, url } = PLATFORM_SETTINGS[platformKey]; | ||
|
||
const downloadLink = createDownloadLink(filePath, url); | ||
return { | ||
subject, | ||
message: `Please click this one-time link to download your requested export: ${downloadLink} | ||
Note that you need to be logged in to the admin panel for it to work, and after clicking it once, you won't be able to download the file again.`, | ||
Note that you need to be logged in to ${friendlyName} for it to work, and after clicking it once, you won't be able to download the file again.`, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { getTempDirectory } from './getTempDirectory'; | ||
|
||
export const getExportPathForUser = (userId: string) => getTempDirectory(`exports/${userId}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { downloadPageAsPDF } from './downloadPageAsPDF'; | ||
export * from './s3'; | ||
export { sendEmail } from './sendEmail'; | ||
export { generateUnsubscribeToken, verifyUnsubscribeToken } from './unsubscribeToken'; | ||
export { configureDotEnv } from './configureDotEnv'; | ||
export { constructExportEmail } from './constructExportEmail'; | ||
export { getTempDirectory } from './getTempDirectory'; | ||
export { getExportPathForUser } from './getExportPathForUser'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should Lesmis be included as an option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea