/**
  * Returns metadata for an asset.
  *
  * @param AssetFileModel $asset    The asset model to parse
  * @param string         $property String in dot notation that sets the root metadata property
  *
  * @return array|string The asset's metadata
  */
 public function getAssetMetadata($asset, $property = null)
 {
     $getId3 = $this->_getGetId3();
     if (!$asset instanceof AssetFileModel) {
         throw new Exception('(Asset Metadata) The plugin only works with asset models.');
     }
     $sourceType = craft()->assetSources->getSourceTypeById($asset->sourceId);
     if ($sourceType->isRemote()) {
         $config = craft()->config->get('remoteAssetSources', 'assetmetadata');
         // Makes a local copy of the file if it's from a remote source.
         $path = AssetsHelper::getTempFilePath($asset->getExtension());
         $this->_downloadRemoteFile($asset->getUrl(), $path, $config['downloadSize']);
         // Pad truncated file to original size.
         if ($config['padTruncatedFiles'] && $config['downloadSize'] && $asset->size > $config['downloadSize']) {
             $this->_padFile($path, $asset->size - $config['downloadSize']);
         }
         $metadata = $getId3->analyze($path);
         IOHelper::deleteFile($path);
     } else {
         $path = $sourceType->getImageSourcePath($asset);
         $metadata = $getId3->analyze($path);
     }
     // Merges ID3 tags and stores them in a "comments" property.
     getid3_lib::CopyTagsToComments($metadata);
     // Removes troublesome properties.
     $this->_removeProperties($metadata);
     return $this->_getValueByKey($property, $metadata);
 }
Exemplo n.º 2
0
 /**
  * Copies the database dump to the production server via SFTP
  *
  * @param BaseModel $settings The task's settings
  * @param mixed $arg An optional second argument
  *
  * @return bool
  */
 public function copyBackup($settings, $arg = null)
 {
     $keepAfterCopy = $this->getSettings()['backup']['keepBackup'] === '1' ? true : false;
     $pathToBackup = craft()->path->getDbBackupPath() . StringHelper::toLowerCase(IOHelper::cleanFilename($settings->backupFileName));
     $destination = $this->getSettings()['copyBackup']['destination'];
     $destination = rtrim($destination, '/') . '/';
     $destination = $destination . basename($pathToBackup);
     $sshHostname = $this->getSettings()['ssh']['remote']['hostname'];
     $sshUsername = $this->getSettings()['ssh']['remote']['username'];
     $sshPassword = craft()->goLive_security->decrypt($this->getSettings()['ssh']['remote']['password']);
     $sftp = new Net_SFTP($sshHostname);
     $sftp->login($sshUsername, $sshPassword);
     $sftpOutput = $sftp->put($destination, $pathToBackup, NET_SFTP_LOCAL_FILE);
     if (!$keepAfterCopy) {
         IOHelper::deleteFile($pathToBackup);
     }
     if ($sftpOutput === false) {
         return false;
     } else {
         return true;
     }
 }
