diff --git a/.dockerignore b/.dockerignore index 273e032..63456cd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -60,3 +60,8 @@ docker-compose*.yml client_secret*.json client_secrets*.json + +# Server-only persistent data and recovery files. +/backups/ +/storage/ +/operations/ diff --git a/.gitignore b/.gitignore index 79d59f1..0614abd 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,8 @@ instance/ # Google OAuth client downloads must never be committed. client_secret*.json client_secrets*.json + +# Server-only persistent data and recovery files. +/backups/ +/storage/ +/operations/ diff --git a/main.py b/main.py index bd1cdf8..41afead 100644 --- a/main.py +++ b/main.py @@ -159,6 +159,9 @@ def main(): @app.route('/studio') def studio(): """Serve the dedicated Video Studio page (music downloader).""" + # Establish identity before the page starts parallel API requests. + youtube_uploads.owner() + youtube_uploads.csrf() return render_template("video.html", title_fonts=_available_fonts()) @@ -1675,7 +1678,7 @@ def make_video() -> tuple[object, int]: youtube_body = None if data.get('youtube') is not None: if not youtube_uploads.valid_csrf(): - return jsonify(error='Invalid YouTube request token. Reload the page.'), 403 + return jsonify(code='youtube_csrf_invalid', error='Invalid YouTube request token. Reload the page.'), 403 try: youtube_body = youtube_uploads.check_ready(data['youtube']) except ValueError as error: diff --git a/static/youtube-upload.js b/static/youtube-upload.js index 6534c69..939448b 100644 --- a/static/youtube-upload.js +++ b/static/youtube-upload.js @@ -18,23 +18,50 @@ function youtubeSettings() { return settings; } -async function youtubeRequest(url, options = {}) { - const response = await fetch(url, {...options, headers: {...options.headers, 'X-YouTube-CSRF': youtubeCsrf}, cache: 'no-store'}); - const result = await response.json(); - if (!response.ok) throw new Error(result.error || 'YouTube request failed.'); - return result; -} +let youtubeConnectionRequest = null; async function loadYouTubeConnection() { - try { - const result = await youtubeRequest('/api/youtube/status'); + // Share one request so simultaneous actions cannot race session setup. + if (youtubeConnectionRequest) return youtubeConnectionRequest; + youtubeConnectionRequest = (async () => { + const response = await fetch('/api/youtube/status', {cache: 'no-store'}); + const result = await response.json(); + if (!response.ok) throw new Error(result.error || 'Could not check YouTube connection.'); youtubeCsrf = result.csrf_token; youtubeConnected = result.connected; ytElement('youtubeConnection').textContent = result.connected ? 'YouTube account connected.' : 'YouTube account not connected.'; ytElement('youtubeRedirect').textContent = result.redirect_uri; - } catch (error) { + return result; + })(); + try { return await youtubeConnectionRequest; } + catch (error) { ytElement('youtubeConnection').textContent = error.message; + throw error; + } finally { youtubeConnectionRequest = null; } +} + +async function youtubeFetch(url, options = {}) { + const mutating = (options.method || 'GET').toUpperCase() === 'POST'; + // Refresh before every mutation, including actions from older open tabs. + if (mutating) await loadYouTubeConnection(); + const send = () => fetch(url, {...options, headers: {...options.headers, 'X-YouTube-CSRF': youtubeCsrf}, cache: 'no-store'}); + let response = await send(); + if (mutating && response.status === 403) { + const rejection = await response.clone().json().catch(() => ({})); + // This response is issued before any upload/render/OAuth side effect. + if (rejection.code === 'youtube_csrf_invalid') { + await loadYouTubeConnection(); + response = await send(); + } } + return response; +} + +async function youtubeRequest(url, options = {}) { + const response = await youtubeFetch(url, options); + const result = await response.json(); + if (!response.ok) throw new Error(result.error || 'YouTube request failed.'); + return result; } async function connectYouTube() { @@ -125,5 +152,5 @@ document.addEventListener('DOMContentLoaded', () => { if (outcome !== 'connected') showToast(outcome === 'denied' ? 'Google authorization cancelled.' : 'Google authorization failed. Check the redirect URI and try connecting again.', 'error'); history.replaceState(null, '', location.pathname); } - loadYouTubeConnection(); + loadYouTubeConnection().catch(() => {}); }); diff --git a/templates/video.html b/templates/video.html index a478cd8..76ff783 100644 --- a/templates/video.html +++ b/templates/video.html @@ -4293,7 +4293,7 @@