<commit_before><?php

namespace Boom\Asset;

use Boom\Person;
use \DB;
use \File;
use \DateTime;

abstract class Asset
{
    /**
	 *
	 * @var \Model_Asset
	 */
    protected $model;

    protected $old_files = [];

    public function __construct(\Model_Asset $model)
    {
        $this->model = $model;
    }

    public static function directory()
    {
        return APPPATH . 'assets';
    }

    public function exists()
    {
        return $this->loaded() && file_exists($this->getFilename());
    }

    public function getAspectRatio()
    {
        return 1;
    }

    public function getCredits()
    {
        return $this->model->credits;
    }

    /**
	 *
	 * @return string
	 */
    public function getExtension()
    {
        return $this->getMimetype()->getExtension();
    }

    public function getDescription()
    {
        return $this->model->description;
    }

    public function getDownloads()
    {
        return $this->model->downloads;
    }

    /**
	 *
	 * @return string
	 */
    public function getFilename()
    {
        return static::directory() . DIRECTORY_SEPARATOR . $this->getId();
    }

    public function getFilesize()
    {
        return $this->model->filesize;
    }

    public function getId()
    {
        return $this->model->id;
    }

    public function getLastModified()
    {
        return new DateTime('@' . $this->model->last_modified);
    }

    public function getMimetype()
    {
        return $this->exists() ? Mimetype::factory(File::mime($this->getFilename())) : null;
    }

    /**
	 * Returns an array of old files which have been replaced.
	 * Where an asset has been replaced the array will contain the names of the backup files for the previous versions.
	 *
	 * @return	array
	 */
    public function getOldFiles()
    {
        // If the asset doesn't exist return an empty array.
        if ( ! $this->loaded()) {
            return [];
        }

        if ($this->old_files === null) {
            // Add files for previous versions of the asset.
            // Wrap the glob in array_reverse() so that we end up with an array with the most recent first.
            foreach (new OldFilesIterator($this) as $file) {
                // Get the version ID out of the filename.
                preg_match('/' . $this->id . '.(\d+).bak$/', $file->getFilename(), $matches);

                if (isset($matches[1])) {
                    $this->old_files[$matches[1]] = $file;
                } else {
                    $this->old_files[] = $file;
                }
            }
        }

        return $this->old_files;
    }

    public function getOriginalFilename()
    {
        return $this->model->filename;
    }

    public function getTags()
    {
        $results = DB::select('tag')
            ->from('assets_tags')
            ->where('asset_id', '=', $this->getId())
            ->execute()
            ->as_array('tag');

        return array_keys($results);
    }

    public function getThumbnailAssetId()
    {
        return $this->model->thumbnail_asset_id;
    }

    public function getThumbnail()
    {
        return Factory::byId($this->getThumbnailAssetId());
    }

    public function getTitle()
    {
        return $this->model->title;
    }

    abstract public function getType();

    public function getUploadedBy()
    {
        return $this->model->uploader;
    }

    public function getUploadedTime()
    {
        return new \DateTime('@' . $this->model->uploaded_time);
    }

    public function getVisibleFrom()
    {
        return new \DateTime('@' . $this->model->visible_from);
    }

    public function isVisible()
    {
        return $this->getVisibleFrom()->getTimestamp() < time();
    }

    public function loaded()
    {
        return $this->model->loaded();
    }

    public function logDownload($ip)
    {
        $ip = ip2long($ip);

        $logged = DB::select(DB::expr("1"))
            ->from('asset_downloads')
            ->where('ip', '=', $ip)
            ->where('asset_id', '=', $this->getId())
            ->where('time', '>=', time() - Date::MINUTE * 10)
            ->limit(1)
            ->execute()
            ->as_array();

        if ( ! count($logged)) {
            ORM::factory('Asset_Download')
                ->values([
                    'asset_id' => $this->getId(),
                    'ip' => $ip,
                ])
                ->create();

            DB::update('assets')
                ->set(['downloads' => DB::expr('downloads + 1')])
                ->where('id', '=', $this->getId())
                ->execute();
        }

        return $this;
    }

    /**
	 *
	 * @return \Boom\Asset\Asset
	 */
    public function save()
    {
        $this->model->loaded() ? $this->model->update() : $this->model->create();

        return $this;
    }

