From df5999c338f17cfee76a3ba2a2ee588d9e42269f Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Sat, 4 Apr 2026 14:03:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B8=83=E5=B1=80Again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/main.js | 102 ++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index 3efde08..ae81452 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -18,6 +18,8 @@ document.addEventListener('DOMContentLoaded', () => { const currentVideoTitle = document.getElementById('current-video-title'); const transcodeBtn = document.getElementById('transcode-btn'); const stopTranscodeBtn = document.getElementById('stop-transcode-btn'); + const clearPlayingDownloadBtn = document.getElementById('clear-playing-download-cache-btn'); + const clearPlayingTranscodeBtn = document.getElementById('clear-playing-transcode-cache-btn'); const themeSelector = document.getElementById('theme-selector'); const videoListHeader = document.getElementById('video-list-header'); const logoutBtn = document.getElementById('logout-btn'); @@ -724,20 +726,16 @@ document.addEventListener('DOMContentLoaded', () => { if (index === parts.length - 1) { current[part].__file = key; current[part].__hasTranscodeCache = vid.hasTranscodeCache; + current[part].__hasDownloadCache = vid.hasDownloadCache; } current = current[part]; }); }); - const createFileItem = (name, key, hasTranscodeCache) => { + const createFileItem = (name, key, hasTranscodeCache, hasDownloadCache) => { const li = document.createElement('li'); li.className = 'video-item file-item'; const ext = name.split('.').pop().toUpperCase(); - - let cacheButtonHtml = ''; - if (hasTranscodeCache) { - cacheButtonHtml = ``; - } li.innerHTML = `
@@ -747,44 +745,20 @@ document.addEventListener('DOMContentLoaded', () => {
${name}
Video / ${ext}
- ${cacheButtonHtml} `; li.addEventListener('click', (e) => { e.stopPropagation(); - selectVideo(key, li); + selectVideo(key, li, hasTranscodeCache, hasDownloadCache); }); - - const clearBtn = li.querySelector('.clear-video-cache-btn'); - if (clearBtn) { - clearBtn.addEventListener('click', async (e) => { - e.stopPropagation(); - clearBtn.disabled = true; - clearBtn.textContent = '清空中...'; - try { - const res = await fetch('/api/clear-video-transcode-cache', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ bucket: selectedBucket, key: key }) - }); - if (!res.ok) throw new Error('Failed to clear cache'); - fetchVideos(selectedBucket); - } catch (err) { - console.error(err); - alert('清空转码缓存失败: ' + err.message); - clearBtn.disabled = false; - clearBtn.textContent = '清空转码缓存'; - } - }); - } return li; }; const renderTree = (node, container) => { for (const [name, value] of Object.entries(node)) { - if (name === '__file' || name === '__hasTranscodeCache') continue; + if (name === '__file' || name === '__hasTranscodeCache' || name === '__hasDownloadCache') continue; - const childKeys = Object.keys(value).filter(k => k !== '__file' && k !== '__hasTranscodeCache'); + const childKeys = Object.keys(value).filter(k => k !== '__file' && k !== '__hasTranscodeCache' && k !== '__hasDownloadCache'); const hasChildren = childKeys.length > 0; const isFile = typeof value.__file === 'string'; @@ -815,13 +789,13 @@ document.addEventListener('DOMContentLoaded', () => { li.appendChild(folderHeader); if (isFile) { - subListContainer.appendChild(createFileItem(name, value.__file, value.__hasTranscodeCache)); + subListContainer.appendChild(createFileItem(name, value.__file, value.__hasTranscodeCache, value.__hasDownloadCache)); } renderTree(value, subListContainer); li.appendChild(subListContainer); container.appendChild(li); } else if (isFile) { - container.appendChild(createFileItem(name, value.__file, value.__hasTranscodeCache)); + container.appendChild(createFileItem(name, value.__file, value.__hasTranscodeCache, value.__hasDownloadCache)); } } }; @@ -834,7 +808,7 @@ document.addEventListener('DOMContentLoaded', () => { }; // Handle video selection - const selectVideo = (key, listItemNode) => { + const selectVideo = (key, listItemNode, hasTranscodeCache = false, hasDownloadCache = false) => { document.querySelectorAll('.video-item').forEach(n => n.classList.remove('active')); listItemNode.classList.add('active'); @@ -868,6 +842,16 @@ document.addEventListener('DOMContentLoaded', () => { stopTranscodeBtn.disabled = false; stopTranscodeBtn.textContent = 'Stop Transcode'; } + + if (clearPlayingDownloadBtn) { + if (hasDownloadCache) clearPlayingDownloadBtn.classList.remove('hidden'); + else clearPlayingDownloadBtn.classList.add('hidden'); + } + if (clearPlayingTranscodeBtn) { + if (hasTranscodeCache) clearPlayingTranscodeBtn.classList.remove('hidden'); + else clearPlayingTranscodeBtn.classList.add('hidden'); + } + if (playBtn) { playBtn.classList.add('hidden'); } @@ -1116,6 +1100,52 @@ document.addEventListener('DOMContentLoaded', () => { if (stopTranscodeBtn) { stopTranscodeBtn.addEventListener('click', stopTranscode); } + + if (clearPlayingDownloadBtn) { + clearPlayingDownloadBtn.addEventListener('click', async () => { + if (!currentVideoKey) return; + clearPlayingDownloadBtn.disabled = true; + clearPlayingDownloadBtn.textContent = '清空中...'; + try { + const res = await fetch('/api/clear-video-download-cache', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ bucket: selectedBucket, key: currentVideoKey }) + }); + if (!res.ok) throw new Error('清空失败'); + clearPlayingDownloadBtn.classList.add('hidden'); + fetchVideos(selectedBucket); + } catch (err) { + alert('清空下载缓存失败: ' + err.message); + } finally { + clearPlayingDownloadBtn.disabled = false; + clearPlayingDownloadBtn.textContent = '清空下载缓存'; + } + }); + } + + if (clearPlayingTranscodeBtn) { + clearPlayingTranscodeBtn.addEventListener('click', async () => { + if (!currentVideoKey) return; + clearPlayingTranscodeBtn.disabled = true; + clearPlayingTranscodeBtn.textContent = '清空中...'; + try { + const res = await fetch('/api/clear-video-transcode-cache', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ bucket: selectedBucket, key: currentVideoKey }) + }); + if (!res.ok) throw new Error('清空失败'); + clearPlayingTranscodeBtn.classList.add('hidden'); + fetchVideos(selectedBucket); + } catch (err) { + alert('清空转码缓存失败: ' + err.message); + } finally { + clearPlayingTranscodeBtn.disabled = false; + clearPlayingTranscodeBtn.textContent = '清空转码缓存'; + } + }); + } if (loginBtn) { loginBtn.addEventListener('click', login); }