<commit_before><?php

namespace Spatie\MediaLibrary;

use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\Exceptions\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileTooBig;
use Spatie\MediaLibrary\Exceptions\MediaDoesNotBelongToModel;
use Spatie\MediaLibrary\Exceptions\MediaIsNotPartOfCollection;

trait HasMediaTrait
{
    /**
     * @var array
     */
    public $mediaConversions = [];

    /**
     * Set the polymorphic relation.
     *
     * @return mixed
     */
    public function media()
    {
        return $this->morphMany(Media::class, 'model');
    }

    /**
     * Add media to media collection from a given file.
     *
     * @param string $file
     * @param string $collectionName
     * @param bool   $removeOriginal
     * @param bool   $addAsTemporary
     *
     * @return Media
     *
     * @throws \Spatie\MediaLibrary\Exceptions\FileDoesNotExist
     * @throws \Spatie\MediaLibrary\Exceptions\FileTooBig
     */
    public function addMedia($file, $collectionName = 'default', $removeOriginal = true, $addAsTemporary = false)
    {
        if (!is_file($file)) {
            throw new FileDoesNotExist();
        }

        if (filesize($file) > config('laravel-medialibrary.max_file_size')) {
            throw new FileTooBig();
        }

        $media = new Media();

        $media->name = pathinfo($file, PATHINFO_FILENAME);
        $media->file_name = pathinfo($file, PATHINFO_BASENAME);

        $media->collection_name = $collectionName;

        $media->size = filesize($file);
        $media->temp = $addAsTemporary;
        $media->manipulations = [];

        $media->save();

        $this->media()->save($media);

        app(Filesystem::class)->add($file, $media);

        if ($removeOriginal) {
            unlink($file);
        }

        return $media;
    }

    /**
     * Determine if there is media in the given collection.
     *
     * @param $collectionName
     *
     * @return bool<commit_msg>allow upload to be added<commit_after><?php

namespace Spatie\MediaLibrary;

use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\Exceptions\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileTooBig;
use Spatie\MediaLibrary\Exceptions\MediaDoesNotBelongToModel;
use Spatie\MediaLibrary\Exceptions\MediaIsNotPartOfCollection;

trait HasMediaTrait
{
    /**
     * @var array
     */
    public $mediaConversions = [];

    /**
     * Set the polymorphic relation.
     *
     * @return mixed
     */
    public function media()
    {
        return $this->morphMany(Media::class, 'model');
    }

    /**
     * Add media to media collection from a given file.
     *
     * @param string|\Symfony\Component\HttpFoundation\File\File $file
     * @param string $collectionName
     * @param bool   $removeOriginal
     * @param bool   $addAsTemporary
     *
     * @return Media
     *
     * @throws \Spatie\MediaLibrary\Exceptions\FileDoesNotExist
     * @throws \Spatie\MediaLibrary\Exceptions\FileTooBig
     */
    public function addMedia($file, $collectionName = 'default', $removeOriginal = true, $addAsTemporary = false)
    {
        if (is_string($file)) {
            $pathToFile = $file;
            $fileName = pathinfo($file, PATHINFO_BASENAME);
            $mediaName = pathinfo($file, PATHINFO_FILENAME);
        }

        if ($file instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) {
            $pathToFile = $file->getPath();
            $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_BASENAME);
            $mediaName = $file->getClientOriginalName();
        }

        if (!is_file($pathToFile)) throw new FileDoesNotExist();

        if (filesize($file) > config('laravel-medialibrary.max_file_size')) {
            throw new FileTooBig();
        }

        $media = new Media();

        $media->name = $mediaName;
        $media->file_name = $fileName;

        $media->collection_name = $collectionName;

        $media->size = filesize($pathToFile);
        $media->temp = $addAsTemporary;
        $media->manipulations = [];

        $media->save();

        $this->media()->save($media);

        app(Filesystem::class)->add($file, $media);

        if ($removeOriginal) {
            unlink($pathToFile);
        }

        return $media;
    }

    /**
     * Determine if there is media in the given collection.
     *
     * @param $collectionName
     *
     * @return bool
{'fraction_non_alphanumeric': 0.12466246624662466, 'fraction_numerical': 0.0, 'mean_word_length': 2.480814408770556, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165206', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1302, 'n_words': 254}