190 lines
9.1 KiB
PHP
190 lines
9.1 KiB
PHP
<?php
|
|
require_once 'auth.php';
|
|
require_once 'db/config.php';
|
|
|
|
$pageTitle = "Upload Album - Zeal Music";
|
|
$successMessage = "";
|
|
$errorMessage = "";
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$pdo = db();
|
|
$userId = $_SESSION['user_id'];
|
|
|
|
// Album details
|
|
$artistName = trim($_POST['artist-name'] ?? 'Unknown Artist');
|
|
$albumName = trim($_POST['album-name'] ?? 'Untitled Album');
|
|
$genre = trim($_POST['genre'] ?? '');
|
|
$releaseDate = !empty($_POST['release-date']) ? $_POST['release-date'] : null;
|
|
|
|
// Handle Album Art Upload
|
|
$coverArtPath = null;
|
|
if (isset($_FILES['album-art']) && $_FILES['album-art']['error'] == UPLOAD_ERR_OK) {
|
|
$artTmpName = $_FILES['album-art']['tmp_name'];
|
|
$artName = basename($_FILES['album-art']['name']);
|
|
$artExt = pathinfo($artName, PATHINFO_EXTENSION);
|
|
$newArtName = uniqid('art_', true) . '.' . $artExt;
|
|
$coverArtPath = 'uploads/cover_art/' . $newArtName;
|
|
if (!move_uploaded_file($artTmpName, $coverArtPath)) {
|
|
$errorMessage = "Failed to upload album art.";
|
|
$coverArtPath = null;
|
|
}
|
|
}
|
|
|
|
// Handle Tracks
|
|
if (empty($errorMessage) && isset($_POST['tracks']) && is_array($_POST['tracks'])) {
|
|
$tracks = $_POST['tracks'];
|
|
$files = $_FILES['tracks'];
|
|
$allSucceeded = true;
|
|
|
|
foreach ($tracks as $index => $track) {
|
|
$trackTitle = trim($track['title']);
|
|
|
|
// Restructure FILES array for easier access
|
|
$audioFile = [
|
|
'name' => $files['name']['audio_file'][$index],
|
|
'type' => $files['type']['audio_file'][$index],
|
|
'tmp_name' => $files['tmp_name']['audio_file'][$index],
|
|
'error' => $files['error']['audio_file'][$index],
|
|
'size' => $files['size']['audio_file'][$index]
|
|
];
|
|
|
|
if (!empty($trackTitle) && $audioFile['error'] == UPLOAD_ERR_OK) {
|
|
$audioTmpName = $audioFile['tmp_name'];
|
|
$audioName = basename($audioFile['name']);
|
|
$audioExt = pathinfo($audioName, PATHINFO_EXTENSION);
|
|
$newAudioName = uniqid('track_', true) . '.' . $audioExt;
|
|
$audioFilePath = 'uploads/music/' . $newAudioName;
|
|
|
|
if (move_uploaded_file($audioTmpName, $audioFilePath)) {
|
|
try {
|
|
$stmt = $pdo->prepare(
|
|
"INSERT INTO tracks (user_id, title, album, genre, release_date, file_path, cover_art_path)
|
|
VALUES (:user_id, :title, :album, :genre, :release_date, :file_path, :cover_art_path)"
|
|
);
|
|
$stmt->execute([
|
|
':user_id' => $userId,
|
|
':title' => $trackTitle,
|
|
':album' => $albumName,
|
|
':genre' => $genre,
|
|
':release_date' => $releaseDate,
|
|
':file_path' => $audioFilePath,
|
|
':cover_art_path' => $coverArtPath
|
|
]);
|
|
} catch (PDOException $e) {
|
|
$errorMessage = "Database error: " . $e->getMessage();
|
|
$allSucceeded = false;
|
|
break; // Exit loop on DB error
|
|
}
|
|
} else {
|
|
$errorMessage = "Failed to move uploaded audio file for track: " . htmlspecialchars($trackTitle);
|
|
$allSucceeded = false;
|
|
break;
|
|
}
|
|
} else {
|
|
$errorMessage = "Please provide a title and audio file for all tracks. Error code: " . $audioFile['error'];
|
|
$allSucceeded = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($allSucceeded) {
|
|
$successMessage = "Album and all tracks submitted successfully!";
|
|
}
|
|
} elseif (empty($errorMessage)) {
|
|
$errorMessage = "No tracks were submitted. Please add at least one track.";
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo htmlspecialchars($pageTitle); ?></title>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body class="dark-theme">
|
|
<div class="page-container">
|
|
<?php include 'partials/sidebar.php'; ?>
|
|
|
|
<main class="main-content">
|
|
<header class="main-header">
|
|
<h1>Upload Your Album</h1>
|
|
<div class="user-profile">
|
|
<i class="fa fa-user-circle"></i>
|
|
<span><?php echo htmlspecialchars($_SESSION['username']); ?></span>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="content-wrapper">
|
|
<div class="upload-form-container">
|
|
<?php if ($successMessage): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($successMessage); ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($errorMessage): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($errorMessage); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="upload.php" method="POST" enctype="multipart/form-data" class="upload-form">
|
|
|
|
<fieldset>
|
|
<legend>Album Details</legend>
|
|
<div class="form-group">
|
|
<label for="artist-name">Artist Name</label>
|
|
<input type="text" id="artist-name" name="artist-name" value="<?php echo htmlspecialchars($_SESSION['username']); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="album-name">Album Name</label>
|
|
<input type="text" id="album-name" name="album-name" placeholder="Enter the album name" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="album-art">Album Art</label>
|
|
<input type="file" id="album-art" name="album-art" accept="image/png, image/jpeg">
|
|
<small>Recommended: Square image, 1500x1500px. Applies to all tracks.</small>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="genre">Genre</label>
|
|
<input type="text" id="genre" name="genre" placeholder="e.g., Pop, Rock, Hip-Hop">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="release-date">Release Date</label>
|
|
<input type="date" id="release-date" name="release-date">
|
|
</div>
|
|
</fieldset>
|
|
|
|
<fieldset>
|
|
<legend>Tracks</legend>
|
|
<div id="tracks-container">
|
|
<div class="track-input-group">
|
|
<h5>Track 1</h5>
|
|
<div class="form-group">
|
|
<label for="track-title-0">Track Title</label>
|
|
<input type="text" id="track-title-0" name="tracks[0][title]" placeholder="Enter the track title" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="audio-file-0">Audio File</label>
|
|
<input type="file" id="audio-file-0" name="tracks[0][audio_file]" accept="audio/mpeg, audio/wav" required>
|
|
<small>Required: High-quality MP3 or WAV file.</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<button type="button" id="add-track-btn" class="btn btn-secondary">Add Another Track</button>
|
|
</fieldset>
|
|
|
|
<div class="form-group-checkbox">
|
|
<input type="checkbox" id="copyright-agree" name="copyright-agree" required>
|
|
<label for="copyright-agree">I confirm that I own all the rights to this music and agree to the <a href="#">distribution terms</a>.</label>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary btn-lg">Submit Album</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|