diff options
Diffstat (limited to 'app/Controllers/Http/Dashboard/ExportController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/ExportController.ts | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/app/Controllers/Http/Dashboard/ExportController.ts b/app/Controllers/Http/Dashboard/ExportController.ts index 5b6df70..6b20a82 100644 --- a/app/Controllers/Http/Dashboard/ExportController.ts +++ b/app/Controllers/Http/Dashboard/ExportController.ts | |||
@@ -1,30 +1,33 @@ | |||
1 | import type { HttpContext } from '@adonisjs/core/http' | 1 | import type { HttpContext } from '@adonisjs/core/http'; |
2 | 2 | ||
3 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | 3 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
4 | function deepParseToJSON(obj: any): Record<string, unknown> { | 4 | function deepParseToJSON(obj: any): Record<string, unknown> { |
5 | if (typeof obj !== 'object' || obj === null) { | 5 | if (typeof obj !== 'object' || obj === null) { |
6 | try { | 6 | try { |
7 | // Try to parse the object as JSON | 7 | // Try to parse the object as JSON |
8 | return JSON.parse(obj) as Record<string, unknown> | 8 | return JSON.parse(obj) as Record<string, unknown>; |
9 | } catch { | 9 | } catch { |
10 | // If parsing fails, return the original value | 10 | // If parsing fails, return the original value |
11 | return obj | 11 | return obj; |
12 | } | 12 | } |
13 | } | 13 | } |
14 | 14 | ||
15 | // If obj is an object, recursively parse its keys | 15 | // If obj is an object, recursively parse its keys |
16 | if (Array.isArray(obj)) { | 16 | if (Array.isArray(obj)) { |
17 | // If obj is an array, recursively parse each element | 17 | // If obj is an array, recursively parse each element |
18 | return obj.map((item) => deepParseToJSON(item)) as unknown as Record<string, unknown> | 18 | return obj.map(item => deepParseToJSON(item)) as unknown as Record< |
19 | string, | ||
20 | unknown | ||
21 | >; | ||
19 | } else { | 22 | } else { |
20 | // If obj is an object, recursively parse its keys | 23 | // If obj is an object, recursively parse its keys |
21 | const parsedObj: Record<string, unknown> = {} | 24 | const parsedObj: Record<string, unknown> = {}; |
22 | for (const key in obj) { | 25 | for (const key in obj) { |
23 | if (obj.hasOwnProperty(key)) { | 26 | if (obj.hasOwnProperty(key)) { |
24 | parsedObj[key] = deepParseToJSON(obj[key]) | 27 | parsedObj[key] = deepParseToJSON(obj[key]); |
25 | } | 28 | } |
26 | } | 29 | } |
27 | return parsedObj | 30 | return parsedObj; |
28 | } | 31 | } |
29 | } | 32 | } |
30 | 33 | ||
@@ -33,9 +36,9 @@ export default class ExportController { | |||
33 | * Display the export page | 36 | * Display the export page |
34 | */ | 37 | */ |
35 | public async show({ auth, response }: HttpContext) { | 38 | public async show({ auth, response }: HttpContext) { |
36 | const user = auth.user! | 39 | const user = auth.user!; |
37 | const services = await user.related('services').query() | 40 | const services = await user.related('services').query(); |
38 | const workspaces = await user.related('workspaces').query() | 41 | const workspaces = await user.related('workspaces').query(); |
39 | 42 | ||
40 | const exportData = { | 43 | const exportData = { |
41 | username: user.username, | 44 | username: user.username, |
@@ -43,11 +46,11 @@ export default class ExportController { | |||
43 | mail: user.email, | 46 | mail: user.email, |
44 | services: deepParseToJSON(JSON.parse(JSON.stringify(services))), | 47 | services: deepParseToJSON(JSON.parse(JSON.stringify(services))), |
45 | workspaces: deepParseToJSON(JSON.parse(JSON.stringify(workspaces))), | 48 | workspaces: deepParseToJSON(JSON.parse(JSON.stringify(workspaces))), |
46 | } | 49 | }; |
47 | 50 | ||
48 | return response | 51 | return response |
49 | .header('Content-Type', 'application/force-download') | 52 | .header('Content-Type', 'application/force-download') |
50 | .header('Content-disposition', 'attachment; filename=export.ferdium-data') | 53 | .header('Content-disposition', 'attachment; filename=export.ferdium-data') |
51 | .send(exportData) | 54 | .send(exportData); |
52 | } | 55 | } |
53 | } | 56 | } |