<commit_before><?php
/**
 * This file is part of Vegas package
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Amsterdam Standard Sp. Z o.o.
 * @homepage https://bitbucket.org/amsdard/vegas-phalcon
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Vegas\Media;
namespace Vegas\Media;

use Vegas\Media\Model\File as FileModel;
use Vegas\Media\File\Exception as FileException;
use Vegas\Media\Uploader\Exception\CannotSaveFileInHardDriveException;
use WideImage;

class Helper
{
    /**
     * Moves files from record (from "files" property of collection) from temporary destination
     * to the original destination.
     * Example of $record:
     *      {
     *          "_id": ObjectId("53195f675c65d1ba0b7b23c7"),
     *          "created_at": NumberInt(1394171751),
     *          "files": {                                <---- This line is the most important
     *              "0": {
     *                  "title": "Our Beautiful File",
     *                  "file_id": "53195f665c65d1ba0b7b23c6"
     *              }
     *          },
     *          "updated_at": NumberInt(1394171751)
     *      }
     *
     * @param Db\Mapping\File $files
     */
    public static final function moveFilesFrom(\Vegas\Media\Db\Mapping\File $files)
    {
        foreach($files as $file) {
            self::moveFile($file);
        }
    }

    /**
     * Move file from the temporary destination to the original destination
     *
     * @param File\Decorator $file
     * @throws File\Exception
     */
    public static final function moveFile(\Vegas\Media\File\Decorator $file)
    {
        $file = $file->getRecord();

        try {
            if($file->is_temp) {
                rename(
                    $file->temp_destination . '/' . $file->temp_name,
                    $file->original_destination . '/' . $file->temp_name
                );

                if(!file_exists($file->original_destination . '/' . $file->temp_name)) {
                    throw new CannotSaveFileInHardDriveException();
                }

                $file->is_temp = false;
                $file->expire = null;
                $file->save();
            }
        } catch (\Exception $exception) {
            throw new FileException($exception->getMessage());
        }
    }

    /**
     * Generates a thumbnails of the files associated with the record
     *
     * @param Db\Mapping\File $files
     * @param array $size
     */
    public static final function generateThumbnailsFrom(\Vegas\Media\Db\Mapping\File $files, array $size)
    {
        foreach($files as $file) {
            self::generateThumbnail($file, $size);
        }
    }

    /**
     * Generates a thumbnails of the images in the following location:
     * {ORIGINAL_DESTINATION_OF_THE_FILE}/thumbnails/filename.{ext}
     *
     * @param File\Decorator $file
     * @param array $size
     */
    public static final function generateThumbnail(\Vegas\Media\File\Decorator $file, array $size = array('width' => 168, 'height' => 120))
    {
        $file = $file->getRecord();

        if(!empty($file->original_destination) && isset($size['width']) && isset($size['height'])) {

            // ie. string(47) "/var/www/vegas/public/uploads/5326acd311dd4.jpg"
            $filePath = $file->original_destination . '/' . $file->temp_name;

            if (!file_exists($file->original_destination . '/thumbnails')) {
                mkdir($file->original_destination . '/thumbnails', 0777, true);
            }

            WideImage::load($filePath)
                ->resize($size['width'], $size['height'], 'outside')<commit_msg>media helper small fix<commit_after><?php
/**
 * This file is part of Vegas package
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Amsterdam Standard Sp. Z o.o.
 * @homepage https://bitbucket.org/amsdard/vegas-phalcon
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Vegas\Media;
namespace Vegas\Media;

use Vegas\Media\Model\File as FileModel;
use Vegas\Media\File\Exception as FileException;
use Vegas\Media\Uploader\Exception\CannotSaveFileInHardDriveException;
use WideImage;

class Helper
{
    /**
     * Moves files from record (from "files" property of collection) from temporary destination
     * to the original destination.
     * Example of $record:
     *      {
     *          "_id": ObjectId("53195f675c65d1ba0b7b23c7"),
     *          "created_at": NumberInt(1394171751),
     *          "files": {                                <---- This line is the most important
     *              "0": {
     *                  "title": "Our Beautiful File",
     *                  "file_id": "53195f665c65d1ba0b7b23c6"
     *              }
     *          },
     *          "updated_at": NumberInt(1394171751)
     *      }
     *
     * @param Db\Mapping\File $files
     */
    public static final function moveFilesFrom(\Vegas\Media\Db\Mapping\File $files)
    {
        foreach($files as $file) {
            self::moveFile($file->getRecord());
        }
    }

    /**
     * Move file from the temporary destination to the original destination
     *
     * @param FileModel $file
     * @throws File\Exception
     */
    public static final function moveFile(\Vegas\Media\Model\File $file)
    {
        try {
            if($file->is_temp) {
                rename(
                    $file->temp_destination . '/' . $file->temp_name,
                    $file->original_destination . '/' . $file->temp_name
                );

                if(!file_exists($file->original_destination . '/' . $file->temp_name)) {
                    throw new CannotSaveFileInHardDriveException();
                }

                $file->is_temp = false;
                $file->expire = null;
                $file->save();
            }
        } catch (\Exception $exception) {
            throw new FileException($exception->getMessage());
        }
    }

    /**
     * Generates a thumbnails of the files associated with the record
     *
     * @param Db\Mapping\File $files
     * @param array $size
     */
    public static final function generateThumbnailsFrom(\Vegas\Media\Db\Mapping\File $files, array $size)
    {
        foreach($files as $file) {
            self::generateThumbnail($file->getRecord(), $size);
        }
    }

    /**
     * Generates a thumbnails of the images in the following location:
     * {ORIGINAL_DESTINATION_OF_THE_FILE}/thumbnails/filename.{ext}
     *
     * @param FileModel $file
     * @param array $size
     */
    public static final function generateThumbnail(\Vegas\Media\Model\File $file, array $size = array('width' => 168, 'height' => 120))
    {
        if(!empty($file->original_destination) && isset($size['width']) && isset($size['height'])) {

            // ie. string(47) "/var/www/vegas/public/uploads/5326acd311dd4.jpg"
            $filePath = $file->original_destination . '/' . $file->temp_name;

            if (!file_exists($file->original_destination . '/thumbnails')) {
                mkdir($file->original_destination . '/thumbnails', 0777, true);
            }

            WideImage::load($filePath)
                ->resize($size['width'], $size['height'], 'outside')
{'fraction_non_alphanumeric': 0.12776947125186897, 'fraction_numerical': 0.019845045534864755, 'mean_word_length': 1.9862012987012987, 'pattern_counts': {'":': 14, '<': 13, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5590590', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2089, 'n_words': 466}