From aeb7c1ac2f78ec538ebcc826eb3f3600b05db425 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Mon, 29 Jul 2013 11:43:40 +0100 Subject: [PATCH 01/12] Added getMediaSource function - looks for setting gallery.mediaSource --- .../gallery/model/gallery/galitem.class.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index 1dc9915..cdf863a 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -23,6 +23,23 @@ * @package gallery */ class galItem extends xPDOSimpleObject { + private $mediaSource; + + private function getMediaSource() { + + //get modMediaSource + $media = new modMediaSource($this->xpdo); //$this->xpdo->loadClass('sources.modMediaSource'); + $mediaSource = $this->xpdo->getOption('gallery.mediaSource',null,1); + + // find our one + $def = $media->getDefaultSource($this->xpdo, $mediaSource); + $def->initialize(); + + $this->mediaSource = $def; + + + } + public function get($k, $format = null, $formatTemplate= null) { switch ($k) { case 'thumbnail': @@ -130,6 +147,7 @@ public function set($k, $v= null, $vType= '') { * @return boolean */ public function upload($file,$albumId) { + $this->getMediaSource(); if (empty($file) || empty($file['tmp_name']) || empty($file['name'])) return false; if (in_array($this->get('id'),array(0,null,''))) return false; /** @var galAlbum $album */ From dd1dc76382c733e3c9eb3b4f316e430eaf76b3c9 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Mon, 29 Jul 2013 13:08:57 +0100 Subject: [PATCH 02/12] Updated item+gallery to use mediasource on 'upload item' --- .../gallery/model/gallery/galalbum.class.php | 50 +++++++++++++------ .../gallery/model/gallery/galitem.class.php | 9 ++-- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/core/components/gallery/model/gallery/galalbum.class.php b/core/components/gallery/model/gallery/galalbum.class.php index 1a78251..e7c2cef 100644 --- a/core/components/gallery/model/gallery/galalbum.class.php +++ b/core/components/gallery/model/gallery/galalbum.class.php @@ -195,35 +195,55 @@ public function ensurePathExists() { return $exists; } - public function uploadItem(galItem $item,$filePath,$name) { + public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $fileName = false; $albumDir = $this->getPath(false); - $targetDir = $this->getPath(); + $targetDir = '/'.str_ireplace(MODX_BASE_PATH, '', $this->getPath()); + + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] albumDir: '.$albumDir); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] targetDir: '.$targetDir); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] filePath: '.$filePath); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] relFilePath: '.$filePath); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] name: '.$name); /* if directory doesnt exist, create it */ - if (!$this->ensurePathExists()) { - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir); - return $fileName; - } - if (!$this->isPathWritable()) { - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); - return $fileName; + if (!$mediaSource->createContainer($targetDir,'/')) { + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory (possibly already exists?): '.$targetDir); + // return $fileName; } + // if (!$this->isPathWritable()) { + // $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); + // return $fileName; + // } /* upload the file */ + $extension = pathinfo($name,PATHINFO_EXTENSION); $shortName = $item->get('id').'.'.$extension; $relativePath = $albumDir.$shortName; $absolutePath = $targetDir.$shortName; - if (@file_exists($absolutePath)) { - @unlink($absolutePath); - } - if (!@move_uploaded_file($filePath,$absolutePath)) { + // if (@file_exists($absolutePath)) { + // @unlink($absolutePath); + // } + // if (!@move_uploaded_file($filePath,$absolutePath)) { + // $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); + // } else { + // } + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] shortName: '.$shortName); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] newName: '.$relativePath); + + $fileName = str_replace(' ','',$relativePath); + + $file = array("name" => $shortName, "tmp_name" => $filePath,"error" => "0"); // emulate a $_FILES object + + $upErrors = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Filename: '.$fileName); + + if($upErrors) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); - } else { - $fileName = str_replace(' ','',$relativePath); + return false; } return $fileName; } diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index cdf863a..f81ca55 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -23,10 +23,10 @@ * @package gallery */ class galItem extends xPDOSimpleObject { - private $mediaSource; + private $mediaSource = false; private function getMediaSource() { - + if($this->mediaSource) return $this->mediaSource; //get modMediaSource $media = new modMediaSource($this->xpdo); //$this->xpdo->loadClass('sources.modMediaSource'); $mediaSource = $this->xpdo->getOption('gallery.mediaSource',null,1); @@ -37,7 +37,7 @@ private function getMediaSource() { $this->mediaSource = $def; - + return $this->mediaSource; } public function get($k, $format = null, $formatTemplate= null) { @@ -147,14 +147,13 @@ public function set($k, $v= null, $vType= '') { * @return boolean */ public function upload($file,$albumId) { - $this->getMediaSource(); if (empty($file) || empty($file['tmp_name']) || empty($file['name'])) return false; if (in_array($this->get('id'),array(0,null,''))) return false; /** @var galAlbum $album */ $album = $this->xpdo->getObject('galAlbum',$albumId); if (empty($album)) return false; - $fileName = $album->uploadItem($this,$file['tmp_name'],$file['name']); + $fileName = $album->uploadItem($this,$file['tmp_name'], $file['name'], $this->getMediaSource()); if (empty($fileName)) { return false; } From 260fe09c4c1289bfe51553a4b06e4b1c57d8ec21 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 09:43:58 +0100 Subject: [PATCH 03/12] Added Multi-upload support with media sources --- .../gallery/model/gallery/galalbum.class.php | 18 +++++-- .../processors/mgr/item/ajaxupload.php | 53 +++++++++++-------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/core/components/gallery/model/gallery/galalbum.class.php b/core/components/gallery/model/gallery/galalbum.class.php index e7c2cef..cf0db5a 100644 --- a/core/components/gallery/model/gallery/galalbum.class.php +++ b/core/components/gallery/model/gallery/galalbum.class.php @@ -199,7 +199,7 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $fileName = false; $albumDir = $this->getPath(false); - $targetDir = '/'.str_ireplace(MODX_BASE_PATH, '', $this->getPath()); + $targetDir = str_ireplace(MODX_BASE_PATH, '', $this->getPath()); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] albumDir: '.$albumDir); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] targetDir: '.$targetDir); @@ -238,8 +238,20 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $file = array("name" => $shortName, "tmp_name" => $filePath,"error" => "0"); // emulate a $_FILES object - $upErrors = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Filename: '.$fileName); + $upErrors = false; + // modFileMediaSource class uses move_uploaded_file - because we create a local file - we cannot use this function and we use streams instead + if(!is_uploaded_file($filePath) && get_class($mediaSource) == 'modFileMediaSource_mysql') { + $input = fopen($filePath, "r"); + $target = fopen($this->getPath(true).$shortName, "w"); + $bytes = stream_copy_to_stream($input, $target); + fclose($input); + fclose($target); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (stream): '.$targetDir.$shortName); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created File: '.$this->getPath(true).$shortName); + } else { + $upErrors = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (upload): '.$fileName); + } if($upErrors) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); diff --git a/core/components/gallery/processors/mgr/item/ajaxupload.php b/core/components/gallery/processors/mgr/item/ajaxupload.php index c52fd29..a768d73 100644 --- a/core/components/gallery/processors/mgr/item/ajaxupload.php +++ b/core/components/gallery/processors/mgr/item/ajaxupload.php @@ -28,29 +28,26 @@ $albumDir = $album.'/'; $targetDir = $modx->call('galAlbum','getFilesPath',array(&$modx)).$albumDir; -$cacheManager = $modx->getCacheManager(); -/* if directory doesnt exist, create it */ -if (!file_exists($targetDir) || !is_dir($targetDir)) { - if (!$cacheManager->writeTree($targetDir)) { - $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir); - return $modx->toJSON(array('error' => 'Could not create directory: ' . $targetDir)); - } -} -/* make sure directory is readable/writable */ -if (!is_readable($targetDir) || !is_writable($targetDir)) { - $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); - return $modx->toJSON(array('error' => 'Could not write to directory: ' . $targetDir)); -} +// $cacheManager = $modx->getCacheManager(); +// /* if directory doesnt exist, create it */ +// if (!file_exists($targetDir) || !is_dir($targetDir)) { +// if (!$cacheManager->writeTree($targetDir)) { +// $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir); +// return $modx->toJSON(array('error' => 'Could not create directory: ' . $targetDir)); +// } +// } +// /* make sure directory is readable/writable */ +// if (!is_readable($targetDir) || !is_writable($targetDir)) { +// $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); +// return $modx->toJSON(array('error' => 'Could not write to directory: ' . $targetDir)); +// } /* upload the file */ -$extension = end(explode('.', $filenm)); +$extension = @end(explode('.', $filenm)); $filename = $item->get('id').'.'.$extension; $relativePath = $albumDir.$filename; $absolutePath = $targetDir.$filename; -if (@file_exists($absolutePath)) { - @unlink($absolutePath); -} if (!empty($_FILES['qqfile'])) { if (!$item->upload($_FILES['qqfile'],$scriptProperties['album'])) { @@ -58,20 +55,34 @@ return $modx->error->failure($modx->lexicon('gallery.item_err_upload')); } } else { - /* Using AJAX upload */ + $modx->log(xPDO::LOG_LEVEL_ERROR,'[GalleryAjaxUpload] filenm '.$filenm); + + $length = 10; + $randomFilename = "/tmp/".substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length).".$extension"; + + /* Using AJAX upload - to tmp file then use the correct media source to upload */ $input = fopen("php://input", "r"); - $target = fopen($absolutePath, "w"); + $target = fopen($randomFilename, "w"); $bytes = stream_copy_to_stream($input, $target); fclose($input); fclose($target); - - if ($bytes == 0) { + + $file = array("name" => $relativePath, "tmp_name" => $randomFilename, "error" => "0"); // emulate a $_FILES object + + $opts = $item->upload($file,$scriptProperties['album']); + $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Album Type: '.$scriptProperties['album']); + $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Upload Type: '.$opts); + + + if ($bytes == 0 || !$item->upload($file,$scriptProperties['album'])) { $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file to '.$absolutePath); $item->remove(); return $modx->toJSON(array('error' => 'gallery.item_err_upload')); } else { $item->set('filename',str_replace(' ','',$relativePath)); } + + @unlink($target); } $item->save(); From 8c704f5b9675013832b7d9a49b7b7f1885572869 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:09:46 +0100 Subject: [PATCH 04/12] Updated batchUpload to use media Source --- .../processors/mgr/item/ajaxupload.php | 2 -- .../processors/mgr/item/batchupload.php | 24 ++++--------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/core/components/gallery/processors/mgr/item/ajaxupload.php b/core/components/gallery/processors/mgr/item/ajaxupload.php index a768d73..6371d3b 100644 --- a/core/components/gallery/processors/mgr/item/ajaxupload.php +++ b/core/components/gallery/processors/mgr/item/ajaxupload.php @@ -69,9 +69,7 @@ $file = array("name" => $relativePath, "tmp_name" => $randomFilename, "error" => "0"); // emulate a $_FILES object - $opts = $item->upload($file,$scriptProperties['album']); $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Album Type: '.$scriptProperties['album']); - $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Upload Type: '.$opts); if ($bytes == 0 || !$item->upload($file,$scriptProperties['album'])) { diff --git a/core/components/gallery/processors/mgr/item/batchupload.php b/core/components/gallery/processors/mgr/item/batchupload.php index 4b4c3c8..6b8e760 100644 --- a/core/components/gallery/processors/mgr/item/batchupload.php +++ b/core/components/gallery/processors/mgr/item/batchupload.php @@ -56,20 +56,6 @@ $targetDir = $modx->call('galAlbum','getFilesPath',array(&$modx)).$scriptProperties['album'].'/'; -$cacheManager = $modx->getCacheManager(); -/* if directory doesnt exist, create it */ -if (!file_exists($targetDir) || !is_dir($targetDir)) { - if (!$cacheManager->writeTree($targetDir)) { - $modx->log(modX::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir); - return $modx->error->failure($modx->lexicon('gallery.directory_err_create',array('directory' => $targetDir))); - } -} -/* make sure directory is readable/writable */ -if (!is_readable($targetDir) || !is_writable($targetDir)) { - $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); - return $modx->error->failure($modx->lexicon('gallery.directory_err_write',array('directory' => $targetDir))); -} - $imagesExts = array('jpg','jpeg','png','gif','bmp'); $use_multibyte = $modx->getOption('use_multibyte',null,false); $encoding = $modx->getOption('modx_charset',null,'UTF-8'); @@ -101,11 +87,11 @@ $newFileName = $item->get('id').'.'.$fileExtension; $newRelativePath = $scriptProperties['album'].'/'.$newFileName; $newAbsolutePath = $targetDir.'/'.$newFileName; - - if (@file_exists($newAbsolutePath)) { - @unlink($newAbsolutePath); - } - if (!@copy($filePathName,$newAbsolutePath)) { + + $file = array("name" => $newRelativePath, "tmp_name" => $filePathName, "error" => "0"); // emulate a $_FILES object + + $success = $item->upload($file,$scriptProperties['album']); + if(!$success) { $errors[] = $modx->lexicon('gallery.file_err_move',array( 'file' => $newFileName, 'target' => $newAbsolutePath, From e0adb4de87d94d1f682d4f16a607aec601f4379c Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:15:07 +0100 Subject: [PATCH 05/12] Updated zip upload to use media source --- .../gallery/model/gallery/import/galzipimport.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/components/gallery/model/gallery/import/galzipimport.class.php b/core/components/gallery/model/gallery/import/galzipimport.class.php index c733d02..3e6c75f 100644 --- a/core/components/gallery/model/gallery/import/galzipimport.class.php +++ b/core/components/gallery/model/gallery/import/galzipimport.class.php @@ -120,10 +120,10 @@ public function importFile($file,array $options = array()) { $newRelativePath = $this->albumId.'/'.$newFileName; $newAbsolutePath = $this->target.'/'.$newFileName; - if (@file_exists($newAbsolutePath)) { - @unlink($newAbsolutePath); - } - if (!@copy($filePathName,$newAbsolutePath)) { + $file = array("name" => $newRelativePath, "tmp_name" => $filePathName, "error" => "0"); // emulate a $_FILES object + + $success = $item->upload($file,$options['album']); + if(!$success) { $errors[] = $this->modx->lexicon('gallery.file_err_move',array( 'file' => $newFileName, 'target' => $newAbsolutePath, From f2b818d80c23fa20bed0c8d0340b7444cae219c1 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:29:08 +0100 Subject: [PATCH 06/12] Tidied comments --- .../gallery/model/gallery/galalbum.class.php | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/core/components/gallery/model/gallery/galalbum.class.php b/core/components/gallery/model/gallery/galalbum.class.php index cf0db5a..8550adf 100644 --- a/core/components/gallery/model/gallery/galalbum.class.php +++ b/core/components/gallery/model/gallery/galalbum.class.php @@ -201,21 +201,10 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $albumDir = $this->getPath(false); $targetDir = str_ireplace(MODX_BASE_PATH, '', $this->getPath()); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] albumDir: '.$albumDir); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] targetDir: '.$targetDir); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] filePath: '.$filePath); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] relFilePath: '.$filePath); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] name: '.$name); - /* if directory doesnt exist, create it */ if (!$mediaSource->createContainer($targetDir,'/')) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory (possibly already exists?): '.$targetDir); - // return $fileName; } - // if (!$this->isPathWritable()) { - // $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir); - // return $fileName; - // } /* upload the file */ @@ -224,13 +213,6 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $relativePath = $albumDir.$shortName; $absolutePath = $targetDir.$shortName; - // if (@file_exists($absolutePath)) { - // @unlink($absolutePath); - // } - // if (!@move_uploaded_file($filePath,$absolutePath)) { - // $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); - // } else { - // } $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] shortName: '.$shortName); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] newName: '.$relativePath); @@ -238,7 +220,7 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $file = array("name" => $shortName, "tmp_name" => $filePath,"error" => "0"); // emulate a $_FILES object - $upErrors = false; + $success = true; // modFileMediaSource class uses move_uploaded_file - because we create a local file - we cannot use this function and we use streams instead if(!is_uploaded_file($filePath) && get_class($mediaSource) == 'modFileMediaSource_mysql') { $input = fopen($filePath, "r"); @@ -249,14 +231,14 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (stream): '.$targetDir.$shortName); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created File: '.$this->getPath(true).$shortName); } else { - $upErrors = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); + $success = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (upload): '.$fileName); } - if($upErrors) { - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); - return false; - } + // if(!$success) { + // $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath); + // return false; + // } return $fileName; } From 60f577313d6476cf8d2ae8c4800a6276e163b2dd Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:46:57 +0100 Subject: [PATCH 07/12] Updated img paths for output - if a media source has a baseUrl it is used --- .../gallery/model/gallery/galitem.class.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index f81ca55..ba3287c 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -52,6 +52,12 @@ public function get($k, $format = null, $formatTemplate= null) { $format['src'] = $this->getSiteUrl(); $format['src'] .= $this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; } + + $ms = $this->getMediaSource(); + if($ms->getBaseUrl() != '/') { + $format['src'] = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; + } + $url = $value.'&'.http_build_query($format,'','&'); if ($this->xpdo->getOption('xhtml_urls',null,false)) { $value = str_replace('&','&',$url); @@ -69,12 +75,24 @@ public function get($k, $format = null, $formatTemplate= null) { $format['src'] = $this->getSiteUrl(); $format['src'] .= $this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; } + + $ms = $this->getMediaSource(); + if($ms->getBaseUrl() != '/') { + $format['src'] = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; + } + $value = $this->getPhpThumbUrl().'&'.http_build_query($format,'','&'); $value = $this->xpdo->getOption('xhtml_urls',null,false) ? str_replace('&','&',$value) : $value; break; case 'absoluteImage': $siteUrl = $this->getSiteUrl(); $value = $siteUrl.$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$this->get('filename'); + + $ms = $this->getMediaSource(); + if($ms->getBaseUrl() != '/') { + $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; + } + break; case 'relativeImage': $baseUrl = $this->getOption('base_url'); @@ -84,6 +102,12 @@ public function get($k, $format = null, $formatTemplate= null) { } else { $value = str_replace($baseUrl,'',$path); } + + $ms = $this->getMediaSource(); // for absolute + relative the link NEEDS the http:// domain + if($ms->getBaseUrl() != '/') { + $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; + } + break; case 'filesize': $filename = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$this->get('filename'); From 7cab21c7079cdc6197a69e15664a66c5b1e1aba6 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:47:19 +0100 Subject: [PATCH 08/12] Tidied up - removed some comments --- core/components/gallery/model/gallery/galalbum.class.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/components/gallery/model/gallery/galalbum.class.php b/core/components/gallery/model/gallery/galalbum.class.php index 8550adf..0497b14 100644 --- a/core/components/gallery/model/gallery/galalbum.class.php +++ b/core/components/gallery/model/gallery/galalbum.class.php @@ -213,9 +213,6 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $relativePath = $albumDir.$shortName; $absolutePath = $targetDir.$shortName; - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] shortName: '.$shortName); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] newName: '.$relativePath); - $fileName = str_replace(' ','',$relativePath); $file = array("name" => $shortName, "tmp_name" => $filePath,"error" => "0"); // emulate a $_FILES object @@ -228,11 +225,8 @@ public function uploadItem(galItem $item,$filePath,$name,$mediaSource) { $bytes = stream_copy_to_stream($input, $target); fclose($input); fclose($target); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (stream): '.$targetDir.$shortName); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created File: '.$this->getPath(true).$shortName); } else { $success = $mediaSource->uploadObjectsToContainer($targetDir,array($file)); - $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Created (upload): '.$fileName); } // if(!$success) { From 5cf521df8aa3f87a226daba3bbeed62a588e861c Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 10:56:48 +0100 Subject: [PATCH 09/12] Added image delete (mediaSource) --- .../gallery/model/gallery/galitem.class.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index ba3287c..5acfad1 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -88,10 +88,10 @@ public function get($k, $format = null, $formatTemplate= null) { $siteUrl = $this->getSiteUrl(); $value = $siteUrl.$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$this->get('filename'); - $ms = $this->getMediaSource(); - if($ms->getBaseUrl() != '/') { - $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; - } + // $ms = $this->getMediaSource(); + // if($ms->getBaseUrl() != '/') { + // $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; + // } break; case 'relativeImage': @@ -103,10 +103,10 @@ public function get($k, $format = null, $formatTemplate= null) { $value = str_replace($baseUrl,'',$path); } - $ms = $this->getMediaSource(); // for absolute + relative the link NEEDS the http:// domain - if($ms->getBaseUrl() != '/') { - $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename; - } + // $ms = $this->getMediaSource(); // for absolute + relative the link NEEDS the http:// domain + // if($ms->getBaseUrl() != '/') { + // $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$baseUrl; + // } break; case 'filesize': @@ -209,7 +209,9 @@ public function remove(array $ancestors = array()) { $filename = $this->get('filename'); if (!empty($filename)) { $filename = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$filename; - if (!@unlink($filename)) { + $filename = str_ireplace(MODX_BASE_PATH, '', $filename); + $ms = $this->getMediaSource(); + if (!@$ms->removeObject($filename)) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to remove the attachment file at: '.$filename); } } From 308265f146dbb5d0663b7bfa387adf1cf44f6169 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 11:12:55 +0100 Subject: [PATCH 10/12] Added new system setting for default media source --- _build/data/transport.settings.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/_build/data/transport.settings.php b/_build/data/transport.settings.php index 261372d..481f272 100644 --- a/_build/data/transport.settings.php +++ b/_build/data/transport.settings.php @@ -90,6 +90,15 @@ 'area' => '', ),'',true,true); +$settings['gallery.mediaSource']= $modx->newObject('modSystemSetting'); +$settings['gallery.mediaSource']->fromArray(array( + 'key' => 'gallery.mediaSource', + 'value' => 1, + 'xtype' => 'modx-combo-source', + 'namespace' => 'gallery', + 'area' => '', +),'',true,true); + /* $settings['gallery.']= $modx->newObject('modSystemSetting'); $settings['gallery.']->fromArray(array( From 5c8acd19b9668dcaa9905e7aadb784985162b7c2 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 14:53:52 +0100 Subject: [PATCH 11/12] Updated absolute paths on image name --- .../gallery/elements/snippets/snippet.gallery.php | 2 +- core/components/gallery/model/gallery/galitem.class.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/core/components/gallery/elements/snippets/snippet.gallery.php b/core/components/gallery/elements/snippets/snippet.gallery.php index 0154b5c..a49c12e 100644 --- a/core/components/gallery/elements/snippets/snippet.gallery.php +++ b/core/components/gallery/elements/snippets/snippet.gallery.php @@ -106,7 +106,7 @@ $itemArray['cls'] .= ' '.$activeCls; } $itemArray['filename'] = basename($item->get('filename')); - $itemArray['image_absolute'] = $filesUrl.$item->get('filename'); + $itemArray['image_absolute'] = $item->get('base_url').$filesUrl.$item->get('filename'); $itemArray['fileurl'] = $itemArray['image_absolute']; $itemArray['filepath'] = $filesPath.$item->get('filename'); $itemArray['filesize'] = $item->get('filesize'); diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index 5acfad1..38f17f2 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -116,6 +116,14 @@ public function get($k, $format = null, $formatTemplate= null) { break; case 'image_path': $value = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$this->get('filename'); + break; + case 'base_url': + $ms = $this->getMediaSource(); + $value=''; + if($ms->getBaseUrl() != '/') { + $value = $ms->getBaseUrl(); + } + break; default: $value = parent::get($k,$format,$formatTemplate); From c24edb11cfc748b553828e59f2329e9ea5287d56 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Tue, 30 Jul 2013 15:06:22 +0100 Subject: [PATCH 12/12] Updated method that gets the selected media source - now works better in manager --- core/components/gallery/model/gallery/galitem.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/components/gallery/model/gallery/galitem.class.php b/core/components/gallery/model/gallery/galitem.class.php index 38f17f2..243d7f9 100644 --- a/core/components/gallery/model/gallery/galitem.class.php +++ b/core/components/gallery/model/gallery/galitem.class.php @@ -28,11 +28,12 @@ class galItem extends xPDOSimpleObject { private function getMediaSource() { if($this->mediaSource) return $this->mediaSource; //get modMediaSource - $media = new modMediaSource($this->xpdo); //$this->xpdo->loadClass('sources.modMediaSource'); $mediaSource = $this->xpdo->getOption('gallery.mediaSource',null,1); - // find our one - $def = $media->getDefaultSource($this->xpdo, $mediaSource); + $def = $this->xpdo->getObject('sources.modMediaSource',array( + 'id' => $mediaSource, + )); + $def->initialize(); $this->mediaSource = $def;