chore: update src/pages/api/admin/config/storage.ts
swadhinbiswas committed 8 months ago
67cf10c
1 parent b7f9879
commit 67cf10c
1 file changed
+67 -7
Showing 1 changed file+67-7
▼src/pages/api/admin/config/storage.ts
+67-7
| @@ -1,4 +1,11 @@ | ||
1 | -import { updateStorageConfig, resetStorage, checkStorageHealth, getStorageConfig } from "@/lib/storage"; | |
1 | +import { | |
2 | + updateStorageConfig, | |
3 | + resetStorage, | |
4 | + checkStorageHealth, | |
5 | + getStorageConfig, | |
6 | + createStorageAdapter, | |
7 | + type StorageConfig, | |
8 | +} from "@/lib/storage"; | |
2 | 9 | import type { NodePgDatabase } from "drizzle-orm/node-postgres"; |
3 | 10 | import type { APIRoute } from "astro"; |
4 | 11 | import { withErrorHandler } from "@/lib/errors"; |
| @@ -62,12 +69,58 @@ export const POST: APIRoute = withErrorHandler(async ({ request, locals }) => { | ||
62 | 69 | return badRequest("Invalid JSON"); |
63 | 70 | } |
64 | 71 | |
65 | - const { action, config } = body; | |
72 | + const { action } = body; | |
73 | + const bodyConfig = body?.config ?? body; | |
74 | + | |
75 | + const normalizeConfig = (input: unknown): StorageConfig => { | |
76 | + if (!input || typeof input !== "object" || Array.isArray(input)) { | |
77 | + throw new Error("Config required"); | |
78 | + } | |
79 | + const cfg = input as Partial<StorageConfig>; | |
80 | + if (!cfg.type) { | |
81 | + throw new Error("Storage type required"); | |
82 | + } | |
83 | + | |
84 | + return { | |
85 | + ...cfg, | |
86 | + type: cfg.type, | |
87 | + basePath: typeof cfg.basePath === "string" | |
88 | + ? cfg.basePath | |
89 | + : cfg.type === "local" | |
90 | + ? "./data/storage" | |
91 | + : "", | |
92 | + } as StorageConfig; | |
93 | + }; | |
66 | 94 | |
67 | 95 | if (action === "test") { |
68 | - // TODO: Test the provided config without saving | |
69 | - // For now, we only support testing current config via GET ?check=true | |
70 | - return badRequest("Test config action not yet implemented"); | |
96 | + let testConfig: StorageConfig; | |
97 | + try { | |
98 | + testConfig = normalizeConfig(bodyConfig); | |
99 | + } catch (error: any) { | |
100 | + return badRequest(error?.message || "Invalid storage config"); | |
101 | + } | |
102 | + | |
103 | + try { | |
104 | + const adapter = createStorageAdapter(testConfig); | |
105 | + const testKey = `admin-storage-test/${Date.now()}-${Math.random().toString(36).slice(2)}.txt`; | |
106 | + | |
107 | + await adapter.put(testKey, Buffer.from("storage test")); | |
108 | + await adapter.delete(testKey).catch(() => { }); | |
109 | + | |
110 | + return success({ | |
111 | + success: true, | |
112 | + healthy: true, | |
113 | + type: testConfig.type, | |
114 | + }); | |
115 | + } catch (error: any) { | |
116 | + logger.warn({ error, storageType: testConfig.type }, "Storage config test failed"); | |
117 | + return success({ | |
118 | + success: false, | |
119 | + healthy: false, | |
120 | + type: testConfig.type, | |
121 | + error: error?.message || "Storage connection test failed", | |
122 | + }); | |
123 | + } | |
71 | 124 | } |
72 | 125 | |
73 | 126 | if (action === "reset") { |
| @@ -75,8 +128,15 @@ export const POST: APIRoute = withErrorHandler(async ({ request, locals }) => { | ||
75 | 128 | return success({ message: "Storage cache cleared" }); |
76 | 129 | } |
77 | 130 | |
78 | - if (!config) { | |
79 | - return badRequest("Config required"); | |
131 | + if (action && action !== "save") { | |
132 | + return badRequest("Unknown action"); | |
133 | + } | |
134 | + | |
135 | + let config: StorageConfig; | |
136 | + try { | |
137 | + config = normalizeConfig(bodyConfig); | |
138 | + } catch (error: any) { | |
139 | + return badRequest(error?.message || "Invalid storage config"); | |
80 | 140 | } |
81 | 141 | |
82 | 142 | await updateStorageConfig(config, user.id); |
83 | 143 | |