Exemplo n.º 3
0
 /**
  * Saves image as webp
  *
  * @param $imageInstance
  * @param $path
  * @param $sourceExtension
  * @param $saveOptions
  * @throws Exception
  */
 private function _saveAsWebp($imageInstance, $path, $sourceExtension, $saveOptions)
 {
     if ($this->getSetting('useCwebp')) {
         // save temp file
         $tempFile = $this->_saveTemporaryFile($imageInstance, $sourceExtension);
         // convert to webp with cwebp
         $command = escapeshellcmd($this->getSetting('cwebpPath') . ' ' . $this->getSetting('cwebpOptions') . ' -q ' . $saveOptions['webp_quality'] . ' ' . $tempFile . ' -o ' . $path);
         $r = shell_exec($command);
         if (!IOHelper::fileExists($path)) {
             throw new Exception(Craft::t('Save operation failed'));
         }
         // delete temp file
         IOHelper::deleteFile($tempFile);
     } else {
         if ($this->imageDriver === 'gd') {
             $instance = $imageInstance->getGdResource();
             if (false === \imagewebp($instance, $path, $saveOptions['webp_quality'])) {
                 throw new Exception(Craft::t('Save operation failed'));
             }
             // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files)
             if (filesize($path) % 2 == 1) {
                 file_put_contents($path, "", FILE_APPEND);
             }
         }
         if ($this->imageDriver === 'imagick') {
             $instance = $imageInstance->getImagick();
             $instance->setImageFormat('webp');
             $instance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
             $instance->setBackgroundColor(new \ImagickPixel('transparent'));
             $instance->setImageCompressionQuality($saveOptions['webp_quality']);
             $imagickOptions = $saveOptions['webp_imagick_options'];
             if ($imagickOptions && count($imagickOptions) > 0) {
                 foreach ($imagickOptions as $key => $val) {
                     $instance->setOption('webp:' . $key, $val);
                 }
             }
             $instance->writeImage($path);
         }
     }
 }
 /**
  * Download
  */
 public function download($pluginHandle)
 {
     Craft::log(__METHOD__, LogLevel::Info, true);
     // -------------------------------
     // Get ready to download & unzip
     // -------------------------------
     $return = array('success' => false);
     $pluginComponent = craft()->plugins->getPlugin($pluginHandle, false);
     // plugin path
     $pluginZipDir = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . "/";
     $pluginZipPath = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . ".zip";
     // remote plugin zip url
     $remotePlugin = $this->_getRemotePlugin($pluginHandle);
     if (!$remotePlugin) {
         $return['msg'] = "Couldn't get plugin last version";
         Craft::log(__METHOD__ . ' : Could not get last version', LogLevel::Info, true);
         return $return;
     }
     $remotePluginZipUrl = $remotePlugin['xml']->enclosure['url'];
     // -------------------------------
     // Download & Install
     // -------------------------------
     try {
         // download remotePluginZipUrl to pluginZipPath
         $client = new \Guzzle\Http\Client();
         $request = $client->get($remotePluginZipUrl);
         $response = $request->send();
         $body = $response->getBody();
         // Make sure we're at the beginning of the stream.
         $body->rewind();
         // Write it out to the file
         IOHelper::writeToFile($pluginZipPath, $body->getStream(), true);
         // Close the stream.
         $body->close();
         // unzip pluginZipPath into pluginZipDir
         Zip::unzip($pluginZipPath, $pluginZipDir);
         $contents = IOHelper::getFiles($pluginZipDir);
         $pluginUnzipDir = false;
         foreach ($contents as $content) {
             if (strrpos($content, "__MACOSX") !== 0) {
                 $pluginUnzipDir = $content;
             }
         }
         // move files we want to keep from their current location to unzipped location
         // keep : .git
         if (file_exists(CRAFT_PLUGINS_PATH . $pluginHandle . '/.git') && !$pluginUnzipDir . '/.git') {
             IOHelper::rename(CRAFT_PLUGINS_PATH . $pluginHandle . '/.git', $pluginUnzipDir . '/.git');
         }
         //rename($path, $newName, $suppressErrors = false)
         // remove current files
         // make a backup of existing plugin (to storage ?) ?
         //deleteFolder($path, $suppressErrors = false)
         IOHelper::deleteFolder(CRAFT_PLUGINS_PATH . $pluginHandle);
         // move new files to final destination
         IOHelper::rename($pluginUnzipDir . '/' . $pluginHandle . '/', CRAFT_PLUGINS_PATH . $pluginHandle);
         // delete zip
         IOHelper::deleteFile($pluginZipPath);
     } catch (\Exception $e) {
         $return['msg'] = $e->getMessage();
         Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true);
         return $return;
     }
     // remove download files
     try {
         IOHelper::deleteFolder($pluginZipDir);
         IOHelper::deleteFolder($pluginZipPath);
     } catch (\Exception $e) {
         $return['msg'] = $e->getMessage();
         Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true);
         return $return;
     }
     Craft::log(__METHOD__ . ' : Success : ', LogLevel::Info, true);
     $return['success'] = true;
     return $return;
 }
 public function download($pluginHandle)
 {
     Craft::log(__METHOD__, LogLevel::Info, true);
     // -------------------------------
     // Get ready to download & unzip
     // -------------------------------
     $return = array('success' => false);
     $filesystem = new Filesystem();
     $pluginComponent = craft()->plugins->getPlugin($pluginHandle, false);
     // plugin path
     $pluginZipDir = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . "/";
     $pluginZipPath = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . ".zip";
     // remote plugin zip url
     $remotePlugin = $this->_getRemotePlugin($pluginHandle);
     if (!$remotePlugin) {
         $return['msg'] = "Couldn't get plugin last version";
         Craft::log(__METHOD__ . ' : Could not get last version', LogLevel::Info, true);
         return $return;
     }
     $remotePluginZipUrl = $remotePlugin['xml']->enclosure['url'];
     // -------------------------------
     // Download & Install
     // -------------------------------
     try {
         // download remotePluginZipUrl to pluginZipPath
         $zipContents = file_get_contents($remotePluginZipUrl);
         file_put_contents($pluginZipPath, $zipContents);
         // unzip pluginZipPath into pluginZipDir
         $unzipper = new Unzip();
         $contents = $unzipper->extract($pluginZipPath, $pluginZipDir);
         // remove current files
         // make a backup of existing plugin (to storage ?) ?
         $filesystem->rename(CRAFT_PLUGINS_PATH . $pluginHandle, CRAFT_PLUGINS_PATH . '_old_' . $pluginHandle);
         // move new files to final destination
         $filesystem->rename($pluginZipDir . $contents[0] . '/' . $pluginHandle . '/', CRAFT_PLUGINS_PATH . $pluginHandle);
     } catch (\Exception $e) {
         $return['msg'] = $e->getMessage();
         Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true);
         return $return;
     }
     // remove download files
     try {
         $filesystem->remove($pluginZipDir);
         $filesystem->remove(CRAFT_PLUGINS_PATH . '_old_' . $pluginHandle);
         if (!IOHelper::deleteFile($pluginZipPath)) {
             Craft::log(__METHOD__ . ' : Crashed : ' . "Could not remove plugin zip file", LogLevel::Info, true);
         }
     } catch (\Exception $e) {
         $return['msg'] = $e->getMessage();
         Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true);
         return $return;
     }
     Craft::log(__METHOD__ . ' : Success : ', LogLevel::Info, true);
     $return['success'] = true;
     return $return;
 }