Page 1 of 2

Run script when done uploading

Posted: 13 Jun 2011, 12:44
by gaitt
Hey guys,

I wondering if anyone can help me out!
I'd like to run an shell script when an uploading session is done.

For example, i put 10 pics to upload. When the last picture is succefully uploaded, the uploader start my shell script!
Is it something possible or not?

I know how to run a script from php but i don't know what file i should look into!

Thx,
Gautier

Re: Run script when done uploading

Posted: 13 Jun 2011, 12:57
by Martin
gaitt wrote:I'd like to run an shell script when an uploading session is done.
I'm really curious what you want the shell script to do after an uploading session....

Re: Run script when done uploading

Posted: 13 Jun 2011, 13:13
by gaitt
The thing is i'm not the only one who can upload files on my gallery, so i'd like to check that the uploaded pictures are in the good resolution (if not resize them) and also to do a sort of newsletter, send an email that say "new pictures are available".
:)

Re: Run script when done uploading

Posted: 13 Jun 2011, 13:33
by Martin
gaitt wrote:The thing is i'm not the only one who can upload files on my gallery, so i'd like to check that the uploaded pictures are in the good resolution (if not resize them) and also to do a sort of newsletter, send an email that say "new pictures are available".
:)
Ah... that explains a lot... such a situation didn't cross my mind...
Thanks for the insight... don't have a solution, I'm afraid... :)

Re: Run script when done uploading

