Download Album

xdreamer
User
Posts: 4
Joined: Tue Mar 22, 2005 3:35 pm
Contact:

Download Album

Post by xdreamer »

Similar to the feature request to stream each song seperately it would be interesting to be able to download an entire album. :)
For example as a ZIP file.
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Re: Download Album

Post by wbartels »

xdreamer wrote:Similar to the feature request to stream each song seperately it would be interesting to be able to download an entire album. :)
For example as a ZIP file.
The zip library in PHP can only be used to extract zip files.

There is another library that also support compression.
Who knows the name of that library????
ponchorage

Post by ponchorage »

You can check these out:

http://www.zend.com/codex.php?id=696&single=1

http://www.phpconcept.net/pclzip/index.en.php (I'd use this).

or you may be thinking of zlib.
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

Thanks, I will first try out zlib.
Maybe it is possible to directly download the zlib stream without a temp file on the server.

[update]
zlib doesn't support multiple files :(
[/update]
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

I believe I now have this function enabled, mind you my php is hacked all to hell and it looks awful, but I think it works... If you would like to know what I did please reply back and I'll add all steps. And if you would like to see a demo of this, check out my website, you can login with anonymous access.

edit: no more urls...
Last edited by lylos on Sun Oct 23, 2005 8:29 am, edited 1 time in total.
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

lylos wrote:I believe I now have this function enabled, mind you my php is hacked all to hell and it looks awful, but I think it works... If you would like to know what I did please reply back and I'll add all steps. And if you would like to see a demo of this, check out my website, you can login with anonymous access.

edit: no more urls...
Nice work.

Did you use a command line progaram for zipping?
If it looks awful, maybe I can clean it up.
So let me see how you did it.

PS
Nice album from Alfie :wink:
Last edited by wbartels on Sun Oct 23, 2005 9:19 am, edited 1 time in total.
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

I used the lite zip script from http://smiledsoft.com/demos/phpzip/index.shtml.
Added this into the includes folder.
Then in stream.php I added the following to the end of the file...

Code: Select all

//  +---------------------------------------------------------------------------+
//  | Download Album                                                            |
//  +---------------------------------------------------------------------------+
function downloadalbum($album_id)
{
require("include/ss_zip.class.php");
global $cfg;
authenticate('access_download');
ini_set('max_execution_time', $cfg['download_timeout']);
$zip= new ss_zip('',1);

$test = mysql_query('SELECT relative_file FROM track WHERE album_id = "' . mysql_real_escape_string($album_id) . '" ORDER BY relative_file');

while($file = mysql_fetch_array($test))
{
$filename = mysql_query('SELECT artist, title FROM track WHERE relative_file = "' . mysql_real_escape_string($file[0]) .'"');
$name = mysql_fetch_array($filename);
$file2 = $cfg['media_dir'] . $file[0];
$zip->add_file($file2,$file[0]);
};
$albumname = mysql_query('SELECT artist, album FROM album WHERE album_id = "' . mysql_real_escape_string($album_id) . '"');
$albumname2 = mysql_fetch_array($albumname);
$combined2 = $albumname2[0] ." - ". $albumname2[1] .".zip";

header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . $combined2 . '"');

$zip->save($combined2, b);
}
Also added the following line to stream.php where you check the commands:

Code: Select all

if ($command == 'downloadalbum') downloadalbum($album_id);
PS:
Thanks, I really love the album :)
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

Thanks very much for the code :D

May this freeware class be bundled with netjukebox witch is GPL licensed?
Its Freeware you may use it as you wish but you may not redistribute it alone.
For commercial applications it is strongly recommended to upgrade to PRO version
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

The way I read it is that it can be re-distributed, but not alone... Which I'm guessing would go to say someone who is trying to say they made it? I don't own the script so I don't know.
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

I don't want to be picky, but I found to big problems with the ss_zip.class.php
There is no possibility to store the file (the minimum compression level is 1)
Try out for yourself the speed difference between store and compression level 1 with WinRAR or another program.

Downloading albums with files larger that 8 MB doesn't work.
When increasing the memory_limit in php.ini to the largest file in the album it works again.
So I assume it has to store the whole processed file in memory.

I could download albums with files larger than 8 MB from your site.
Have you increased the memory_limit in php.ini?
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

Because of the limitations of the script I have searched the internet for alternatives.
I found other open source php scripts but they all needed to allocate memory to the largest file.

