149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
|
// upload.php - Step 1: File upload and immediate processing
|
|
|
|
require_once 'includes/globals.php';
|
|
|
|
// Ensure user is authenticated
|
|
if (!isset($_SESSION['user'])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
// Process the form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['mix_file'])) {
|
|
// Get upload config from $config
|
|
$tmpPath = rtrim($config['uploads']['tmp_path'], '/') . '/';
|
|
$maxFileSize = $config['uploads']['max_file_size'];
|
|
$allowedMimeTypes = $config['uploads']['allowed_mime_type'];
|
|
$allowedFileTypes = $config['uploads']['allowed_file_types'];
|
|
|
|
$file = $_FILES['mix_file'];
|
|
|
|
// Basic file validations
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
$_SESSION['error'] = "File upload error.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
if ($file['size'] > $maxFileSize) {
|
|
$_SESSION['error'] = "File is too large.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
// Get file extension and mime type
|
|
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
|
|
if (!in_array($ext, $allowedFileTypes)) {
|
|
$_SESSION['error'] = "File type not allowed.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
if (!in_array($file['type'], $allowedMimeTypes)) {
|
|
$_SESSION['error'] = "MIME type not allowed.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
|
|
// Create a unique temporary filename
|
|
$uniqueName = uniqid() . "." . $ext;
|
|
$destination = $tmpPath . $uniqueName;
|
|
|
|
if (!move_uploaded_file($file['tmp_name'], $destination)) {
|
|
$_SESSION['error'] = "Failed to save uploaded file.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
|
|
// Process the file: if mp3, extract metadata; if zip, extract each mp3's metadata.
|
|
$uploadTask = [];
|
|
$uploadTask['original_name'] = $file['name'];
|
|
$uploadTask['local_path'] = $destination;
|
|
$uploadTask['size'] = $file['size'];
|
|
$uploadTask['ext'] = $ext;
|
|
|
|
if ($ext === "mp3") {
|
|
// Process MP3 file using shell_exec calls (ensure you sanitize arguments)
|
|
$escapedFile = escapeshellarg($destination);
|
|
$artist = trim(shell_exec("eyed3 --query='%a%' $escapedFile"));
|
|
$title = trim(shell_exec("eyed3 --query='%t%' $escapedFile"));
|
|
$duration = trim(shell_exec("mp3info -p \"%S\" $escapedFile"));
|
|
// You can extract additional info as needed
|
|
$uploadTask['file_type'] = 'mp3';
|
|
$uploadTask['metadata'] = [
|
|
'artist' => $artist,
|
|
'title' => $title,
|
|
'duration' => $duration, // in seconds
|
|
];
|
|
$uploadTask['mediaplayer'] = 1;
|
|
} elseif ($ext === "zip") {
|
|
// Process ZIP file using ZipArchive
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($destination) === true) {
|
|
$totalDuration = 0;
|
|
$tracklist = [];
|
|
for ($i = 0; $i < $zip->numFiles; $i++) {
|
|
$entryName = $zip->getNameIndex($i);
|
|
$entryExt = strtolower(pathinfo($entryName, PATHINFO_EXTENSION));
|
|
if ($entryExt === "mp3") {
|
|
// Extract the MP3 temporarily to process metadata
|
|
$tempDir = $tmpPath . uniqid('zip_');
|
|
mkdir($tempDir);
|
|
$tempFilePath = $tempDir . '/' . basename($entryName);
|
|
$zip->extractTo($tempDir, $entryName);
|
|
$escapedFile = escapeshellarg($tempFilePath);
|
|
$title = trim(shell_exec("eyed3 --query='%t%' $escapedFile"));
|
|
$duration = trim(shell_exec("mp3info -p \"%S\" $escapedFile"));
|
|
$tracklist[] = $title ?: basename($entryName);
|
|
$totalDuration += (int)$duration;
|
|
unlink($tempFilePath);
|
|
rmdir($tempDir);
|
|
}
|
|
}
|
|
$zip->close();
|
|
$uploadTask['file_type'] = 'zip';
|
|
$uploadTask['metadata'] = [
|
|
'tracklist' => $tracklist,
|
|
'total_duration' => $totalDuration,
|
|
];
|
|
// Mark ZIPs as download only (no mediaplayer)
|
|
$uploadTask['mediaplayer'] = 0;
|
|
} else {
|
|
$_SESSION['error'] = "Failed to open ZIP file.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
} else {
|
|
$_SESSION['error'] = "Unsupported file type.";
|
|
header("Location: upload.php");
|
|
exit;
|
|
}
|
|
|
|
// Save the upload task details in session so step 2 can use them.
|
|
$_SESSION['upload_task'] = $uploadTask;
|
|
header("Location: upload_details.php");
|
|
exit;
|
|
}
|
|
|
|
require_once 'includes/header.php';
|
|
?>
|
|
|
|
<section class="upload-section py-5">
|
|
<div class="container">
|
|
<h2 class="mb-4">Upload a New Mix</h2>
|
|
<?php
|
|
if (isset($_SESSION['error'])) {
|
|
echo '<div class="alert alert-danger">' . htmlspecialchars($_SESSION['error']) . '</div>';
|
|
unset($_SESSION['error']);
|
|
}
|
|
?>
|
|
<form action="upload.php" method="post" enctype="multipart/form-data">
|
|
<div class="mb-3">
|
|
<label for="mix_file" class="form-label">Select Mix File (MP3 or ZIP)</label>
|
|
<input type="file" class="form-control" id="mix_file" name="mix_file" accept=".mp3,.zip" required>
|
|
</div>
|
|
<!-- Optionally, add album art upload here later -->
|
|
<button type="submit" class="btn btn-primary">Upload File</button>
|
|
</form>
|
|
</div>
|
|
</section>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|