Posted: 14 Jun 2011, 06:25
by Nick
Iv-includes/admin/controller/IndexControllers.php
Code
	/**
	 * Upload file
	 *
	 */
	function uploadAction()
	{

Re: Run script when done uploading

Posted: 14 Jun 2011, 17:14
by gaitt
Thanks Nick!!

I suppose it's "admin/controller/IndexControllers.php" coz "Iv-includes/admin/controller/IndexControllers.php" doesn't exist on my gallery!
Anyway, could you give me some help/tips to do it, i was thinking i can do it myself but i only know some newby stuff of php so ... and the functions i tried so far doesn't seems to be called.

If you plan to do some newsletter oriented features in the 2.8 release, then maybe i can wait! :D

Re: Run script when done uploading

Posted: 15 Jun 2011, 00:54
by Nick
Sorry iv-admin/controllers/IndexController.php (I assume you have the latest version of Imagevue)

Just add
Code
mail('to@me.com','New file uploaded',$imageName); 
after
Code
echo "File {$imageName} succesfully uploaded";
It should work. I think you can expand this code as you see fit.

Re: Run script when done uploading

Posted: 18 Jun 2011, 14:00
by gaitt
Well hmmm, it's not working!

If i run a php script from command line with "mail", everything works fine.
But in the IndexController, nothing happen when i upload files.

Even a simple
Code
touch('/var/www/toto');
just after
Code
echo "File {$imageName} succesfully uploaded";
doesn't work!
It's weird, i'm kind of lost rigth now.

How can i see the prints of "echo". Did i need a php debugger?
Thx in advance.

Re: Run script when done uploading

Posted: 19 Jun 2011, 03:10
by Nick
This is a callback function for JS, it doesn't really output anything you can see. Just checked my own code though:
Code
mail('nick@whatever',"File {$imageName} succesfully uploaded","File {$imageName} succesfully uploaded");
And here it is
Code
To: nick@whatever.local
Subject: File Page Capture 1.png succesfully uploaded
Date: Sun, 19 Jun 2011 14:09:05
From: pain@mini.local (Nick)

File Page Capture 1.png succesfully uploaded
So it should work. Are you sure you're not editing IndexController in iv-admin/guestControllers?

Re: Run script when done uploading

Posted: 26 Jun 2011, 17:38
by gaitt
Ok, i finally figured out what was wrong!!

I'm using msmtp for handling emails on my server. In php.ini the "sendmail_path" configuration was ok but the config file of msmtp wasn't own by www-data!

So, right now i receive 1 mail for 1 picture added! Is there any possiblility to get 1 mail for 1 upload session (for ex: 1 mail for 10 pictures added)??

BTW, thx very much Nick for helping!

Re: Run script when done uploading

Posted: 28 Jun 2011, 09:53
by Nick
Sorry but there is no place to hook up something like this in the code.

Re: Run script when done uploading

Posted: 26 Aug 2011, 10:46
by Artur
Hello.

I have noob question :oops: .

I put some code, just after:
Code
echo "File {$imageName} succesfully uploaded";
My code is:
Code
$dom = new DomDocument('1.0', 'UTF-8');
$element = $dom->createElement($imageName);
$dom->appendChild($element);
$dom->save("new_files.xml");
Why my code doesn't work? Even if something is in xml file, script rewrite it, and save as empty file.
When I write "some_text" instead of $imageName, then works fine, my xml file looks like:
Code
<?xml version="1.0" encoding="UTF-8"?>
<some_text />

Re: Run script when done uploading

Posted: 29 Aug 2011, 07:22
by Nick
I believe this is explained by the fact that $imageName doesn't have valid XML node name. It has dots in it. You can't have <image.jpg /> in XML.

Re: Run script when done uploading

Posted: 29 Aug 2011, 11:22
by Artur
Yes. I can see now, that $imageName is not only name of image, but also "dot" + extension.

Already done code that store the last 20(can be changed) images in the xml file. The rest job is done by the flash module :wink:
If somebody will need, there is the code I use (I know it can be simplified , but I'm not so good PHP coder):
Code
$simplepath = ROOT_DIR . $this->path;
$today = date("m.d.y"); 
$thumbPrefix = "tn_";
$swfAddressPrefix = "#";
$imageUrl = "$swfAddressPrefix$simplepath$imageName";
$thumbnailUrl = "$simplepath$thumbPrefix$imageName";

function loadXmlFile() {
	$domtree = new DOMDocument('1.0', 'UTF-8');
	$domtree->load("new_files.xml");
				
	$rootElement = $domtree->documentElement;
	$newImage = $domtree->createElement("image");
	$newImage = $rootElement->appendChild($newImage);
								
	$fileDetails = $domtree->createAttribute("date");
	$newImage->appendChild($fileDetails);
	$imageDetails = $domtree->createTextNode($today);
	$fileDetails->appendChild($imageDetails);
								
	$thumb = $domtree->createAttribute("thumb");
	$newImage->appendChild($thumb);
	$thumbValue = $domtree->createTextNode($thumbnailUrl);
	$thumb->appendChild($thumbValue);
								
	$imagelink = $domtree->createAttribute("image_link");
	$newImage->appendChild($imagelink);
	$imageValue = $domtree->createTextNode($fullpath);
	$imagelink->appendChild($imageValue);
								
	$imagename = $domtree->createAttribute("image_name");
	$newImage->appendChild($imagename);
	$imagenameValue = $domtree->createTextNode($imageName);
	$imagename->appendChild($imagenameValue);
							
	$itemsId = $domtree->getElementsByTagName('image');
	$id = 1;
	foreach ($itemsId as $itemId) {
		  $itemId->setAttributeNode(new DOMAttr('id', $id));
		  ++$id;
	}
	$domtree->save("new_files.xml");
}

//Let's check if the current folder is "_altimage". If so, uploaded image data will not be stored in xml file.					
$findme  = '_altimage';
$pos = strpos($simplepath, $findme);

if ($pos === false) {				
	$deleteElementId = new DOMDocument('1.0', 'UTF-8');
	$deleteElementId->load("new_files.xml");

	$numberOfNodes = $deleteElementId->getElementsByTagName('image')->length;

	//We want 20 images as "Newest" . So, If xml file already include 20 images, script will delete oldest image data and save newest one.
	if ($numberOfNodes == 20) {
		$xpath = new DOMXpath($deleteElementId);
		$items = $xpath->query('/images/image[@id="1"]');
		foreach ($items as $item) {
			 $item->parentNode->removeChild($item);
		}				
		$deleteElementId->save("new_files.xml");
		loadXmlFile();
	} else {
		loadXmlFile();
	}
} else {
	echo "The string '$findme' was found";
}
And the example data in xml file:
Code
<?xml version="1.0" encoding="UTF-8"?>
<images>
     <image details="08.28.11" thumbnail="/content/gallery/pics/tn_PIC_0001.jpg" url="#/content/gallery/pics/PIC_0001.jpg" image_name="PIC_0001.jpg" id="1"/>
     <image details="08.28.11" thumbnail="/content/gallery/pics/tn_PIC_0002.jpg" url="#/content/gallery/pics/PIC_0002.jpg" image_name="PIC_0002.jpg" id="2"/>
     <image details="08.28.11" thumbnail="/content/gallery/pics/tn_PIC_0003.jpg" url="#/content/gallery/pics/PIC_0003.jpg" image_name="PIC_0003.jpg" id="3"/>
     ...
</images>
The images are sorted by the "id" tag. So, first image will always be oldest. For reverse images order (newest->oldest), remember put the folowing code in your flash module:
Code
var childItems=this.firstChild.childNodes;
childItems.reverse();
One thing I can't figure out - how to disable operations on xml file when folder is hidden or protected by password. I've trying this, but doesn't works :| :
Code
$folder = ivMapperFactory::getMapper('folder')->find(ROOT_DIR . $this->path);
if ($folder->hidden == 'false') {
     my_action...
}else{
     echo "Folder is Hidden";
}
	

Re: Run script when done uploading

Posted: 02 Sep 2011, 11:36
by Nick
You are doing great I see, just use $folder->isHidden() and $folder->hasPassword().