Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
schorschii committed Dec 31, 2017
1 parent 27056fa commit 2961a5b
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 149 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
@@ -1,3 +1,9 @@
v0.0.4
- [new] scan.php finds correct next free cover image number for file name in music_thumb
- [fix] scan.php optimizations: artist and album are now updated correctly, option for completely rescan files (truncates database)
- [new] mysqli connection now uses utf8 encoding
- [new] volume slider value is now saved in PHP session, so the value is restored when opening an new track

v0.0.3
- [new] update notification
- [new] scan doesn't truncate tables anymore - it updates already existing tracks, so playlists don't get lost anymore
Expand Down
2 changes: 1 addition & 1 deletion CURRENTVERSION.txt
@@ -1 +1 @@
0.0.3
0.0.4
1 change: 1 addition & 0 deletions database.php
Expand Up @@ -10,4 +10,5 @@
if ($mysqli->connect_errno) {
die("Failed to connect to database server: " . $mysqli->connect_error);
}
$mysqli->set_charset("utf8");
?>
4 changes: 3 additions & 1 deletion js/player.js
Expand Up @@ -200,6 +200,8 @@ function setTime() {
}
function setVolume() {
obj('mainPlayer').volume = obj('volumeBar').value;
// save volume in php session, so it can be restored when opening a new track
ajaxRequest('dummy', 'sessionvars.php?set=volume&value='+obj('volumeBar').value, '');
}

