Projects/demandware/upload.php
(Deskargatu)
<?php
$webdav_user="user";
$webdav_password="password";
$webdav_host = "sfcc-host-here";
$version = "version1";
$zip_file_limit = 5; /* X mo max upload size*/
$max_threads=10;
$mh = curl_multi_init();
$files_data = [];
curl_multi_setopt ( $mh , CURLMOPT_MAXCONNECTS, $max_threads);
$script_name = array_shift($argv);
$to_upload = [];
foreach($argv as $dir)
{
if($dir!="." && $dir!="..")
{
if(is_dir($dir))
{
$it = new RecursiveDirectoryIterator("$dir");
foreach(new RecursiveIteratorIterator($it) as $file)
{
$file= $file."";
if(!preg_match("/\/\.+$/", $file) && is_file($file))
{
$to_upload[] = $file;
}
}
}
else if(!preg_match("/zip$/", $dir))
{
$to_upload[] = $dir;
}
}
}
$zips = [];
$last_zip=null;
$current_size=0;
// Create zip files to upload
print "Compresing files : Please wait...\n";
foreach($to_upload as $file)
{
if(!$last_zip || $current_size > 1000000 * $zip_file_limit /*30Mo*/)
{
// Close previous zip
if($last_zip)
{
$last_zip->close();
}
$current_size=0;
$last_zip = new ZipArchive();
$filename = "./cartridges".(count($zips)+1).".zip";
print "\t$filename file\n";
@unlink($filename);
if ($last_zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zips[]=$filename;
}
$current_size+= filesize($file);
$last_zip->addFile($file);
// Remove compression for this file to speedup unzip step on SFCC side
$last_zip->setCompressionName($file, ZipArchive::CM_DEFLATE);
}
print "Created ".count($zips)." files.\n";
// Close last zip
$last_zip->close();
$max_files = count($zips);
$files = [];
function upload($file)
{
global $mh, $files, $webdav_user, $webdav_password, $webdav_host, $version;
print "Add upload for $file.\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$webdav_host/on/demandware.servlet/webdav/Sites/Cartridges/$version/$file");
curl_setopt($ch, CURLOPT_USERPWD, "$webdav_user:$webdav_password");
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
$files[(int)$ch] = ['ch' => $ch, 'file' => $file, 'task' => 'upload', 'fh' => $fh_res];
curl_multi_add_handle($mh,$ch);
return $ch;
}
function unzip($file)
{
global $mh, $files, $webdav_user, $webdav_password, $webdav_host, $version;
print "Add unzip for $file.\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$webdav_host/on/demandware.servlet/webdav/Sites/Cartridges/$version/$file");
curl_setopt($ch, CURLOPT_USERPWD, "$webdav_user:$webdav_password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "method=UNZIP");
$files[(int)$ch] = ['task' => 'unzip', 'ch' => $ch, 'file' => $file];
curl_multi_add_handle($mh,$ch);
return $ch;
}
function delete($file)
{
global $mh, $files, $webdav_user, $webdav_password, $webdav_host, $version;
print "Delete remote and local $file.\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$webdav_host/on/demandware.servlet/webdav/Sites/Cartridges/$version/$file");
curl_setopt($ch, CURLOPT_USERPWD, "$webdav_user:$webdav_password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$files[(int)$ch] = ['ch' => $ch, 'file' => $file, 'task' => 'delete'];
curl_multi_add_handle($mh,$ch);
@unlink($file);
return $ch;
}
$requests_upload = 0;
$requests_unzip = 0;
$requests_delete = 0;
$requests = 0;
$running=0;
$unzippeds = 0;
// Upload / UNZIP / Delete files
$sendzips = $zips;
$prev=-1;
while(count($sendzips) || $running>0)
{
while($running<$max_threads && count($sendzips))
{
$file = array_pop($sendzips);
upload($file);
$running++;
}
curl_multi_exec($mh, $running);
curl_multi_select($mh);
if($requests!=$prev)
{
$prev=$requests;
print "Status - Upload [$requests_upload/$max_files] Unzip [$requests_unzip/$max_files] Delete [$requests_delete/$max_files]\n";
}
while ($info = curl_multi_info_read($mh)) {
$data = $files[(int)$info['handle']];
$file = $data['file'];
$http_code = curl_getinfo($info['handle'], CURLINFO_HTTP_CODE);
if($http_code < 200 || $http_code>=300)
{
// Wait 1 second delay...
sleep(1);
$content = curl_multi_getcontent($info['handle']);
if($data['task']=='upload')
{
print "Error uploading ".$file." $http_code\n";
$fh = $data['fh'];
fclose($fh);
upload($file);
$running++;
}
else if($data['task']=='unzip')
{
print "Error unzipping ".$file." $http_code\n";
unzip($file);
$running++;
}
else if($data['task']=='delete')
{
print "Error deleting ".$file." $http_code\n";
//delete($file);
}
curl_multi_remove_handle($mh, $info['handle']);
}
else
{
// End of upload step
if($data['task']=='upload')
{
$fh = $data['fh'];
fclose($fh);
$requests_upload++;
unzip($file);
$running++;
}
// End of unzip step
if($data['task']=='unzip')
{
$requests_unzip++;
delete($file);
$running++;
}
if($data['task']=='delete')
{
$requests_delete++;
$running++;
}
curl_multi_remove_handle($mh, $info['handle']);
}
}
usleep(500 * 1000);
$requests = $requests_upload +$requests_unzip + $requests_delete;
}
print "Done\n.";