<commit_before><?php

include_once(LIB . 'relic' . DS . 'library' . DS . 'PDF.php');

class SplitPdfTask extends AppShell {
    public $uses = array('Resource', 'Membership');

    public function execute($data) {
        $this->_loadModels();
        $id = $data['resource_id'];
        $collection_id = $data['collection_id'];
        $resource = $this->Resource->findById($id);
        $metadata = $resource['Metadatum'];
        $resource = $resource['Resource'];

        # Get and set its path.
        $path = $this->Resource->path($resource['sha'], $resource['file_name']);

        $pdf = new \Relic\PDF($path);

        # For each page in the PDF:
        for ($page = 1; $page <= $pdf->npages; $page++) {
            # Create a tmp file to write to.
            $tmp_file = tempnam(sys_get_temp_dir(), 'ARCS');
            # Extract the page.
            $pdf->extractPage($page, $tmp_file);

            # The name is just the PDF file name plus "-pX.jpeg"
            $basename = str_ireplace('.pdf', '', $resource['file_name']);
            $fname = $basename . "-p$page.jpeg";
            # Create the resource file.
            $sha = $this->Resource->createFile($tmp_file, array(
                'filename' => $fname, 
                'thumb' => true
            ));

            $this->Resource->permit('sha', 'file_size', 'file_name', 'user_id');
            # Save the resource.
            $this->Resource->add(array(
                'sha' => $sha,
                'title' => $resource['title'] . "-p$page",
                'public' => $resource['public'],
                'context' => $collection_id,
                'file_name' => $fname,
                'file_size' => $this->Resource->size($sha, $fname),
                'mime_type' => 'image/jpeg',
                'user_id' => $resource['user_id']
            ));

            # Map the PDF's metadata on to the page.
            $this->mapMetadata($this->Resource->id, $metadata);

            # Save the collection membership.
            $this->Membership->pair($this->Resource->id, $collection_id, $page);

            # Reset the Resource and Membership models for the next round.
            $this->Resource->create();<commit_msg>PDF splits now set the resource type.<commit_after><?php

include_once(LIB . 'Relic' . DS . 'Library' . DS . 'PDF.php');

class SplitPdfTask extends AppShell {
    public $uses = array('Resource', 'Membership');

    public function execute($data) {
        $this->_loadModels();
        $id = $data['resource_id'];
        $collection_id = $data['collection_id'];
        $resource = $this->Resource->findById($id);
        $metadata = $resource['Metadatum'];
        $resource = $resource['Resource'];

        # Get and set its path.
        $path = $this->Resource->path($resource['sha'], $resource['file_name']);

        $pdf = new \Relic\PDF($path);

        # For each page in the PDF:
        for ($page = 1; $page <= $pdf->npages; $page++) {
            # Create a tmp file to write to.
            $tmp_file = tempnam(sys_get_temp_dir(), 'ARCS');
            # Extract the page.
            $pdf->extractPage($page, $tmp_file);

            # The name is just the PDF file name plus "-pX.jpeg"
            $basename = str_ireplace('.pdf', '', $resource['file_name']);
            $fname = $basename . "-p$page.jpeg";
            # Create the resource file.
            $sha = $this->Resource->createFile($tmp_file, array(
                'filename' => $fname, 
                'thumb' => true
            ));

            $this->Resource->permit('sha', 'file_size', 'file_name', 'user_id');
            # Save the resource.
            $this->Resource->add(array(
                'sha' => $sha,
                'title' => $resource['title'] . "-p$page",
                'public' => $resource['public'],
                'context' => $collection_id,
                'file_name' => $fname,
                'file_size' => $this->Resource->size($sha, $fname),
                'mime_type' => 'image/jpeg',
                'type' => isset($data['type']) ? $data['type'] : null,
                'user_id' => $resource['user_id']
            ));

            # Map the PDF's metadata on to the page.
            $this->mapMetadata($this->Resource->id, $metadata);

            # Save the collection membership.
            $this->Membership->pair($this->Resource->id, $collection_id, $page);

            # Reset the Resource and Membership models for the next round.
            $this->Resource->create();
{'fraction_non_alphanumeric': 0.16589250165892502, 'fraction_numerical': 0.00044238000442380006, 'mean_word_length': 1.9497716894977168, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 72, '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': '1201748', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1268, 'n_words': 305}