0

I'm in the middle of developing a Safari extension for imageboard-type websites and one of the bigger features I'm hoping to implement is the ability to download all of the images (the posted ones, not the global page-level images) that had been posted.

There are similar questions here already, but mine differs a bit in that the images in question are hosted on an entirely different server. I've been brainstorming a bit and figured that gathering all of the image URLs in a JS array then sending it to my server to be turned into a zip file (forcing the download, not just a link to the file) would be the best way to go. I also want the zip to be deleted after the user downloads it.

I've already finished the majority of the extension features but this one is stumping me. Any help would be greatly appreciated.

How would I'd go about doing this?

Drise
  • 4,310
  • 5
  • 41
  • 66
Nik Matt
  • 3
  • 3

2 Answers2

0

You want a extension to contact your server for downloads? That's a terrible idea! Make the zipfile locally - it's not regular javascript, it's an extension - you have full access.

Anyway assuming you want to do this anyway, what is the trouble you are having? You get a list of urls, send them to your server, your server downloads them, zips them and send them to the user. (The "your server downloads them" part should worry you!)

What problem are you having?

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • It's not so much a problem per-se, I just haven't had practice for this sort of thing in a hot minute. So are you guys saying that JavaScript can make archives itself? From the best of my knowledge, Safari doesn't allow extensions to access enduser files that aren't in the extension package. I assume that means it can't write files either. – Nik Matt Jul 16 '12 at 19:14
  • So are you guys saying that JavaScript can make archives itself? From the best of my knowledge, Safari doesn't allow extensions to access enduser files that aren't in the extension package. I assume that means it can't write files either. Basically all they can do is inject content into the target and modify Safari itself. Basically the only option I can think of is to send an array of image urls to the PHP script and archive from that. If that's not a good idea what would you guys do? Sorry for the double post, I didn't realize there was a 5 minute edit limit. – Nik Matt Jul 16 '12 at 19:21
  • @NikMatt You can delete your previous comment, or leave it and just add the additional info instead of repeating it in your second comment. – Ariel Jul 16 '12 at 20:22
  • @NikMatt If your extension can't do then I guess the webserver is your only choice, but you are going to have to write it as a service - people submit the list, then wait till the file is ready before downloading. It will take too long to download all the images for you to be able to do it live. But you should try really hard to find another way - doing this as a web service is really not ideal. – Ariel Jul 16 '12 at 20:23
  • I agree about it not being ideal. It baffles me that the reverse would be quite easy to implement (extracting zip onto server). Now I know why this type of script doesn't exist outside of a Windows executable. Thank you for your help! – Nik Matt Jul 16 '12 at 20:29
0

You can use PHP's ZipArchive class to make a ZIP, then stream it to the browser.

<?php
// Create temp zip file
$zip = new ZipArchive;
$temp = tempnam(sys_get_temp_dir(), 'zip');
$zip->open($temp);

// Add files
$zip->addFromString('file.jpg', file_get_contents('http://path/to/file.jpg'));
$zip->addFile('/this/is/my/file.txt');

// Write temp file
$zip->close();

// Stream file to browser
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myFile.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($temp));

readfile($temp);

unlink($temp);
exit;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337