La comunità italiana su CakePHP

You are not logged in. Please login or register.


Pages: 1

Atom RSS

Posts [ 1 ]

dalpo

Topic: Media View

Ciao

Se avete problemi con il media view, vi posto posto le modifiche che ho fatto per farlo funzionare, sotto indicazioni del ticket:
https://trac.cakephp.org/ticket/4517

nel file: /cake/libs/view/media.php
ho modificato il seguente metodo:

function render() {
    $name = null;
    $download = null;
    $extension = null;
    $id = null;
    $modified = null;
    $path = null;
    $size = null;
    extract($this->viewVars, EXTR_OVERWRITE);

    if ($size) {
        $id = $id . "_$size";
    }
    $path = $path . $id;
    
    if (is_null($name)) {
        $name = $id;
    }

    if (file_exists($path) && isset($extension) && connection_status() == 0) {
        $chunkSize = 1 * (1024 * 8);
        $buffer = '';
        $fileSize = @filesize($path);
        $handle = fopen($path, 'rb');

        if ($handle === false) {
            return false;
        }
        if (isset($modified) && !empty($modified)) {
            $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
        } else {
            $modified = gmdate('D, d M Y H:i:s').' GMT';
        }

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Last-Modified: $modified");

        if ($download) {
            $contentType = 'application/octet-stream';
            $agent = env('HTTP_USER_AGENT');

            if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
                $contentType = 'application/octetstream';
            }

            header('Content-Type: ' . $contentType);
            header("Content-Disposition: attachment; filename=\"" . $name . '.' . $extension . "\";");
            header("Expires: 0");
            header('Accept-Ranges: bytes');
            header("Cache-Control: private", false);
            header("Pragma: private");

            $httpRange = env('HTTP_RANGE');

            if (isset($httpRange)) {
                list ($toss, $range) = explode("=", $httpRange);
                str_replace($range, "-", $range);

                $size = $fileSize - 1;
                $length = $fileSize - $range;

                header("HTTP/1.1 206 Partial Content");
                header("Content-Length: $length");
                header("Content-Range: bytes $range$size/$fileSize");
                fseek($handle, $range);
            } else {
                header("Content-Length: " . $fileSize);
            }
        } else {
            header("Content-Type: " . $this->mimeType[$extension]);            
            header("Content-Length: " . $fileSize);
        }
        @ob_end_clean();

        while (!feof($handle) && connection_status() == 0) {
            set_time_limit(0);
            $buffer = fread($handle, $chunkSize);
            echo $buffer;
            @flush();
            @ob_flush();
        }
        fclose($handle);
        return((connection_status() == 0) && !connection_aborted());
    }
    return false;
}

vi posto anche un semplice metodo per utilizzare la media view:

function download($id = null) {
        if($id) {
            $communication = $this->Communication->read(null, $id);            
            $values = explode('.', $communication['Communication']['attachment_name']);
            $ext = array_pop($values);
            $filename = implode('.', $values);        
            $this->view = 'Media';
            $params = array(
                'id' => $communication['Communication']['attachment'],
                'name' => $filename,
                'download' => true,
                'extension' => low($ext),
                'path' => WWW_ROOT . 'files' . DS . 'communications' . DS 
            );
            $this->set($params);
        }
    }

N.B.
$communication['Communication']['attachment_name'] è il nome del file caricato in fase di upload
mentre
$communication['Communication']['attachment'] è il nome univoco che gli ho assegnato per evitare di sovrascrivere altri file
ad es.:
$communication['Communication']['attachment_name']  = 'Pippo e pluto.pdf'
$communication['Communication']['attachment']  = '123yg128712ug12387.pdf'

spero che vi possa tornare utile

bye

Posts [ 1 ]

Guest posting is disabled. You must login or register to post a reply.

Pages: 1

Topic info

0 guests and 0 users are reading this topic now


Forum quick jump menu

Currently used extensions: pun_topic_online_users, pun_karma, pun_admin_hook_navigator, pun_bbcode. Copyright © 2008 PunBB

[ Generated in 0.028 seconds, 12 queries executed ]