mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
feat: import and download flow
This commit is contained in:
1 parent
0a308fa716
commit
ab5d168c58
3 files changed
+34
-207
No files matched your search
@@ -104,197 +104,3 @@ export type ParsedRecords = {
|
||||
ALIAS: Partial<DnsRecord>[];
|
||||
[key: string]: Partial<DnsRecord>[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse DNS zone file content and extract DNS records
|
||||
* @param content Zone file content as string
|
||||
* @returns Object with arrays of records grouped by type
|
||||
*/
|
||||
export function parseDnsRecords(content: string): Record<string, Partial<DnsRecord>[]> {
|
||||
// Initialize records object with empty arrays for each record type
|
||||
const records: Record<string, Partial<DnsRecord>[]> = {
|
||||
A: [],
|
||||
AAAA: [],
|
||||
CNAME: [],
|
||||
MX: [],
|
||||
TXT: [],
|
||||
NS: [],
|
||||
SRV: [],
|
||||
CAA: [],
|
||||
PTR: [],
|
||||
HTTPS: [],
|
||||
ALIAS: []
|
||||
};
|
||||
|
||||
if (!content || content.trim() === '') {
|
||||
return records;
|
||||
}
|
||||
|
||||
// Default TTL value
|
||||
let defaultTTL = 3600;
|
||||
// Origin for the zone
|
||||
let origin = '';
|
||||
|
||||
// Process the content line by line
|
||||
const lines = content.split('\n');
|
||||
|
||||
// Process each line
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let line = lines[i].trim();
|
||||
|
||||
// Skip empty lines
|
||||
if (line === '') continue;
|
||||
|
||||
// Skip comment lines
|
||||
if (line.startsWith(';')) continue;
|
||||
|
||||
// Skip newline characters
|
||||
if (line === '\n') continue;
|
||||
|
||||
// Remove inline comments
|
||||
const commentIndex = line.indexOf(';');
|
||||
if (commentIndex !== -1) {
|
||||
line = line.substring(0, commentIndex).trim();
|
||||
}
|
||||
|
||||
// Handle directives
|
||||
if (line.startsWith('$')) {
|
||||
if (line.startsWith('$ORIGIN')) {
|
||||
origin = line.split(/\s+/)[1].replace(/\.$/, '');
|
||||
} else if (line.startsWith('$TTL')) {
|
||||
defaultTTL = parseInt(line.split(/\s+/)[1], 10);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip SOA record which spans multiple lines
|
||||
if (line.includes('SOA') || line.includes('(')) {
|
||||
// Skip until we find the closing parenthesis
|
||||
while (i < lines.length && !lines[i].includes(')')) {
|
||||
i++;
|
||||
}
|
||||
// Skip one more line if it contains the closing parenthesis
|
||||
if (i < lines.length && lines[i].includes(')')) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the record
|
||||
try {
|
||||
const record = parseLine(line, defaultTTL, origin, Object.keys(records));
|
||||
if (record && record.type && records[record.type]) {
|
||||
records[record.type].push(record);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing line:', line, error);
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
function parseLine(
|
||||
line: string,
|
||||
defaultTTL: number,
|
||||
origin: string,
|
||||
validTypes: string[]
|
||||
): Partial<DnsRecord> | null {
|
||||
// Skip invalid lines
|
||||
if (!line || line.trim() === '') return null;
|
||||
|
||||
// Split the line by whitespace, preserving quoted strings
|
||||
const parts: string[] = [];
|
||||
let currentPart = '';
|
||||
let inQuotes = false;
|
||||
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
const char = line[i];
|
||||
|
||||
if (char === '"' && (i === 0 || line[i - 1] !== '\\')) {
|
||||
inQuotes = !inQuotes;
|
||||
currentPart += char;
|
||||
} else if (!inQuotes && /\s/.test(char)) {
|
||||
if (currentPart) {
|
||||
parts.push(currentPart);
|
||||
currentPart = '';
|
||||
}
|
||||
} else {
|
||||
currentPart += char;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentPart) {
|
||||
parts.push(currentPart);
|
||||
}
|
||||
|
||||
// Filter out empty parts
|
||||
const filteredParts = parts.filter((part) => part !== '');
|
||||
if (filteredParts.length < 2) return null;
|
||||
|
||||
let recordName = filteredParts[0];
|
||||
let type = '';
|
||||
let value = '';
|
||||
let ttl = defaultTTL;
|
||||
let priority = 0;
|
||||
let position = 1;
|
||||
|
||||
// Handle special case for entries starting with IN (for complex zone file with multiple servers)
|
||||
if (recordName === 'IN') {
|
||||
recordName = '';
|
||||
}
|
||||
|
||||
// Clean trailing dots from domain names
|
||||
recordName = recordName.replace(/\.$/, '');
|
||||
|
||||
// Check if the second part is a TTL
|
||||
if (/^\d+$/.test(filteredParts[position])) {
|
||||
ttl = parseInt(filteredParts[position], 10);
|
||||
position++;
|
||||
}
|
||||
|
||||
// Check if the next part is the record class (IN)
|
||||
if (filteredParts[position] === 'IN') {
|
||||
position++;
|
||||
}
|
||||
|
||||
// Get the record type
|
||||
if (position < filteredParts.length) {
|
||||
type = filteredParts[position].toUpperCase();
|
||||
position++;
|
||||
}
|
||||
|
||||
// Ensure we have a valid record type
|
||||
if (!type || !validTypes.includes(type)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle MX records which have priority
|
||||
if (type === 'MX' && position < filteredParts.length) {
|
||||
priority = parseInt(filteredParts[position], 10);
|
||||
position++;
|
||||
}
|
||||
|
||||
// Get the record value
|
||||
if (position < filteredParts.length) {
|
||||
// For quoted strings, remove the quotes
|
||||
let recordValue = filteredParts[position];
|
||||
if (recordValue.startsWith('"') && recordValue.endsWith('"')) {
|
||||
recordValue = recordValue.substring(1, recordValue.length - 1);
|
||||
}
|
||||
|
||||
// Remove trailing dot for domain names
|
||||
value = recordValue.replace(/\.$/, '');
|
||||
}
|
||||
|
||||
// Create and return the record
|
||||
return {
|
||||
type,
|
||||
name: recordName,
|
||||
value,
|
||||
ttl,
|
||||
priority,
|
||||
weight: 0,
|
||||
port: 0,
|
||||
comment: ''
|
||||
};
|
||||
}
|
||||
+31
-1
@@ -23,6 +23,8 @@
|
||||
import Table from './table.svelte';
|
||||
import AddPresetModal from './addPresetModal.svelte';
|
||||
import ImportRecordModal from './importRecordModal.svelte';
|
||||
import { sdk } from '$lib/stores/sdk';
|
||||
import { addNotification } from '$lib/stores/notifications';
|
||||
|
||||
export let data;
|
||||
|
||||
@@ -30,6 +32,34 @@
|
||||
let showPresetModal = false;
|
||||
let showImportModal = false;
|
||||
let selectedPreset = '';
|
||||
|
||||
async function downloadRecords() {
|
||||
try {
|
||||
const zone = await sdk.forConsole.domains.getZone(data.domain.$id);
|
||||
|
||||
if ('message' in zone) {
|
||||
const blob = new Blob([zone.message as string], { type: 'text/plain' });
|
||||
|
||||
// Create download link
|
||||
const downloadLink = document.createElement('a');
|
||||
downloadLink.href = URL.createObjectURL(blob);
|
||||
downloadLink.download = `${data.domain.domain}.txt`;
|
||||
|
||||
// Trigger download and clean up
|
||||
document.body.appendChild(downloadLink);
|
||||
downloadLink.click();
|
||||
document.body.removeChild(downloadLink);
|
||||
URL.revokeObjectURL(downloadLink.href);
|
||||
} else {
|
||||
throw new Error('Failed to download records');
|
||||
}
|
||||
} catch (error) {
|
||||
addNotification({
|
||||
type: 'error',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<Container>
|
||||
@@ -45,7 +75,7 @@
|
||||
Import zone file
|
||||
</Button>
|
||||
<Tooltip>
|
||||
<PinkButton.Button variant="secondary" icon>
|
||||
<PinkButton.Button variant="secondary" icon on:click={downloadRecords}>
|
||||
<Icon icon={IconDownload} size="s" />
|
||||
</PinkButton.Button>
|
||||
<svelte:fragment slot="tooltip">Export as .txt</svelte:fragment>
|
||||
|
||||
+3
-12
@@ -7,8 +7,9 @@
|
||||
import { IconInfo } from '@appwrite.io/pink-icons-svelte';
|
||||
import { removeFile } from '$lib/helpers/files';
|
||||
import { page } from '$app/state';
|
||||
import { createRecord, parseDnsRecords } from '$lib/helpers/domains';
|
||||
import { sdk } from '$lib/stores/sdk';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { Dependencies } from '$lib/constants';
|
||||
|
||||
export let show = false;
|
||||
let files: FileList;
|
||||
@@ -20,19 +21,9 @@
|
||||
|
||||
const file = files[0];
|
||||
const content = await file.text();
|
||||
// const parsedRecords = parseDnsRecords(content);
|
||||
|
||||
// let recordCount = 0;
|
||||
|
||||
// for (const [type, records] of Object.entries(parsedRecords)) {
|
||||
// recordCount += records.length;
|
||||
|
||||
// for (const record of records) {
|
||||
// await createRecord(record, page.params.domain);
|
||||
// }
|
||||
// }
|
||||
|
||||
await sdk.forConsole.domains.updateZone(page.params.domain, content);
|
||||
invalidate(Dependencies.DOMAIN);
|
||||
show = false;
|
||||
addNotification({
|
||||
type: 'success',
|
||||
|
||||
Reference in new issue
Block a user