Initial commit, plex-like web media server
This commit is contained in:
commit
c9f51869cf
3 changed files with 852 additions and 0 deletions
102
get_files.php
Normal file
102
get_files.php
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
/**
|
||||
* get_files.php - Recursively scans media directories and returns file structure as JSON
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Get the media type from query parameter
|
||||
$type = isset($_GET['type']) ? $_GET['type'] : 'videos';
|
||||
|
||||
// Define base directories
|
||||
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: '/home/' . get_current_user());
|
||||
$baseDir = $homeDir . '/GDrive';
|
||||
|
||||
$directories = [
|
||||
'videos' => $baseDir . '/Videos',
|
||||
'music' => $baseDir . '/Music'
|
||||
];
|
||||
|
||||
// Validate type
|
||||
if (!isset($directories[$type])) {
|
||||
echo json_encode(['error' => 'Invalid media type']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$targetDir = $directories[$type];
|
||||
|
||||
// Check if directory exists
|
||||
if (!is_dir($targetDir)) {
|
||||
echo json_encode(['error' => 'Directory not found', 'path' => $targetDir]);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively scan directory and build file tree
|
||||
* @param string $dir Directory to scan
|
||||
* @param string $baseDir Base directory for relative paths
|
||||
* @return array File tree structure
|
||||
*/
|
||||
function scanDirectory($dir, $baseDir) {
|
||||
$result = [];
|
||||
|
||||
// Valid media extensions
|
||||
$videoExtensions = ['mp4', 'mkv', 'avi', 'mov', 'wmv', 'flv', 'webm', 'm4v'];
|
||||
$audioExtensions = ['mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'wma', 'opus'];
|
||||
$validExtensions = array_merge($videoExtensions, $audioExtensions);
|
||||
|
||||
try {
|
||||
$items = scandir($dir);
|
||||
|
||||
foreach ($items as $item) {
|
||||
// Skip hidden files and parent directory references
|
||||
if ($item === '.' || $item === '..' || $item[0] === '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fullPath = $dir . DIRECTORY_SEPARATOR . $item;
|
||||
|
||||
if (is_dir($fullPath)) {
|
||||
// Recursively scan subdirectory
|
||||
$subItems = scanDirectory($fullPath, $baseDir);
|
||||
if (!empty($subItems)) {
|
||||
$result[$item] = $subItems;
|
||||
}
|
||||
} else {
|
||||
// Check if file has valid media extension
|
||||
$extension = strtolower(pathinfo($item, PATHINFO_EXTENSION));
|
||||
if (in_array($extension, $validExtensions)) {
|
||||
// Store relative path from base directory
|
||||
$relativePath = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $fullPath);
|
||||
$result[$item] = $relativePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Handle permission errors gracefully
|
||||
return [];
|
||||
}
|
||||
|
||||
// Sort results: directories first, then files
|
||||
uksort($result, function($a, $b) use ($result) {
|
||||
$aIsDir = is_array($result[$a]);
|
||||
$bIsDir = is_array($result[$b]);
|
||||
|
||||
if ($aIsDir && !$bIsDir) return -1;
|
||||
if (!$aIsDir && $bIsDir) return 1;
|
||||
return strcasecmp($a, $b);
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Scan the directory and return JSON
|
||||
$fileTree = scanDirectory($targetDir, $baseDir);
|
||||
|
||||
if (empty($fileTree)) {
|
||||
echo json_encode(['message' => 'No media files found in this directory']);
|
||||
} else {
|
||||
echo json_encode($fileTree);
|
||||
}
|
||||
?>
|
||||
|
||||
582
index.php
Normal file
582
index.php
Normal file
|
|
@ -0,0 +1,582 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Media Player</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Archivo:wght@300;400;600&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--bg-primary: #0a0e14;
|
||||
--bg-secondary: #151b24;
|
||||
--bg-tertiary: #1f2937;
|
||||
--accent-primary: #00ffcc;
|
||||
--accent-secondary: #ff00ff;
|
||||
--text-primary: #e8edf3;
|
||||
--text-secondary: #8b95a8;
|
||||
--border-color: #2d3748;
|
||||
--shadow-glow: 0 0 20px rgba(0, 255, 204, 0.3);
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Archivo', sans-serif;
|
||||
background: linear-gradient(135deg, var(--bg-primary) 0%, #0d1117 50%, var(--bg-secondary) 100%);
|
||||
color: var(--text-primary);
|
||||
min-height: 100vh;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
body::before {
|
||||
content: '';
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background:
|
||||
radial-gradient(circle at 20% 50%, rgba(0, 255, 204, 0.05) 0%, transparent 50%),
|
||||
radial-gradient(circle at 80% 20%, rgba(255, 0, 255, 0.05) 0%, transparent 50%);
|
||||
pointer-events: none;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.container {
|
||||
position: relative;
|
||||
max-width: 1600px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
header {
|
||||
text-align: center;
|
||||
margin-bottom: 3rem;
|
||||
animation: slideDown 0.8s ease-out;
|
||||
}
|
||||
|
||||
@keyframes slideDown {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-30px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-family: 'Orbitron', sans-serif;
|
||||
font-size: 3.5rem;
|
||||
font-weight: 900;
|
||||
background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-clip: text;
|
||||
letter-spacing: 0.05em;
|
||||
text-transform: uppercase;
|
||||
margin-bottom: 0.5rem;
|
||||
filter: drop-shadow(0 0 30px rgba(0, 255, 204, 0.5));
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.9rem;
|
||||
letter-spacing: 0.2em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.main-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 350px 1fr;
|
||||
gap: 2rem;
|
||||
animation: fadeIn 1s ease-out 0.3s both;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
height: fit-content;
|
||||
position: sticky;
|
||||
top: 2rem;
|
||||
backdrop-filter: blur(10px);
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.media-type-tabs {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.tab-btn {
|
||||
flex: 1;
|
||||
padding: 0.75rem;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border-color);
|
||||
color: var(--text-secondary);
|
||||
font-family: 'Orbitron', sans-serif;
|
||||
font-size: 0.85rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
cursor: pointer;
|
||||
border-radius: 6px;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.tab-btn:hover {
|
||||
background: var(--bg-primary);
|
||||
border-color: var(--accent-primary);
|
||||
color: var(--accent-primary);
|
||||
}
|
||||
|
||||
.tab-btn.active {
|
||||
background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
|
||||
border-color: var(--accent-primary);
|
||||
color: var(--bg-primary);
|
||||
font-weight: 700;
|
||||
box-shadow: var(--shadow-glow);
|
||||
}
|
||||
|
||||
.file-browser {
|
||||
max-height: 600px;
|
||||
overflow-y: auto;
|
||||
padding-right: 0.5rem;
|
||||
}
|
||||
|
||||
.file-browser::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
}
|
||||
|
||||
.file-browser::-webkit-scrollbar-track {
|
||||
background: var(--bg-tertiary);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.file-browser::-webkit-scrollbar-thumb {
|
||||
background: var(--accent-primary);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.folder {
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.folder-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0.6rem 0.8rem;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.folder-header:hover {
|
||||
background: var(--bg-primary);
|
||||
border-color: var(--accent-primary);
|
||||
transform: translateX(3px);
|
||||
}
|
||||
|
||||
.folder-icon {
|
||||
margin-right: 0.5rem;
|
||||
font-size: 1.1rem;
|
||||
transition: transform 0.2s ease;
|
||||
}
|
||||
|
||||
.folder.open .folder-icon {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.folder-content {
|
||||
display: none;
|
||||
padding-left: 1.5rem;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.folder.open .folder-content {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.file-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0.6rem 0.8rem;
|
||||
margin-bottom: 0.3rem;
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid transparent;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.file-item:hover {
|
||||
border-color: var(--accent-primary);
|
||||
background: var(--bg-tertiary);
|
||||
transform: translateX(5px);
|
||||
box-shadow: -3px 0 0 0 var(--accent-primary);
|
||||
}
|
||||
|
||||
.file-item.active {
|
||||
background: linear-gradient(90deg, rgba(0, 255, 204, 0.15) 0%, transparent 100%);
|
||||
border-color: var(--accent-primary);
|
||||
color: var(--accent-primary);
|
||||
box-shadow: -3px 0 0 0 var(--accent-primary);
|
||||
}
|
||||
|
||||
.file-icon {
|
||||
margin-right: 0.5rem;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.player-section {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
padding: 2rem;
|
||||
backdrop-filter: blur(10px);
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.player-container {
|
||||
background: var(--bg-primary);
|
||||
border: 2px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
padding: 2rem;
|
||||
min-height: 400px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.player-container::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: -50%;
|
||||
left: -50%;
|
||||
width: 200%;
|
||||
height: 200%;
|
||||
background: radial-gradient(circle, rgba(0, 255, 204, 0.03) 0%, transparent 70%);
|
||||
animation: pulse 4s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
50% {
|
||||
transform: scale(1.1);
|
||||
opacity: 0.8;
|
||||
}
|
||||
}
|
||||
|
||||
.player-placeholder {
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.player-placeholder-icon {
|
||||
font-size: 4rem;
|
||||
margin-bottom: 1rem;
|
||||
opacity: 0.3;
|
||||
}
|
||||
|
||||
video, audio {
|
||||
max-width: 100%;
|
||||
max-height: 500px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
audio {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.now-playing {
|
||||
margin-top: 1.5rem;
|
||||
padding: 1.5rem;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.now-playing-label {
|
||||
font-size: 0.75rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.1em;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.now-playing-title {
|
||||
font-family: 'Orbitron', sans-serif;
|
||||
font-size: 1.3rem;
|
||||
color: var(--accent-primary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.loading {
|
||||
display: inline-block;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 3px solid var(--border-color);
|
||||
border-top-color: var(--accent-primary);
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
.main-layout {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
position: static;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>Media Player</h1>
|
||||
<div class="subtitle">Your Digital Entertainment Hub</div>
|
||||
</header>
|
||||
|
||||
<div class="main-layout">
|
||||
<aside class="sidebar">
|
||||
<div class="media-type-tabs">
|
||||
<button class="tab-btn active" data-type="videos">Videos</button>
|
||||
<button class="tab-btn" data-type="music">Music</button>
|
||||
</div>
|
||||
<div class="file-browser" id="fileBrowser">
|
||||
<div class="loading"></div>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<main class="player-section">
|
||||
<div class="player-container" id="playerContainer">
|
||||
<div class="player-placeholder">
|
||||
<div class="player-placeholder-icon">🎵</div>
|
||||
<p>Select a file to start playing</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="now-playing" id="nowPlaying" style="display: none;">
|
||||
<div class="now-playing-label">Now Playing</div>
|
||||
<div class="now-playing-title" id="nowPlayingTitle"></div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const state = {
|
||||
currentType: 'videos',
|
||||
currentFile: null,
|
||||
filesData: null
|
||||
};
|
||||
|
||||
// Initialize
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
loadFiles('videos');
|
||||
setupTabListeners();
|
||||
});
|
||||
|
||||
function setupTabListeners() {
|
||||
document.querySelectorAll('.tab-btn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const type = btn.dataset.type;
|
||||
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
|
||||
btn.classList.add('active');
|
||||
state.currentType = type;
|
||||
loadFiles(type);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFiles(type) {
|
||||
const browser = document.getElementById('fileBrowser');
|
||||
browser.innerHTML = '<div class="loading"></div>';
|
||||
|
||||
try {
|
||||
const response = await fetch(`get_files.php?type=${type}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (!contentType || !contentType.includes('application/json')) {
|
||||
const text = await response.text();
|
||||
console.error('Non-JSON response:', text);
|
||||
throw new Error('Server returned non-JSON response');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Loaded files:', data);
|
||||
|
||||
if (data.error) {
|
||||
browser.innerHTML = `<p style="color: #ff6b6b; padding: 1rem;">${data.error}</p>`;
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.message) {
|
||||
browser.innerHTML = `<p style="color: var(--text-secondary); padding: 1rem;">${data.message}</p>`;
|
||||
return;
|
||||
}
|
||||
|
||||
state.filesData = data;
|
||||
renderFileTree(data);
|
||||
} catch (error) {
|
||||
console.error('Error loading files:', error);
|
||||
browser.innerHTML = `<p style="color: #ff6b6b; padding: 1rem;">Error loading files: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderFileTree(files, container = document.getElementById('fileBrowser'), depth = 0) {
|
||||
if (depth === 0) {
|
||||
container.innerHTML = '';
|
||||
}
|
||||
|
||||
for (const [name, content] of Object.entries(files)) {
|
||||
if (typeof content === 'object' && !Array.isArray(content)) {
|
||||
// It's a folder
|
||||
const folder = document.createElement('div');
|
||||
folder.className = 'folder';
|
||||
|
||||
const header = document.createElement('div');
|
||||
header.className = 'folder-header';
|
||||
header.innerHTML = `<span class="folder-icon">▶</span><span>${name}</span>`;
|
||||
|
||||
const folderContent = document.createElement('div');
|
||||
folderContent.className = 'folder-content';
|
||||
|
||||
header.addEventListener('click', () => {
|
||||
folder.classList.toggle('open');
|
||||
});
|
||||
|
||||
folder.appendChild(header);
|
||||
folder.appendChild(folderContent);
|
||||
container.appendChild(folder);
|
||||
|
||||
renderFileTree(content, folderContent, depth + 1);
|
||||
} else if (typeof content === 'string') {
|
||||
// It's a file
|
||||
const fileItem = document.createElement('div');
|
||||
fileItem.className = 'file-item';
|
||||
|
||||
const icon = state.currentType === 'videos' ? '🎬' : '🎵';
|
||||
fileItem.innerHTML = `<span class="file-icon">${icon}</span><span>${name}</span>`;
|
||||
|
||||
fileItem.addEventListener('click', () => {
|
||||
playFile(content, name);
|
||||
document.querySelectorAll('.file-item').forEach(f => f.classList.remove('active'));
|
||||
fileItem.classList.add('active');
|
||||
});
|
||||
|
||||
container.appendChild(fileItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function playFile(path, name) {
|
||||
const playerContainer = document.getElementById('playerContainer');
|
||||
const nowPlaying = document.getElementById('nowPlaying');
|
||||
const nowPlayingTitle = document.getElementById('nowPlayingTitle');
|
||||
|
||||
state.currentFile = { path, name };
|
||||
|
||||
const isVideo = state.currentType === 'videos';
|
||||
const mediaElement = document.createElement(isVideo ? 'video' : 'audio');
|
||||
|
||||
mediaElement.controls = true;
|
||||
mediaElement.autoplay = true;
|
||||
|
||||
// Add error handling
|
||||
mediaElement.addEventListener('error', (e) => {
|
||||
console.error('Media playback error:', {
|
||||
error: mediaElement.error,
|
||||
code: mediaElement.error?.code,
|
||||
message: mediaElement.error?.message,
|
||||
path: path,
|
||||
name: name
|
||||
});
|
||||
|
||||
playerContainer.innerHTML = `
|
||||
<div class="player-placeholder">
|
||||
<div class="player-placeholder-icon">⚠️</div>
|
||||
<p style="color: #ff6b6b;">Error playing: ${name}</p>
|
||||
<p style="color: var(--text-secondary); font-size: 0.8rem; margin-top: 0.5rem;">
|
||||
${mediaElement.error?.message || 'Unknown error'}
|
||||
</p>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
|
||||
// Add loading state
|
||||
mediaElement.addEventListener('loadstart', () => {
|
||||
console.log('Loading media:', name);
|
||||
});
|
||||
|
||||
mediaElement.addEventListener('canplay', () => {
|
||||
console.log('Media ready to play:', name);
|
||||
});
|
||||
|
||||
// URL encode the path properly
|
||||
const encodedPath = encodeURIComponent(path);
|
||||
mediaElement.src = `serve_media.php?file=${encodedPath}`;
|
||||
|
||||
console.log('Playing file:', {
|
||||
name: name,
|
||||
path: path,
|
||||
encodedPath: encodedPath,
|
||||
fullUrl: `serve_media.php?file=${encodedPath}`
|
||||
});
|
||||
|
||||
playerContainer.innerHTML = '';
|
||||
playerContainer.appendChild(mediaElement);
|
||||
|
||||
nowPlayingTitle.textContent = name;
|
||||
nowPlaying.style.display = 'block';
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
168
serve_media.php
Normal file
168
serve_media.php
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
/**
|
||||
* serve_media.php - Serves media files with proper headers for streaming
|
||||
*/
|
||||
|
||||
// Get the requested file path
|
||||
$requestedFile = isset($_GET['file']) ? $_GET['file'] : '';
|
||||
|
||||
if (empty($requestedFile)) {
|
||||
header('HTTP/1.1 400 Bad Request');
|
||||
echo 'No file specified';
|
||||
exit;
|
||||
}
|
||||
|
||||
// IMPORTANT: Set base directory to where media actually lives
|
||||
// Using home directory + GDrive instead of __DIR__
|
||||
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: '/home/' . get_current_user());
|
||||
$baseDir = $homeDir . '/GDrive';
|
||||
|
||||
// Construct full path
|
||||
$filePath = $baseDir . DIRECTORY_SEPARATOR . $requestedFile;
|
||||
|
||||
// Security: Prevent directory traversal attacks
|
||||
$realBase = realpath($baseDir);
|
||||
$realFile = realpath($filePath);
|
||||
|
||||
// Additional debug info (remove in production)
|
||||
if ($realFile === false) {
|
||||
header('HTTP/1.1 404 Not Found');
|
||||
echo 'File not found. Debug info:' . "\n";
|
||||
echo 'Base dir: ' . $baseDir . "\n";
|
||||
echo 'Requested: ' . $requestedFile . "\n";
|
||||
echo 'Full path: ' . $filePath . "\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
if (strpos($realFile, $realBase) !== 0) {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
echo 'Access denied - path outside base directory';
|
||||
exit;
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($filePath) || !is_file($filePath)) {
|
||||
header('HTTP/1.1 404 Not Found');
|
||||
echo 'File not found';
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get file information
|
||||
clearstatcache(true, $filePath); // Clear file stat cache
|
||||
$fileSize = filesize($filePath);
|
||||
$fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
||||
|
||||
// Disable output buffering to prevent Content-Length mismatches
|
||||
if (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
// Set content type based on extension
|
||||
$mimeTypes = [
|
||||
// Video
|
||||
'mp4' => 'video/mp4',
|
||||
'mkv' => 'video/x-matroska',
|
||||
'avi' => 'video/x-msvideo',
|
||||
'mov' => 'video/quicktime',
|
||||
'wmv' => 'video/x-ms-wmv',
|
||||
'flv' => 'video/x-flv',
|
||||
'webm' => 'video/webm',
|
||||
'm4v' => 'video/x-m4v',
|
||||
// Audio
|
||||
'mp3' => 'audio/mpeg',
|
||||
'wav' => 'audio/wav',
|
||||
'ogg' => 'audio/ogg',
|
||||
'flac' => 'audio/flac',
|
||||
'm4a' => 'audio/mp4',
|
||||
'aac' => 'audio/aac',
|
||||
'wma' => 'audio/x-ms-wma',
|
||||
'opus' => 'audio/opus'
|
||||
];
|
||||
|
||||
$contentType = isset($mimeTypes[$fileExtension]) ? $mimeTypes[$fileExtension] : 'application/octet-stream';
|
||||
|
||||
// Handle range requests for seeking
|
||||
$range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : '';
|
||||
|
||||
if (!empty($range)) {
|
||||
// Parse range header
|
||||
list($unit, $range) = explode('=', $range, 2);
|
||||
|
||||
if ($unit === 'bytes') {
|
||||
// Parse range values
|
||||
list($start, $end) = explode('-', $range, 2);
|
||||
$start = intval($start);
|
||||
$end = !empty($end) ? intval($end) : $fileSize - 1;
|
||||
|
||||
// Validate range
|
||||
if ($start > $end || $start < 0 || $end >= $fileSize) {
|
||||
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
||||
header("Content-Range: bytes */$fileSize");
|
||||
exit;
|
||||
}
|
||||
|
||||
$length = $end - $start + 1;
|
||||
|
||||
// Set headers for partial content
|
||||
header('HTTP/1.1 206 Partial Content');
|
||||
header("Content-Range: bytes $start-$end/$fileSize");
|
||||
header("Content-Length: $length");
|
||||
header("Content-Type: $contentType");
|
||||
header('Accept-Ranges: bytes');
|
||||
|
||||
// Open file and seek to start position
|
||||
$file = fopen($filePath, 'rb');
|
||||
if ($file === false) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
echo 'Failed to open file';
|
||||
exit;
|
||||
}
|
||||
|
||||
fseek($file, $start);
|
||||
|
||||
// Output the requested range
|
||||
$buffer = 8192; // 8KB chunks
|
||||
$bytesRemaining = $length;
|
||||
|
||||
while ($bytesRemaining > 0 && !feof($file)) {
|
||||
$bytesToRead = min($buffer, $bytesRemaining);
|
||||
$data = fread($file, $bytesToRead);
|
||||
if ($data === false) {
|
||||
break;
|
||||
}
|
||||
echo $data;
|
||||
$bytesRemaining -= strlen($data);
|
||||
if (connection_aborted()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
exit; // Important: exit after streaming
|
||||
}
|
||||
} else {
|
||||
// Normal full file response
|
||||
header('HTTP/1.1 200 OK');
|
||||
header("Content-Type: $contentType");
|
||||
header("Content-Length: $fileSize");
|
||||
header('Accept-Ranges: bytes');
|
||||
header('Cache-Control: public, max-age=3600');
|
||||
|
||||
// Stream file in chunks to avoid memory issues
|
||||
$file = fopen($filePath, 'rb');
|
||||
if ($file === false) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
echo 'Failed to open file';
|
||||
exit;
|
||||
}
|
||||
|
||||
$buffer = 8192; // 8KB chunks
|
||||
while (!feof($file) && !connection_aborted()) {
|
||||
echo fread($file, $buffer);
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
exit; // Important: exit after streaming
|
||||
}
|
||||
?>
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue