<commit_before><?php

/*
 * This file is part of the RednoseFrameworkBundle package.
 *
 * (c) RedNose <http://www.rednose.nl>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rednose\FrameworkBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileController extends Controller
{
    public function uploadAction()
    {
        $request = $this->getRequest();

        if (count($request->files) !== 1) {
            throw new \InvalidArgumentException();
        }

        foreach($request->files as $uploadedFile) {
            if ($uploadedFile instanceof UploadedFile && $uploadedFile->isValid()) {
                $dir  = $this->getTempDir();
                $name = sprintf('%s.tmp', uniqid('tmp'));

                $metadata = array(
                    'name'     => $uploadedFile->getClientOriginalName(),
                    'mimeType' => $uploadedFile->getMimeType(),
                );

                // Write file.
                $file = $uploadedFile->move($dir, $name);

                // Write metadata.
                file_put_contents(sprintf('%s/%s.meta', $dir, $name), serialize($metadata));

                return new Response($file->getFilename());
            }
        }

        return new Response(null, 500);
    }

    protected function getTempDir()
    {
        $base = $this->get('kernel')->getRootDir().'/cache';
        $env  = $this->get('kernel')->getEnvironment();

        return sprintf('%s/%s/rednose_framework/tmp', $base, $env);
    }
}<commit_msg>File uploading fixes<commit_after><?php

/*
 * This file is part of the RednoseFrameworkBundle package.
 *
 * (c) RedNose <http://www.rednose.nl>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rednose\FrameworkBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileController extends Controller
{
    public function uploadAction()
    {
        $request = $this->getRequest();

        if (count($request->files) !== 1) {
            throw new \InvalidArgumentException();
        }

        foreach($request->files as $uploadedFile) {
            if ($uploadedFile instanceof UploadedFile && $uploadedFile->isValid()) {
                $dir  = $this->getTempDir();
                $name = sprintf('%s.%s', uniqid(), $uploadedFile->getClientOriginalExtension());

                $metadata = array(
                    'name'     => $uploadedFile->getClientOriginalName(),
                    'mimeType' => $uploadedFile->getMimeType(),
                );

                // Write file.
                $uploadedFile->move($dir, $name);

                // Write metadata.
                file_put_contents(sprintf('%s/%s.meta', $dir, $name), serialize($metadata));

                $uri = sprintf('%s/%s', $dir, $name);

                return new Response(str_replace('/app_dev.php', '', sprintf('%s/%s', $this->container->get('router')->getContext()->getBaseUrl(), $uri)));
            }
        }

        return new Response(null, 500);
    }

    /**
     * @return string
     */
    protected function getTempDir()
    {
        return sprintf('cache/rednose_framework/file');
    }
}
{'fraction_non_alphanumeric': 0.13771475320425416, 'fraction_numerical': 0.00218161985274066, 'mean_word_length': 2.3713235294117645, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1854411', 'n_tokens_mistral': 1028, 'n_tokens_neox': 974, 'n_words': 213}