diff options
author | 2024-02-10 18:19:14 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | 7584d2d7a7110aef0331ebfa178b2295842c59fa (patch) | |
tree | 900cd71237e6231b57936fcce77ff229cd459041 /app/Controllers/Http/Dashboard/ExportController.ts | |
parent | upgrade recipes submodule (diff) | |
download | ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.gz ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.zst ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.zip |
refactor: project maintenance
- work in progress
Diffstat (limited to 'app/Controllers/Http/Dashboard/ExportController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/ExportController.ts | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/app/Controllers/Http/Dashboard/ExportController.ts b/app/Controllers/Http/Dashboard/ExportController.ts index 7155eab..5b6df70 100644 --- a/app/Controllers/Http/Dashboard/ExportController.ts +++ b/app/Controllers/Http/Dashboard/ExportController.ts | |||
@@ -1,33 +1,30 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | 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< | 18 | return obj.map((item) => deepParseToJSON(item)) as unknown as Record<string, unknown> |
19 | string, | ||
20 | unknown | ||
21 | >; | ||
22 | } else { | 19 | } else { |
23 | // If obj is an object, recursively parse its keys | 20 | // If obj is an object, recursively parse its keys |
24 | const parsedObj: Record<string, unknown> = {}; | 21 | const parsedObj: Record<string, unknown> = {} |
25 | for (const key in obj) { | 22 | for (const key in obj) { |
26 | if (obj.hasOwnProperty(key)) { | 23 | if (obj.hasOwnProperty(key)) { |
27 | parsedObj[key] = deepParseToJSON(obj[key]); | 24 | parsedObj[key] = deepParseToJSON(obj[key]) |
28 | } | 25 | } |
29 | } | 26 | } |
30 | return parsedObj; | 27 | return parsedObj |
31 | } | 28 | } |
32 | } | 29 | } |
33 | 30 | ||
@@ -35,10 +32,10 @@ export default class ExportController { | |||
35 | /** | 32 | /** |
36 | * Display the export page | 33 | * Display the export page |
37 | */ | 34 | */ |
38 | public async show({ auth, response }: HttpContextContract) { | 35 | public async show({ auth, response }: HttpContext) { |
39 | const user = auth.user!; | 36 | const user = auth.user! |
40 | const services = await user.related('services').query(); | 37 | const services = await user.related('services').query() |
41 | const workspaces = await user.related('workspaces').query(); | 38 | const workspaces = await user.related('workspaces').query() |
42 | 39 | ||
43 | const exportData = { | 40 | const exportData = { |
44 | username: user.username, | 41 | username: user.username, |
@@ -46,11 +43,11 @@ export default class ExportController { | |||
46 | mail: user.email, | 43 | mail: user.email, |
47 | services: deepParseToJSON(JSON.parse(JSON.stringify(services))), | 44 | services: deepParseToJSON(JSON.parse(JSON.stringify(services))), |
48 | workspaces: deepParseToJSON(JSON.parse(JSON.stringify(workspaces))), | 45 | workspaces: deepParseToJSON(JSON.parse(JSON.stringify(workspaces))), |
49 | }; | 46 | } |
50 | 47 | ||
51 | return response | 48 | return response |
52 | .header('Content-Type', 'application/force-download') | 49 | .header('Content-Type', 'application/force-download') |
53 | .header('Content-disposition', 'attachment; filename=export.ferdium-data') | 50 | .header('Content-disposition', 'attachment; filename=export.ferdium-data') |
54 | .send(exportData); | 51 | .send(exportData) |
55 | } | 52 | } |
56 | } | 53 | } |