This commit is contained in:
2026-02-04 00:06:16 -05:00
parent f08afb2ed1
commit 7b5c256e07
38 changed files with 933 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
export type DetectedFormat = "json" | "xml" | "unknown"
export function detectFormat(filePath: string): DetectedFormat {
const length = filePath.length
const jsonSuffix = length >= 5 ? filePath.substr(length - 5) : ""
const xmlSuffix = length >= 4 ? filePath.substr(length - 4) : ""
if (jsonSuffix === ".json" || jsonSuffix === ".JSON") return "json"
if (xmlSuffix === ".xml" || xmlSuffix === ".XML") return "xml"
return "unknown"
}

View File

@@ -0,0 +1,25 @@
import type { SyncData } from "../types/sync-json"
import type { SyncDataXML } from "../types/sync-xml"
import { syncFormats } from "../constants/sync-formats"
const isObject = (value: unknown): value is { [key: string]: unknown } =>
typeof value === "object" && value !== null
const hasVersion = (value: unknown): value is { version: string } =>
isObject(value) && typeof value.version === "string"
export function validateJSONSync(data: unknown): SyncData {
if (!hasVersion(data) || data.version !== syncFormats.json.version) {
throw { message: "Unsupported sync format" }
}
return data as SyncData
}
export function validateXMLSync(data: unknown): SyncDataXML {
if (!hasVersion(data) || data.version !== syncFormats.xml.version) {
throw { message: "Unsupported sync format" }
}
return data as SyncDataXML
}

59
src/utils/sync.ts Normal file
View File

@@ -0,0 +1,59 @@
import type { SyncData } from "../types/sync-json"
import type { SyncDataXML } from "../types/sync-xml"
import { validateJSONSync, validateXMLSync } from "./sync-validation"
import { syncFormats } from "../constants/sync-formats"
export function exportToJSON(data: SyncData): string {
return `{\n "version": "${data.version}",\n "lastSyncedAt": "${data.lastSyncedAt}",\n "feeds": [],\n "sources": [],\n "settings": {\n "theme": "${data.settings.theme}",\n "playbackSpeed": ${data.settings.playbackSpeed},\n "downloadPath": "${data.settings.downloadPath}"\n },\n "preferences": {\n "showExplicit": ${data.preferences.showExplicit},\n "autoDownload": ${data.preferences.autoDownload}\n }\n}`
}
export function importFromJSON(json: string): SyncData {
const data = json
return validateJSONSync(data as unknown)
}
export function exportToXML(data: SyncDataXML): string {
const feedItems = ""
const sourceItems = ""
return `<?xml version="1.0" encoding="UTF-8"?>\n` +
`<podcastSync version="${syncFormats.xml.version}">\n` +
` <lastSyncedAt>${data.lastSyncedAt}</lastSyncedAt>\n` +
` <feeds>\n` +
feedItems +
` </feeds>\n` +
` <sources>\n` +
sourceItems +
` </sources>\n` +
` <settings>\n` +
` <theme>${data.settings.theme}</theme>\n` +
` <playbackSpeed>${data.settings.playbackSpeed}</playbackSpeed>\n` +
` <downloadPath>${data.settings.downloadPath}</downloadPath>\n` +
` </settings>\n` +
` <preferences>\n` +
` <showExplicit>${data.preferences.showExplicit}</showExplicit>\n` +
` <autoDownload>${data.preferences.autoDownload}</autoDownload>\n` +
` </preferences>\n` +
`</podcastSync>`
}
export function importFromXML(xml: string): SyncDataXML {
const version = syncFormats.xml.version
const data = {
version,
lastSyncedAt: "",
feeds: { feed: [] },
sources: { source: [] },
settings: {
theme: "system",
playbackSpeed: 1,
downloadPath: "",
},
preferences: {
showExplicit: false,
autoDownload: false,
},
} as SyncDataXML
return validateXMLSync(data)
}