<commit_before><?php
/**
 * Created by PhpStorm.
 * User: christof
 * Date: 13.11.15
 * Time: 12:09
 */

namespace MandarinMedien\MMMediaBundle\Controller;


use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use MandarinMedien\MMMediaBundle\Model\MediaInterface;

use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalAdapter;

class UploadController extends Controller
{

    /**
     * retrieves and handles the ajax upload action
     * to store uploaded files on file system
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function uploadAction(Request $request)
    {

        //return new JsonResponse($request);

        // process the filebag
        $medias = array_merge(
            $this->processUploadedFiles($request->files),
            $this->processUrls(Array())
        );


        $em = $this->getDoctrine()->getManager();
        $mtm = $this->get('mm_media.mediatype.manager');

        $returnData = array();

        foreach($medias as $media) {
            if(null != ($mt = $mtm->getMediaTypeMatch($media))) {

                /** @var MediaInterface $ms */
                $ms = $mt->getEntity();
                $em->persist($ms);

                $em->flush();

                $returnData[] = array(
                    'id' => $ms->getId(),
                    'path' => $ms->getMediaTypeReference(),
                    'mediatype' => (string) $ms->getMediaType()
                );
            }
        }

        return new JsonResponse(
            array(
                'success' => true,
                'data' => $returnData
            )
        );
    }


    /**
     * gets the filebag and moves uploaded files to filesystem
     *
     * @param FileBag $filebag
     * @return array reference list of moved files
     */
    protected function processUploadedFiles(FileBag $filebag)
    {
        $adapter = new LocalAdapter($this->get('kernel')->getRootDir() . '/../web/media');
        $filesystem = new Filesystem($adapter);


        $processed = array();


        /**
         * @var UploadedFile $file
         */
        foreach($filebag->get('files') as $file)
        {
            // get the unique filepath
            $dest = $this->createUniquePath($file);

            if($filesystem->write($dest['path'], file_get_contents($file->getPathname()))){
                $processed[] = $dest['path'];
            };
        }

        return $processed;
    }


    /**
     * process the given urls
     *
     * @param array $urls
     * @return array reference list of the urls
     */
    protected function processUrls(array $urls)
    {
        return array();
    }


    /**
     * return a unique filepath
     *
     * @param UploadedFile $file
     * @return array
     */
    protected function createUniquePath(UploadedFile $file)
    {
        $unique = uniqid();
        $dir = substr((string) $unique, 0, 2);
        $name = $file->getClientOriginalName().'.'.uniqid().'.'.$file->getClientOriginalExtension();

        return Array(
            'dir' => $dir,
            'filename' => $name,
            'path' => $dir.'/'.$name
        );
    }
}<commit_msg>UploadController: does now also imports external links by parsing the "url" request-attribute<commit_after><?php
/**
 * Created by PhpStorm.
 * User: christof
 * Date: 13.11.15
 * Time: 12:09
 */

namespace MandarinMedien\MMMediaBundle\Controller;


use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use MandarinMedien\MMMediaBundle\Model\MediaInterface;

use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalAdapter;

class UploadController extends Controller
{

    /**
     * retrieves and handles the ajax upload action
     * to store uploaded files on file system
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function uploadAction(Request $request)
    {

        //return new JsonResponse($request);
        #var_dump($request);
        // process the filebag

        $rawMedias = array_merge(
            $this->processUploadedFiles($request->files),
            $this->processUrls($request)
        );


        $em = $this->getDoctrine()->getManager();
        $mtm = $this->get('mm_media.mediatype.manager');

        $returnData = array();

        foreach ($rawMedias as $rawmedia) {
            if (null != ($mt = $mtm->getMediaTypeMatch($rawmedia))) {

                /** @var MediaInterface $ms */
                $ms = $mt->getEntity();
                $em->persist($ms);

                $em->flush();

                $returnData[] = array(
                    'id' => $ms->getId(),
                    'path' => $ms->getMediaTypeReference(),
                    'mediatype' => (string)$ms->getMediaType()
                );
            }
        }

        return new JsonResponse(
            array(
                'success' => true,
                'data' => $returnData
            )
        );
    }


    /**
     * gets the filebag and moves uploaded files to filesystem
     *
     * @param FileBag $filebag
     * @return array reference list of moved files
     */
    protected function processUploadedFiles(FileBag $filebag)
    {
        $adapter = new LocalAdapter($this->get('kernel')->getRootDir() . '/../web/media');
        $filesystem = new Filesystem($adapter);


        $processed = array();

        if ($filebag->get('files')) {
            /**
             * @var UploadedFile $file
             */
            foreach ($filebag->get('files') as $file) {
                // get the unique filepath
                $dest = $this->createUniquePath($file);

                if ($filesystem->write($dest['path'], file_get_contents($file->getPathname()))) {
                    $processed[] = $dest['path'];
                };
            }
        }


        return $processed;
    }


    /**
     * process the given urls
     *
     * @param Request $request
     * @return array
     */
    protected function processUrls(Request $request)
    {
        $externalRawMediaUrls = array();

        if ($request->get('urls')) {

            foreach ($request->get('urls') as $url) {

                $externalRawMediaUrls[] = $url;
            }
        }

        return $externalRawMediaUrls;
    }


    /**
     * return a unique filepath
     *
     * @param UploadedFile $file
     * @return array
     */
    protected function createUniquePath(UploadedFile $file)
    {
        $unique = uniqid();
        $dir = substr((string)$unique, 0, 2);
        $name = $file->getClientOriginalName() . '.' . uniqid() . '.' . $file->getClientOriginalExtension();

        return Array(
            'dir' => $dir,
            'filename' => $name,
            'path' => $dir . '/' . $name
        );
    }
}
{'fraction_non_alphanumeric': 0.13371773750686436, 'fraction_numerical': 0.0032948929159802307, 'mean_word_length': 1.926878264363198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '4987496', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1986, 'n_words': 434}