    /**
	 *
	 * @param string $credits
	 * @return \Boom\Asset\Asset
	 */
    public function setCredits($credits)
    {
        $this->model->credits = $credits;

        return $this;
    }

    /**
	 *
	 * @param string $description
	 * @return \Boom\Asset\Asset
	 */
    public function setDescription($description)
    {
        $this->model->description = $description;

        return $this;
    }

    /**
	 *
	 * @param string $filename
	 * @return \Boom\Asset\Asset
	 */
    public function setFilename($filename)
    {
        $this->model->filename = $filename;

        return $this;
    }

    /**
	 *
	 * @param float $size
	 * @return \Boom\Asset\Asset
	 */
    public function setFilesize($size)
    {
        $this->model->filesize = $size;

        return $this;
    }

    /**
	 *
	 * @param DateTime $time
	 * @return \Boom\Asset\Asset
	 */
    public function setLastModified(DateTime $time)
    {
        $this->model->last_modified = $time->getTimestamp();

        return $this;
    }

    /**
	 *
	 * @param int $assetId
	 * @return \Boom\Asset\Asset
	 */
    public function setThumbnailAssetId($assetId)
    {
        $this->model->thumbnail_asset_id = $assetId;

        return $this;
    }

    /**
	 *
	 * @param string $title
	 * @return \Boom\Asset\Asset
	 */
    public function setTitle($title)
    {
        $this->model->title = $title;

        return $this;
    }

    /**
	 *
	 * @param \Boom\Person\Person $person
	 * @return \Boom\Asset\Asset
	 */
    public function setUploadedBy(Person\Person $person)
    {
        $this->model->uploaded_by = $person->getId();

        return $this;
    }

    public function setVisibleFrom(DateTime $time)
    {
        $this->model->visible_from = $time->getTimestamp();

        return $this;
    }
}
<commit_msg>Fixed bug in asset download<commit_after><?php

namespace Boom\Asset;

use Boom\Person;
use \DB;
use \File;
use \DateTime;
use Date;

abstract class Asset
{
    /**
	 *
	 * @var \Model_Asset
	 */
    protected $model;

    protected $old_files = [];

    public function __construct(\Model_Asset $model)
    {
        $this->model = $model;
    }

    public static function directory()
    {
        return APPPATH . 'assets';
    }

    public function exists()
    {
        return $this->loaded() && file_exists($this->getFilename());
    }

    public function getAspectRatio()
    {
        return 1;
    }

    public function getCredits()
    {
        return $this->model->credits;
    }

    /**
	 *
	 * @return string
	 */
    public function getExtension()
    {
        return $this->getMimetype()->getExtension();
    }

    public function getDescription()
    {
        return $this->model->description;
    }

    public function getDownloads()
    {
        return $this->model->downloads;
    }

    /**
	 *
	 * @return string
	 */
    public function getFilename()
    {
        return static::directory() . DIRECTORY_SEPARATOR . $this->getId();
    }

    public function getFilesize()
    {
        return $this->model->filesize;
    }

    public function getId()
    {
        return $this->model->id;
    }

    public function getLastModified()
    {
        return new DateTime('@' . $this->model->last_modified);
    }

    public function getMimetype()
    {
        return $this->exists() ? Mimetype::factory(File::mime($this->getFilename())) : null;
    }

    /**
	 * Returns an array of old files which have been replaced.
	 * Where an asset has been replaced the array will contain the names of the backup files for the previous versions.
	 *
	 * @return	array
	 */
    public function getOldFiles()
    {
        // If the asset doesn't exist return an empty array.
        if ( ! $this->loaded()) {
            return [];
        }

        if ($this->old_files === null) {
            // Add files for previous versions of the asset.
            // Wrap the glob in array_reverse() so that we end up with an array with the most recent first.
            foreach (new OldFilesIterator($this) as $file) {
                // Get the version ID out of the filename.
                preg_match('/' . $this->id . '.(\d+).bak$/', $file->getFilename(), $matches);

                if (isset($matches[1])) {
                    $this->old_files[$matches[1]] = $file;
                } else {
                    $this->old_files[] = $file;
                }
            }
        }

        return $this->old_files;
    }