What I found interesting is the 7zip open source command line compressor.
Witch can compress in many different formats and is available for Windows and Linux.

http://www.7-zip.org/
7za a –mx0 –tzip %destination %source

Because of the new login system a temp directory based on session can be created.
So different zip files can be created at the same time ;-)

All this gives me a new idea.
When making use of a temp directory it is also possible to transcode the music to an other format before making the zip file.
This makes sense for lossless music.

What do you think of this idea?
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

In the login script is there a session ID that is created or some kind of key that we can use to create a directory to store the zip in? In my situation, I have everyone log in via the anonymous login to use my script. Storing the zip file in a directory based on username wouldn't be enough. I'm searching the login script now.

Update... Found a key I was looking for. :D

As for the idea, I'm loving it. I can get the zip part down, just about have it working. But for the transcoding into a different format, that's way out of me league :)

Code: Select all

cookie('netjukebox_hash')
User avatar
wbartels
netjukebox developer
Posts: 881
Joined: Thu Nov 04, 2004 3:12 pm
Location: Netherlands
Contact:

Post by wbartels »

lylos wrote:In the login script is there a session ID that is created or some kind of key that we can use to create a directory to store the zip in? In my situation, I have everyone log in via the anonymous login to use my script. Storing the zip file in a directory based on username wouldn't be enough. I'm searching the login script now.

Update... Found a key I was looking for. :D

Code: Select all

cookie('netjukebox_hash')
On netjukebox 3.75 the cookie('netjukebox_hash') cannot be used as session id.
It is a hash based on the expire time and will change very often.

In netjukebox 3.76 cookie('netjukebox_sid') is set a random number witch will be updated often, but tracked in the database with a session_id.
I will finish netjukebox 3.76 now, and start with the album downloading in the next version.
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

Here's what I have so far. It generates the zip file, it sends it, then it deletes the zip file after completion of the send.

Only issue I'm having is when I cancel the download or click stop, I'm stuck with a huge zip file in a temp directory... any ideas?

And this is UGLY UGLY UGLY... Still working on it :)

Code: Select all

//  +---------------------------------------------------------------------------+
//  | Download Album                                                            |
//  +---------------------------------------------------------------------------+
function downloadalbum($album_id)
{
global $cfg;
authenticate('access_download');
ini_set('max_execution_time', $cfg['download_timeout']);

$test = mysql_query('SELECT relative_file FROM track WHERE album_id = "' . mysql_real_escape_string($album_id) . '" ORDER BY relative_file');

while($file = mysql_fetch_array($test))
{
$filename = mysql_query('SELECT artist, title FROM track WHERE relative_file = "' . mysql_real_escape_string($file[0]) .'"');
$name = mysql_fetch_array($filename);
$file2 = $cfg['media_dir'] . $file[0];
$filelist .= '"'. $file2 .'" ';
};
$albumname = mysql_query('SELECT artist, album FROM album WHERE album_id = "' . mysql_real_escape_string($album_id) . '"');
$albumname2 = mysql_fetch_array($albumname);
$combined2 = $albumname2[0] ." - ". $albumname2[1] .".zip";
set_time_limit(0);
$downloadcmd = 'C:/Codecs/7za.exe a -mx0 -tzip "'. $cfg['tempdir'] . cookie('netjukebox_hash') .'/'. $combined2 .'" '. $filelist;
@exec($downloadcmd);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="' . $combined2 . '"');
header('Content-Transfer-Encoding: binary');
$file = $cfg['tempdir'] . cookie('netjukebox_hash') .'/'. $combined2;
		if (version_compare(phpversion(), '5.0.0', '>='))
			{
			$filehandle = @fopen($file, 'rb') or exit();
			while (!feof($filehandle))
				echo fread($filehandle, 1024 * 1024);
			fclose($filehandle);
			}
		else
			@readfile($file);
function rmdirr($dir) {
if($objs = glob($dir."/*")){
foreach($objs as $obj) {
is_dir($obj)? rmdirr($obj) : unlink($obj);
}
}
rmdir($dir);
} 
rmdirr($cfg['tempdir'] . cookie('netjukebox_hash') .'/');
}
lylos
User
Posts: 14
Joined: Tue Dec 14, 2004 2:36 pm

Post by lylos »

You said with the new version that there will be a session_id tracked in the mysql database. Is this going to be deleted after a timeout or whenever the user leaves the website? If so, then the deletion of the temp dirs can be tied with this.
Locked