/**
  * Store the contents of a folder on a CDN. 
  * 
  * If processReferences is set, relative URL references are attempted to be 
  * detected and stored remotely as well, with the file to be stored rewritten 
  * to refer to the CDN value. This really is only useful for CSS 
  *
  * @param string $folder
  * @param boolean $processReferences 
  */
 public function storeThemeFile($toCdn, $file, $forceUpdate = false, $processReferences = false)
 {
     $mtime = @filemtime($file);
     $relativeName = self::CDN_THEME_PREFIX . '/' . $mtime . '/' . trim(str_replace(Director::baseFolder(), '', $file), '/');
     if (!$forceUpdate) {
         // see if the file already exists, if not we do NOT do an update
         $reader = $this->contentService->findReaderFor($toCdn, $relativeName);
         if ($reader && $reader->exists()) {
             return $reader->getURL();
         }
     }
     $clear = false;
     if ($processReferences) {
         $clear = true;
         $file = $this->processFileReferences($toCdn, $file, $forceUpdate);
     }
     // otherwise, lets get a content writer
     $writer = $this->contentService->getWriter($toCdn);
     try {
         $writer->write($file, $relativeName);
     } catch (Exception $e) {
         SS_Log::log($e, SS_Log::WARN);
     }
     if ($clear && strpos($file, '.cdn') > 0) {
         @unlink($file);
     }
     $id = $writer->getContentId();
     return $writer->getReader()->getURL();
 }
 public function CDNPath($assetPath, $uploadMissing = false, $verify = false)
 {
     $current = $this->currentThemeCdn();
     if ($current && (Director::isLive() || isset($_GET['stage']) && $_GET['stage'] == 'Live')) {
         $store = $current->StoreIn;
         // if we want to upload missing files, verify their existence.
         if (!$verify && $uploadMissing) {
             $verify = true;
         }
         $mtime = @filemtime(Director::baseFolder() . '/' . $assetPath);
         $timedAssetPath = $mtime . '/' . $assetPath;
         $reader = $this->contentService->findReaderFor($store, $timedAssetPath);
         if ($reader && (!$verify || $reader->exists())) {
             return $reader->getURL();
         }
         if ($uploadMissing) {
             if (strpos($assetPath, '.css')) {
                 // if we're a relative path, make absolute
                 $fullPath = $assetPath;
                 if ($assetPath[0] != '/') {
                     $fullPath = Director::baseFolder() . '/' . $assetPath;
                 }
                 if (!file_exists($fullPath)) {
                     return $assetPath;
                 }
                 // upload all references too
                 return $this->contentDelivery->storeThemeFile($store, $fullPath, false, true);
             }
             // otherwise just upload
             $writer = $this->getWriter();
             // otherwise, we need to write the file
             $writer->write(Director::baseFolder() . '/' . $assetPath, $timedAssetPath);
             return $writer->getReader()->getURL();
         }
     }
     return $assetPath;
 }