    public function getOriginalFilename()
    {
        return $this->model->filename;
    }

    public function getTags()
    {
        $results = DB::select('tag')
            ->from('assets_tags')
            ->where('asset_id', '=', $this->getId())
            ->execute()
            ->as_array('tag');

        return array_keys($results);
    }

    public function getThumbnailAssetId()
    {
        return $this->model->thumbnail_asset_id;
    }

    public function getThumbnail()
    {
        return Factory::byId($this->getThumbnailAssetId());
    }

    public function getTitle()
    {
        return $this->model->title;
    }

    abstract public function getType();

    public function getUploadedBy()
    {
        return $this->model->uploader;
    }

    public function getUploadedTime()
    {
        return new \DateTime('@' . $this->model->uploaded_time);
    }

    public function getVisibleFrom()
    {
        return new \DateTime('@' . $this->model->visible_from);
    }

    public function isVisible()
    {
        return $this->getVisibleFrom()->getTimestamp() < time();
    }

    public function loaded()
    {
        return $this->model->loaded();
    }

    public function logDownload($ip)
    {
        $ip = ip2long($ip);

        $logged = DB::select(DB::expr("1"))
            ->from('asset_downloads')
            ->where('ip', '=', $ip)
            ->where('asset_id', '=', $this->getId())
            ->where('time', '>=', time() - Date::MINUTE * 10)
            ->limit(1)
            ->execute()
            ->as_array();

        if ( ! count($logged)) {
            ORM::factory('Asset_Download')
                ->values([
                    'asset_id' => $this->getId(),
                    'ip' => $ip,
                ])
                ->create();

            DB::update('assets')
                ->set(['downloads' => DB::expr('downloads + 1')])
                ->where('id', '=', $this->getId())
                ->execute();
        }

        return $this;
    }

    /**
	 *
	 * @return \Boom\Asset\Asset
	 */
    public function save()
    {
        $this->model->loaded() ? $this->model->update() : $this->model->create();

        return $this;
    }

    /**
	 *
	 * @param string $credits
	 * @return \Boom\Asset\Asset
	 */
    public function setCredits($credits)
    {
        $this->model->credits = $credits;

        return $this;
    }

    /**
	 *
	 * @param string $description
	 * @return \Boom\Asset\Asset
	 */
    public function setDescription($description)
    {
        $this->model->description = $description;

        return $this;
    }

    /**
	 *
	 * @param string $filename
	 * @return \Boom\Asset\Asset
	 */
    public function setFilename($filename)
    {
        $this->model->filename = $filename;

        return $this;
    }

    /**
	 *
	 * @param float $size
	 * @return \Boom\Asset\Asset
	 */
    public function setFilesize($size)
    {
        $this->model->filesize = $size;

        return $this;
    }

    /**
	 *
	 * @param DateTime $time
	 * @return \Boom\Asset\Asset
	 */
    public function setLastModified(DateTime $time)
    {
        $this->model->last_modified = $time->getTimestamp();

        return $this;
    }

    /**
	 *
	 * @param int $assetId
	 * @return \Boom\Asset\Asset
	 */
    public function setThumbnailAssetId($assetId)
    {
        $this->model->thumbnail_asset_id = $assetId;

        return $this;
    }

    /**
	 *
	 * @param string $title
	 * @return \Boom\Asset\Asset
	 */
    public function setTitle($title)
    {
        $this->model->title = $title;

        return $this;
    }

    /**
	 *
	 * @param \Boom\Person\Person $person
	 * @return \Boom\Asset\Asset
	 */
    public function setUploadedBy(Person\Person $person)
    {
        $this->model->uploaded_by = $person->getId();

        return $this;
    }

    public function setVisibleFrom(DateTime $time)
    {
        $this->model->visible_from = $time->getTimestamp();

        return $this;
    }
}

{'fraction_non_alphanumeric': 0.14863034316676701, 'fraction_numerical': 0.0013546056592414208, 'mean_word_length': 1.8148697309891972, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 193, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1655861', 'n_tokens_mistral': 4087, 'n_tokens_neox': 3846, 'n_words': 848}