nca-ai-cms-astro-plugin 1.0.16 → 1.0.17

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nca-ai-cms-astro-plugin",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -0,0 +1,35 @@
1
+ import type { APIRoute } from 'astro';
2
+ import * as fs from 'fs/promises';
3
+ import * as path from 'path';
4
+ import { jsonError } from '../_utils';
5
+
6
+ function getDbPath(): string {
7
+ const envPath = process.env.ASTRO_DATABASE_FILE;
8
+ if (envPath) {
9
+ const resolved = path.resolve(process.cwd(), envPath);
10
+ if (!resolved.startsWith(process.cwd())) {
11
+ throw new Error('Invalid database path');
12
+ }
13
+ return resolved;
14
+ }
15
+ return path.join(process.cwd(), '.astro', 'content.db');
16
+ }
17
+
18
+ export const GET: APIRoute = async () => {
19
+ try {
20
+ const dbPath = getDbPath();
21
+ const dbBuffer = await fs.readFile(dbPath);
22
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
23
+
24
+ return new Response(dbBuffer, {
25
+ status: 200,
26
+ headers: {
27
+ 'Content-Type': 'application/x-sqlite3',
28
+ 'Content-Disposition': `attachment; filename="content-${timestamp}.db"`,
29
+ 'Content-Length': String(dbBuffer.length),
30
+ },
31
+ });
32
+ } catch {
33
+ return jsonError('Database file not found', 404);
34
+ }
35
+ };
@@ -0,0 +1,74 @@
1
+ import type { APIRoute } from 'astro';
2
+ import * as fs from 'fs/promises';
3
+ import * as path from 'path';
4
+ import { jsonResponse, jsonError } from '../_utils';
5
+
6
+ const MAX_DB_SIZE = 50 * 1024 * 1024; // 50 MB
7
+
8
+ function getDbPath(): string {
9
+ const envPath = process.env.ASTRO_DATABASE_FILE;
10
+ if (envPath) {
11
+ const resolved = path.resolve(process.cwd(), envPath);
12
+ if (!resolved.startsWith(process.cwd())) {
13
+ throw new Error('Invalid database path');
14
+ }
15
+ return resolved;
16
+ }
17
+ return path.join(process.cwd(), '.astro', 'content.db');
18
+ }
19
+
20
+ export const POST: APIRoute = async ({ request }) => {
21
+ try {
22
+ const dbPath = getDbPath();
23
+ const contentType = request.headers.get('content-type') || '';
24
+
25
+ let dbBuffer: Buffer;
26
+
27
+ if (contentType.includes('multipart/form-data')) {
28
+ const formData = await request.formData();
29
+ const file = formData.get('database') as File | null;
30
+
31
+ if (!file) {
32
+ return jsonError('No database file provided. Use field name "database".', 400);
33
+ }
34
+
35
+ if (file.size > MAX_DB_SIZE) {
36
+ return jsonError('File too large. Maximum 50 MB.', 413);
37
+ }
38
+
39
+ const arrayBuffer = await file.arrayBuffer();
40
+ dbBuffer = Buffer.from(arrayBuffer);
41
+ } else if (contentType.includes('application/octet-stream')) {
42
+ const arrayBuffer = await request.arrayBuffer();
43
+ dbBuffer = Buffer.from(arrayBuffer);
44
+
45
+ if (dbBuffer.length > MAX_DB_SIZE) {
46
+ return jsonError('File too large. Maximum 50 MB.', 413);
47
+ }
48
+ } else {
49
+ return jsonError('Invalid content type. Use multipart/form-data or application/octet-stream.', 400);
50
+ }
51
+
52
+ // SQLite validation
53
+ const header = dbBuffer.subarray(0, 16).toString('utf8');
54
+ if (!header.startsWith('SQLite format 3')) {
55
+ return jsonError('Invalid file: not a SQLite database.', 400);
56
+ }
57
+
58
+ // Backup current DB before overwriting
59
+ try {
60
+ await fs.copyFile(dbPath, `${dbPath}.backup`);
61
+ } catch {
62
+ // No existing DB to backup
63
+ }
64
+
65
+ await fs.writeFile(dbPath, dbBuffer);
66
+
67
+ return jsonResponse({
68
+ success: true,
69
+ size: dbBuffer.length,
70
+ });
71
+ } catch {
72
+ return jsonError('Database upload failed', 500);
73
+ }
74
+ };
@@ -289,6 +289,50 @@ export function SettingsTab() {
289
289
  ))}
