/** * @return mixed */ public function getSize() { if (!$this->_size) { $this->_size = IOHelper::getFileSize($this->getRealPath()); } return $this->_size; }
/** * Saves log messages in files. * * @param array $logs list of log messages */ protected function processLogs($logs) { $logFile = IOHelper::normalizePathSeparators($this->getLogPath() . '/' . $this->getLogFile()); if (IOHelper::getFileSize($logFile) > $this->getMaxFileSize() * 1024) { $this->rotateFiles(); } $lock = craft()->config->get('useLockWhenWritingToFile') === true; $fp = @fopen($logFile, 'a'); if ($lock) { @flock($fp, LOCK_EX); } foreach ($logs as $log) { $message = LoggingHelper::redact($log[0]); $level = $log[1]; $category = $log[2]; $time = $log[3]; $force = isset($log[4]) && $log[4] == true ? true : false; @fwrite($fp, $this->formatLogMessageWithForce($message, $level, $category, $time, $force)); } @fwrite($fp, PHP_EOL . '******************************************************************************************************' . PHP_EOL); if ($lock) { @flock($fp, LOCK_UN); } @fclose($fp); }
/** * Saves log messages in files. * * @param array $logs The list of log messages * * @return null */ protected function processLogs($logs) { $types = array(); foreach ($logs as $log) { $message = LoggingHelper::redact($log[0]); $level = $log[1]; $category = $log[2]; $time = $log[3]; $force = isset($log[4]) && $log[4] == true ? true : false; $plugin = isset($log[5]) ? StringHelper::toLowerCase($log[5]) : 'craft'; if (isset($types[$plugin])) { $types[$plugin] .= $this->formatLogMessageWithForce($message, $level, $category, $time, $force, $plugin); } else { $types[$plugin] = $this->formatLogMessageWithForce($message, $level, $category, $time, $force, $plugin); } } foreach ($types as $plugin => $text) { $text .= PHP_EOL . '******************************************************************************************************' . PHP_EOL; $this->setLogFile($plugin . '.log'); $logFile = IOHelper::normalizePathSeparators($this->getLogPath() . '/' . $this->getLogFile()); // Check the config setting first. Is it set to true? if (craft()->config->get('useWriteFileLock') === true) { $lock = true; } else { if (craft()->config->get('useWriteFileLock') === false) { $lock = false; } else { if (craft()->cache->get('useWriteFileLock') === 'yes') { $lock = true; } else { $lock = false; } } } $fp = @fopen($logFile, 'a'); if ($lock) { @flock($fp, LOCK_EX); } if (IOHelper::getFileSize($logFile) > $this->getMaxFileSize() * 1024) { $this->rotateFiles(); if ($lock) { @flock($fp, LOCK_UN); } @fclose($fp); if ($lock) { IOHelper::writeToFile($logFile, $text, false, true, false); } else { IOHelper::writeToFile($logFile, $text, false, true, true); } } else { @fwrite($fp, $text); if ($lock) { @flock($fp, LOCK_UN); } @fclose($fp); } } }
/** * Constructor * * @param null $imagePath * @param null $imageUrl */ public function __construct($imagePath = null, $imageUrl = null) { if ($imagePath != 'null') { $this['path'] = $imagePath; $imageInfo = @getimagesize($imagePath); $this['width'] = $imageInfo[0]; $this['height'] = $imageInfo[1]; $this['extension'] = IOHelper::getExtension($imagePath); $this['mimeType'] = IOHelper::getMimeType($imagePath); $this['size'] = IOHelper::getFileSize($imagePath); } if ($imageUrl != 'null') { $this['url'] = $imageUrl; } }
/** * Constructor * * @param null $imagePath * @param null $imageUrl */ public function __construct($imagePath = null, $imageUrl = null, $paths = null, $transform = null) { if ($imagePath != 'null') { $this['path'] = $imagePath; $this['extension'] = IOHelper::getExtension($imagePath); $this['mimeType'] = IOHelper::getMimeType($imagePath); $this['size'] = IOHelper::getFileSize($imagePath); $imageInfo = @getimagesize($imagePath); if (is_array($imageInfo) && $imageInfo[0] !== '' && $imageInfo[1] !== '') { $this['width'] = $imageInfo[0]; $this['height'] = $imageInfo[1]; } else { // Couldn't get size. Calculate size based on source image and transform. $sourceImageInfo = @getimagesize($paths->sourcePath . $paths->sourceFilename); $sourceSize = new \Imagine\Image\Box($sourceImageInfo[0], $sourceImageInfo[1]); $targetCrop = craft()->imager->getCropSize($sourceSize, $transform); $this['width'] = $targetCrop->getWidth(); $this['height'] = $targetCrop->getHeight(); } } if ($imageUrl != 'null') { $this['url'] = $imageUrl; } }
/** * Create a resumable video upload to YouTube. * * @param AssetFileModel $asset * @param \Google_Service_YouTube_Video $video * * @throws Exception * * @return bool|string */ protected function uploadVideo(AssetFileModel $asset, \Google_Service_YouTube_Video $video) { // Get file by asset $file = $this->getAssetFile($asset); // Specify the size of each chunk of data, in bytes. Set a higher value for // reliable connection as fewer chunks lead to faster uploads. Set a lower // value for better recovery on less reliable connections. $chunkSizeBytes = 1 * 1024 * 1024; // Verify the client if ($this->client instanceof \Google_Client) { // Setting the defer flag to true tells the client to return a request which can be called // with ->execute(); instead of making the API call immediately. $this->client->setDefer(true); // Create a request for the API's videos.insert method to create and upload the video. $insertRequest = $this->youtube->videos->insert('status,snippet', $video); // Create a MediaFileUpload object for resumable uploads. $media = new \Google_Http_MediaFileUpload($this->client, $insertRequest, 'video/*', null, true, $chunkSizeBytes); $media->setFileSize(IOHelper::getFileSize($file)); // Read the media file and upload it chunk by chunk. $status = $this->uploadChunks($file, $media, $chunkSizeBytes); // If you want to make other calls after the file upload, set setDefer back to false $this->client->setDefer(false); // Return the status return $status; } // Or die throw new Exception(Craft::t('Unable to authenticate the YouTube API client')); }
/** * Get a local image source to use for transforms. * * @param $file * * @throws Exception * @return mixed */ public function getLocalImageSource(AssetFileModel $file) { $sourceType = craft()->assetSources->getSourceTypeById($file->sourceId); $imageSourcePath = $sourceType->getImageSourcePath($file); if (!IOHelper::fileExists($imageSourcePath) || IOHelper::getFileSize($imageSourcePath) == 0) { if (!$sourceType->isRemote()) { throw new Exception(Craft::t('Image “{file}” cannot be found.', array('file' => $file->filename))); } // Delete it just in case it's a 0-byter IOHelper::deleteFile($imageSourcePath, true); $localCopy = $sourceType->getLocalCopy($file); if (!IOHelper::fileExists($localCopy) || IOHelper::getFileSize($localCopy) == 0) { throw new Exception(Craft::t('Tried to download the source file for image “{file}”, but it was 0 bytes long.', array('file' => $file->filename))); } $this->storeLocalSource($localCopy, $imageSourcePath); $this->queueSourceForDeletingIfNecessary($imageSourcePath); IOHelper::deleteFile($localCopy, true); } $file->setTransformSource($imageSourcePath); return $imageSourcePath; }
/** * @param $tempFileName * @param $originalSize * @param $extension * @param $minQuality * @param $maxQuality * @param int $step * * @return bool */ private function _autoGuessImageQuality($tempFileName, $originalSize, $extension, $minQuality, $maxQuality, $step = 0) { // Give ourselves some extra time. @set_time_limit(30); if ($step == 0) { $tempFileName = IOHelper::getFolderName($tempFileName).IOHelper::getFileName($tempFileName, false).'-temp.'.$extension; } // Find our target quality by splitting the min and max qualities $midQuality = (int)ceil($minQuality + (($maxQuality - $minQuality) / 2)); // Set the min and max acceptable ranges. .10 means anything between 90% and 110% of the original file size is acceptable. $acceptableRange = .10; clearstatcache(); // Generate a new temp image and get it's file size. $this->_image->save($tempFileName, $this->_getSaveOptions($midQuality, $extension)); $newFileSize = IOHelper::getFileSize($tempFileName); // If we're on step 10 OR we're within our acceptable range threshold OR midQuality = maxQuality (1 == 1), // let's use the current image. if ($step == 10 || abs(1 - $originalSize / $newFileSize) < $acceptableRange || $midQuality == $maxQuality) { clearstatcache(); // Generate one last time. $this->_image->save($tempFileName, $this->_getSaveOptions($midQuality)); return true; } $step++; if ($newFileSize > $originalSize) { return $this->_autoGuessImageQuality($tempFileName, $originalSize, $extension, $minQuality, $midQuality, $step); } // Too much. else { return $this->_autoGuessImageQuality($tempFileName, $originalSize, $extension, $midQuality, $maxQuality, $step); } }
/** * Upload a file to Rackspace. * * @param $targetUri * @param $sourceFile * * @return bool */ private function _uploadFile($targetUri, $sourceFile) { $fileSize = IOHelper::getFileSize($sourceFile); $fp = fopen($sourceFile, "r"); $headers = array('Content-type: ' . IOHelper::getMimeType($sourceFile), 'Content-length: ' . $fileSize); $curlOptions = array(CURLOPT_UPLOAD => true, CURLOPT_INFILE => $fp, CURLOPT_INFILESIZE => $fileSize); $targetUri = $this->_prepareRequestURI($this->getSettings()->container, $targetUri); $this->_doAuthenticatedRequest(static::RACKSPACE_STORAGE_OPERATION, $targetUri, 'PUT', $headers, $curlOptions); fclose($fp); return true; }
private function _updateAsset($asset, $image, $path) { // Update our model $asset->size = IOHelper::getFileSize($path); $asset->width = $image->getWidth(); $asset->height = $image->getHeight(); // Then, make sure we update the asset info as stored in the database $fileRecord = AssetFileRecord::model()->findById($asset->id); $fileRecord->size = $asset->size; $fileRecord->width = $asset->width; $fileRecord->height = $asset->height; $fileRecord->dateModified = IOHelper::getLastTimeModified($path); $fileRecord->save(false); }