// don't toggle play pause on space bar key press if prev/play/pause/next button is focused (this will call togglePlayPause() twice)
Expand Down Expand Up @@ -312,7 +314,7 @@ function runPlaylist(link, playerobjid) {
player.volume = audioLastState;

// start playback
player.load(); player.play();
player.load(); player.play(); setVolume();

// if remote player is set, refresh current state
if(remotePlayerId != -1)
Expand Down
10 changes: 6 additions & 4 deletions php/library-views/options.php
Expand Up @@ -11,13 +11,12 @@
<img id='aboutLogo' src='img/buzzsaw_icon_b.svg'>
<b>BUZZSAW html multimedia server (library and player) <?php echo $CVERSION; ?></b>
<br>Licensed under the terms of the <a href='#' onclick="ajaxRequest('content','library.php?view=license');">GPLv2</a>
<br><a href="https://github.com/schorschii/buzzsaw" target="_blank">Fork me on GitHub</a>!
<br><a href="https://github.com/schorschii/buzzsaw" target="_blank">Fork me on GitHub</a>
<br>&copy; 2017 Georg Sieber
</p>
<p>
This program uses the <a href='http://getid3.sourceforge.net/' target='blank'>getid3()</a> library v1.9.14
<br>&copy; 2017 James Heinrich
<br>Licensed under the terms of the <a href='#' onclick="ajaxRequest('content','library.php?view=license');">GPLv2</a>
<br>&copy; 2017 James Heinrich (License: <a href='#' onclick="ajaxRequest('content','library.php?view=license');">GPLv2</a>)
</p>
</div>
</li>
Expand All @@ -26,7 +25,10 @@
<a href='#' onclick="ajaxRequest('content','library.php?view=upload');"><img src='img/upload.svg'>Upload and import tracks</a>
</li>
<li class='option tracklisting'>
<a href='scan.php' target='_blank' onclick='return confirm("This will scan the >music< directory inside your buzzsaw directory for tracks. Depending on the amount of tracks, this could take some time.");'><img src='img/search.svg'>Scan filesystem for tracks</a>
<a href='scan.php' target='_blank' onclick='return confirm("This will scan the >music< directory inside your buzzsaw directory for new or changed tracks. Depending on the amount of tracks, this could take some time.");'><img src='img/search.svg'>Scan filesystem for tracks</a>
</li>
<li class='option tracklisting'>
<a href='scan.php?rescan=1' target='_blank' onclick='return confirm("This will truncate your music database and completely rescan the >music< directory inside your buzzsaw directory. Depending on the amount of tracks, this could take some time. You should only use this scan method, if you encounter problems with your database.");'><img src='img/search.svg'>Completely rescan filesystem for tracks</a>
</li>
<li class='option tracklisting'>
<a href='login.php?changepassword=1'><img src='img/password.svg'>Change password</a>
Expand Down
4 changes: 2 additions & 2 deletions player.php
Expand Up @@ -373,7 +373,7 @@ function createCurrentPlaylistEntry($track_number, $title, $artist, $album, $pat
<input type="range" id="timeBarCurrent" min="0" max="100" step="0.001" value="0" onchange="setTime();" onmousedown="refreshTimeBar=false;" onmouseup="refreshTimeBar=true;"></input>
</div>

<input type="range" id="volumeBar" min="0" max="1" step="0.01" value="0.8" onchange="setVolume();" style="display: none;"></input>
<input type="range" id="volumeBar" min="0" max="1" step="0.01" value="<?php echo isset($_SESSION['volume']) ? $_SESSION['volume'] : "0.8"; ?>" onchange="setVolume();" style="display: none;"></input>

<div id="menu">
<?php if(!(getOS1() == "iPhone" && getBrowser1() == "Safari")) { ?>
Expand All @@ -387,7 +387,7 @@ function createCurrentPlaylistEntry($track_number, $title, $artist, $album, $pat
<div id="notification">
</div>

<script>initPlaylist(<?php echo ($currenttrack_number-1); ?>);</script>
<script>initPlaylist(<?php echo ($currenttrack_number-1); ?>);setVolume();</script>

<?php if($currentURL == "") echo "<script>toggleMenu('');</script>"; ?>

Expand Down
114 changes: 64 additions & 50 deletions scan.php
Expand Up @@ -29,35 +29,38 @@
$getID3 = new getID3;

require_once('database.php');

// remove old data
#if (!$mysqli->multi_query(file_get_contents("sql/clean.sql")))
# echo("<b>ERROR TRUNCATING TABLES:</b><br>" . $mysqli->error . "<br>");
#clearStoredResults($mysqli);

// remove all previous album cover thumbnails
#$files = glob('./music_thumb/*');
#foreach($files as $file){
# if(is_file($file))
# unlink($file);
#}
$THUMB_DIR = "music_thumb";
$MUSIC_DIR = "music";

// remove old db entries and album cover thumbnails
if(isset($_GET['rescan']) && $_GET['rescan'] == 1) {
if (!$mysqli->multi_query(file_get_contents("sql/clean.sql")))
die("<b>ERROR TRUNCATING TABLES:</b><br>" . $mysqli->error . "<br>");
clearStoredResults($mysqli);

$files = glob($THUMB_DIR.'/*');
foreach($files as $file){
if(is_file($file)) unlink($file);
}
}

// find all music files inside the music directory
$counter = 0; $counter_new = 0; $counter_update = 0;
$counter = 0;
$fs_perm_warned = false;
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./music'));
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($MUSIC_DIR));
foreach ($it as $file) {
if (isAudioFile($file)) {
// check if file is already in database
$track_id = -1;
$track_cover = "";
$sql = "SELECT tr.id AS 'id', tr.cover AS 'cover' "
. "FROM track tr "
. "WHERE tr.path = ?;";
$sql = "SELECT tr.id AS 'id', tr.cover AS 'cover' FROM track tr WHERE tr.path = ?";
$statement = $mysqli->prepare($sql);
$statement->bind_param('s', $file);
$statement->execute();
if (!$statement->execute())
echo("<b>EXEC FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>");
$result = $statement->get_result();
if (!$result)
echo("<b>GET RESULT FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>");
while($row = $result->fetch_object()) {
$track_id = $row->id;
$track_cover = $row->cover;
Expand All @@ -81,6 +84,8 @@
$title = "Unknown Title";
if (isset($FileInfo['comments_html']['title'][0]))
$title = $FileInfo['comments_html']['title'][0];
else
$title = pathinfo($file)['filename'];

// read track number
$track_number = 0;
Expand All @@ -89,52 +94,46 @@
if (strpos($track_number, '/') !== false) $track_number = explode('/', $track_number)[0];

// read cover if available
$cover = NULL;
if (isset($FileInfo['comments']['picture'][0])) {
if ($track_id != -1)
$filename = $track_cover;
$cover = null;
if (isset($FileInfo['comments']['picture'][0])
&& $FileInfo['comments']['picture'][0] != "") {
if ($track_id == -1 || $track_cover == "")
$filename = $THUMB_DIR . "/" . findFreeImageNumber() . ".jpg";
else
$filename = "music_thumb/" . $counter . ".jpg";
$filename = $track_cover;

if (file_put_contents($filename, $FileInfo['comments']['picture'][0]['data']) === false && $fs_perm_warned == false) {
$fs_perm_warned = true;
echo "<b>WARN:</b>&nbsp;Unable to write into ./music_thumb. Album covers will not be available.<br>";
echo "<b>WARN:</b>&nbsp;Unable to write into cover thumbnail directory. Album covers will not be available.<br>";
}
$cover = $filename;
}

if ($track_id == -1) {
// insert new track
$sql = "CALL InsertTrack(?, ?, ?, ?, ?, ?);";
$statement = $mysqli->prepare($sql);
if (!$statement)
echo "<b>PREPARE FAILED:</b>&nbsp;$sql<br>".$mysqli->error."<br>";
if (!$statement->bind_param('ssssss', $title, $album, $artist, $file, $track_number, $cover))
echo "<b>BIND FAILED:</b>&nbsp;$file<br>$sql<br>";
if (!$statement->execute())
echo "<b>EXEC FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>";
$counter_new ++;
} else {
// update track
$sql = "UPDATE track "
. "SET title = ?, track_number = ? "
. "WHERE id = ?;";
$statement = $mysqli->prepare($sql);
if (!$statement)
echo "<b>PREPARE FAILED:</b>&nbsp;$sql<br>".$mysqli->error."<br>";
if (!$statement->bind_param('sii', $title, $track_number, $track_id))
echo "<b>BIND FAILED:</b>&nbsp;$file<br>$sql<br>";
if (!$statement->execute())
echo "<b>EXEC FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>";
$counter_update ++;
}

// call insert/update sql procedure
$sql = "CALL InsertUpdateTrack(?, ?, ?, ?, ?, ?)";
$statement = $mysqli->prepare($sql);
if (!$statement)
echo("<b>PREPARE FAILED:</b>&nbsp;$sql<br>".$mysqli->error."<br>");
if (!$statement->bind_param('ssssss', $title, $album, $artist, $file, $track_number, $cover))
echo("<b>BIND FAILED:</b>&nbsp;$file<br>$sql<br>");
if (!$statement->execute())
echo("<b>EXEC FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>");

flush(); ob_flush();
$counter ++;
}
}

echo "<br><b>Finished - inserted $counter_new new track(s), updated $counter_update track(s).</b><br>";
// call cleanup script
$sql = "CALL PurgeAlbumArtist";
$statement = $mysqli->prepare($sql);
if (!$statement)
echo("<b>PREPARE FAILED:</b>&nbsp;$sql<br>".$mysqli->error."<br>");
if (!$statement->execute())
echo("<b>EXEC FAILED:</b>&nbsp;$file<br>$sql<br>".$statement->error."<br>");

echo "<br><b>Finished - scanned $counter track(s).</b><br>";
echo "<a href='player.php' class='styled_link'>Open Player</a>";
?>
</div>
Expand All @@ -143,3 +142,18 @@

</body>
</html>

<?php
// find next free number for image file
function findFreeImageNumber() {
global $THUMB_DIR;
$free = false;
$img_free_counter = 0;
while (!$free) {
$img_free_counter ++;
if (!file_exists($THUMB_DIR . "/" . $img_free_counter . ".jpg"))
$free = true;
}
return $img_free_counter;
}
?>
21 changes: 21 additions & 0 deletions sessionvars.php
@@ -0,0 +1,21 @@
<?php
session_start();

if (isset($_SESSION['username'])) {
if(isset($_GET['set']) && isset($_GET['value'])) {
switch($_GET['set']) {
case "volume":
$_SESSION['volume'] = $_GET['value'];
exit;
}
}

if(isset($_GET['get']) && isset($_GET['value'])) {
switch($_GET['get']) {
case "volume":
return $_SESSION['volume'];
exit;
}
}
}
?>
5 changes: 4 additions & 1 deletion setup.php
Expand Up @@ -18,7 +18,10 @@
if (!$mysqli->multi_query(file_get_contents("sql/tables.sql")))
die("<b>ERROR CREATING TABLES:</b><br>" . $mysqli->error . "<br>");
clearStoredResults($mysqli);
if (!$mysqli->query(file_get_contents("sql/InsertTrack.sql")))
if (!$mysqli->query(file_get_contents("sql/InsertUpdateTrack.sql")))
die("<b>ERROR IMPORTING FUNCTION INSERTTRACK:</b><br>" . $mysqli->error . "<br>");
clearStoredResults($mysqli);
if (!$mysqli->query(file_get_contents("sql/PurgeAlbumArtist.sql")))
die("<b>ERROR IMPORTING FUNCTION INSERTTRACK:</b><br>" . $mysqli->error . "<br>");
clearStoredResults($mysqli);
if (!$mysqli->query(file_get_contents("sql/MoveTrackInPlaylist.sql")))
Expand Down
27 changes: 0 additions & 27 deletions sql/InsertTrack.sql

This file was deleted.

43 changes: 43 additions & 0 deletions sql/InsertUpdateTrack.sql
@@ -0,0 +1,43 @@
CREATE PROCEDURE `InsertUpdateTrack`(`insert_track` TEXT, `insert_album` TEXT, `insert_artist` TEXT, `insert_path` TEXT, `insert_track_number` INT, `insert_cover` TEXT)

BEGIN

DECLARE artist_check INT;
DECLARE album_check INT;
DECLARE track_check INT;

SELECT id INTO artist_check FROM artist ar
WHERE ar.title = insert_artist
LIMIT 1;

SELECT al.id INTO album_check FROM album al
INNER JOIN artist ar ON al.artist_id = ar.id
WHERE ar.title = insert_artist
AND al.title = insert_album
LIMIT 1;

SELECT t.id INTO track_check FROM track t
WHERE t.path = insert_path
LIMIT 1;

IF artist_check IS NULL THEN
INSERT INTO artist (title) VALUES (insert_artist);
SELECT LAST_INSERT_ID() INTO artist_check;
END IF;
IF album_check IS NULL THEN
INSERT INTO album (title, artist_id) VALUES (insert_album, artist_check);
SELECT LAST_INSERT_ID() INTO album_check;
END IF;


IF track_check IS NOT NULL THEN
/* update if track already exists */
UPDATE track SET title = insert_track, artist_id = artist_check, album_id = album_check, path = insert_path, track_number = insert_track_number, cover = insert_cover
WHERE id = track_check;
ELSE
/* insert new track */
INSERT INTO track (title, artist_id, album_id, path, track_number, cover)
VALUES (insert_track, artist_check, album_check, insert_path, insert_track_number, insert_cover);
END IF;

END;
23 changes: 23 additions & 0 deletions sql/PurgeAlbumArtist.sql
@@ -0,0 +1,23 @@
CREATE PROCEDURE `PurgeAlbumArtist`()

BEGIN

/* purge albums with no tracks anymore */
DELETE a FROM album a
JOIN (
SELECT al.id AS 'id' FROM album al
LEFT JOIN track t on t.album_id = al.id
GROUP BY al.id
HAVING count(t.id) = 0
) del ON a.id = del.id;

/* purge artists with no albums anymore */
DELETE a FROM artist a
JOIN (
SELECT ar.id AS 'id' FROM artist ar
LEFT JOIN album al on al.artist_id = ar.id
GROUP BY ar.id
HAVING count(al.id) = 0
) del ON a.id = del.id;

END;

0 comments on commit 2961a5b

Please sign in to comment.