290
290
  </div>
291
291
 
292
+ {/* Database Management — only on Website tab */}
293
+ {activeSubTab === 'website' && (
294
+ <div style={{ ...styles.plannerForm, flexDirection: 'row', alignItems: 'center' }}>
295
+ <span style={{ ...styles.label, marginRight: 'auto' }}>Datenbank-Verwaltung</span>
296
+ <a
297
+ href="/api/db/download"
298
+ style={{ ...styles.editButton, textDecoration: 'none', display: 'inline-flex', alignItems: 'center', gap: '0.4rem' }}
299
+ >
300
+ ↓ DB herunterladen
301
+ </a>
302
+ <label style={{ ...styles.editButton, cursor: 'pointer', display: 'inline-flex', alignItems: 'center', gap: '0.4rem', margin: 0 }}>
303
+ ↑ DB hochladen
304
+ <input
305
+ type="file"
306
+ accept=".db,.sqlite,.sqlite3"
307
+ style={styles.srOnly}
308
+ onChange={async (e) => {
309
+ const file = e.target.files?.[0];
310
+ if (!file) return;
311
+ if (!confirm(`Datenbank "${file.name}" hochladen? Die aktuelle DB wird ueberschrieben.`)) {
312
+ e.target.value = '';
313
+ return;
314
+ }
315
+ const formData = new FormData();
316
+ formData.append('database', file);
317
+ try {
318
+ const res = await fetch('/api/db/upload', { method: 'POST', body: formData });
319
+ const data = await res.json();
320
+ if (res.ok) {
321
+ alert('Datenbank hochgeladen. Seite wird neu geladen.');
322
+ window.location.reload();
323
+ } else {
324
+ alert('Fehler: ' + (data.error || 'Upload fehlgeschlagen'));
325
+ }
326
+ } catch {
327
+ alert('Upload fehlgeschlagen');
328
+ }
329
+ e.target.value = '';
330
+ }}
331
+ />
332
+ </label>
333
+ </div>
334
+ )}
335
+
292
336
  {loading && (
293
337
  <div style={styles.loadingBox}>Einstellungen werden geladen...</div>
294
338
  )}
package/src/index.ts CHANGED
@@ -181,6 +181,18 @@ export default function ncaAiCms(
181
181
  prerender: false,
182
182
  });
183
183
 
184
+ // Inject DB management routes (auth-protected via middleware)
185
+ injectRoute({
186
+ pattern: '/api/db/download',
187
+ entrypoint: 'nca-ai-cms-astro-plugin/api/db/download.ts',
188
+ prerender: false,
189
+ });
190
+ injectRoute({
191
+ pattern: '/api/db/upload',
192
+ entrypoint: 'nca-ai-cms-astro-plugin/api/db/upload.ts',
193
+ prerender: false,
194
+ });
195
+
184
196
  // Inject auth routes
185
197
  injectRoute({
186
198
  pattern: '/api/auth/login',
package/update.md CHANGED
@@ -1,3 +1,17 @@
1
+ # v1.0.17
2
+
3
+ ## Feature: Database download/upload via Editor UI
4
+ - New GET `/api/db/download` endpoint — exports SQLite database as file download with timestamp
5
+ - New POST `/api/db/upload` endpoint — imports SQLite database with validation and backup
6
+ - Download/Upload buttons in Editor → Einstellungen → Website tab
7
+ - Path traversal protection: database path validated against project root
8
+ - File size limit: 50 MB max upload
9
+ - SQLite header validation on upload (rejects non-SQLite files)
10
+ - Automatic backup of current DB before overwrite (`content.db.backup`)
11
+ - Both routes auth-protected via existing middleware
12
+
13
+ ---
14
+
1
15
  # v1.0.16
2
16
 
3
17
  ## Feature: Pages content type with flat URL structure