mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-16 09:13:42 +08:00
feat: Synchronize environment-provided subscription sources with user settings.
This commit is contained in:
@@ -8,10 +8,12 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
export const runtime = 'edge';
|
||||
|
||||
const ACCESS_PASSWORD = process.env.ACCESS_PASSWORD || '';
|
||||
const SUBSCRIPTION_SOURCES = process.env.SUBSCRIPTION_SOURCES || process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES || '';
|
||||
|
||||
export async function GET() {
|
||||
return NextResponse.json({
|
||||
hasEnvPassword: ACCESS_PASSWORD.length > 0,
|
||||
subscriptionSources: SUBSCRIPTION_SOURCES,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,11 @@ export function PasswordGate({ children }: { children: React.ReactNode }) {
|
||||
const res = await fetch('/api/config');
|
||||
const data = await res.json();
|
||||
setHasEnvPassword(data.hasEnvPassword);
|
||||
|
||||
// Sync subscription sources if provided by environment
|
||||
if (data.subscriptionSources) {
|
||||
settingsStore.syncEnvSubscriptions(data.subscriptionSources);
|
||||
}
|
||||
} catch {
|
||||
// Silently fail - env password not available
|
||||
}
|
||||
|
||||
@@ -44,12 +44,8 @@ export const getDefaultAdultSources = (): VideoSource[] => ADULT_SOURCES;
|
||||
|
||||
|
||||
|
||||
function getEnvSubscriptions(): SourceSubscription[] {
|
||||
if (typeof process === 'undefined' || !process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const envValue = process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES.trim();
|
||||
function getEnvSubscriptions(customValue?: string): SourceSubscription[] {
|
||||
const envValue = (customValue || process.env.SUBSCRIPTION_SOURCES || process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES || '').trim();
|
||||
if (!envValue) return [];
|
||||
|
||||
// 1. Try JSON
|
||||
@@ -231,6 +227,43 @@ export const settingsStore = {
|
||||
return importSettings(jsonString, (s) => this.saveSettings(s), this.getSettings());
|
||||
},
|
||||
|
||||
syncEnvSubscriptions(rawEnvValue: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
const currentSettings = this.getSettings();
|
||||
const envSubs = getEnvSubscriptions(rawEnvValue);
|
||||
|
||||
if (envSubs.length === 0) return;
|
||||
|
||||
const mergedSubscriptions = [...currentSettings.subscriptions];
|
||||
let changed = false;
|
||||
|
||||
envSubs.forEach(envSub => {
|
||||
const existingIndex = mergedSubscriptions.findIndex(s => s.url === envSub.url);
|
||||
if (existingIndex > -1) {
|
||||
// Only update if something meaningful changed to avoid unnecessary re-renders
|
||||
if (mergedSubscriptions[existingIndex].name !== envSub.name) {
|
||||
mergedSubscriptions[existingIndex] = {
|
||||
...mergedSubscriptions[existingIndex],
|
||||
name: envSub.name,
|
||||
autoRefresh: true
|
||||
};
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
mergedSubscriptions.push(envSub);
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (changed) {
|
||||
this.saveSettings({
|
||||
...currentSettings,
|
||||
subscriptions: mergedSubscriptions
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
resetToDefaults(): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem(SETTINGS_KEY);
|
||||
|
||||
Reference in New Issue
Block a user