code/controllers/CDNSecureFileController.php
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {

		$response = new SS_HTTPResponse();
        
        $filename = $request->getURL();
        
        if (strpos($filename, 'cdnassets') === 0) {
            $filename = 'assets/' . substr($filename, strlen('cdnassets/'));
        }
        
        $file = null;
        if (strpos($filename, '_resampled') !== false) {
            $file = ContentServiceAsset::get()->filter('Filename', $filename)->first();
        } else if (strpos($filename, '/_versions/') !== false) {
            $file = FileVersion::get()->filter('Filename', "/" . $filename)->first();
        } else {
            $file = File::get()->filter('filename', $filename)->first();
        }
        
		if ($file && $file->canView()) {
            if (!$file->CDNFile && !$file->FilePointer) {
                return $this->httpError(404);
            }
			// Permission passed redirect to file
            $redirectLink = '';
            if ($file->getViewType() != CDNFile::ANYONE_PERM) {
                if ($file->hasMethod('getSecureURL')) {
                    $redirectLink = $file->getSecureURL(180);
                } 
                if (!strlen($redirectLink)) {
                    // can we stream it?
                    return $this->sendFile($file);
                }
            } else {
                $redirectLink = $file->getURL();
            }

            if ($redirectLink && trim($redirectLink, '/') != $request->getURL()) {
                $response->redirect($redirectLink);
            } else {
                return $this->httpError(404);
            }
		} else {
            if (class_exists('SecureFileController')) {
                $handoff = SecureFileController::create();
                return $handoff->handleRequest($request, $model);
            } elseif ($file instanceof File) {
                // Permission failure
                Security::permissionFailure($this, 'You are not authorised to access this resource. Please log in.');
            } else {
                // File doesn't exist
                $response = new SS_HTTPResponse('File Not Found', 404);
            }
        }

		return $response;
	}
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {

		$response = new SS_HTTPResponse();
        
        $filename = $request->getURL();
        
        if (strpos($filename, 'cdnassets') === 0) {
            $filename = 'assets/' . substr($filename, strlen('cdnassets/'));
        }
        
        $file = null;
        if (strpos($filename, '_resampled') !== false) {
            $file = ContentServiceAsset::get()->filter('Filename', $filename)->first();
        } else if (strpos($filename, '/_versions/') !== false) {
            $file = FileVersion::get()->filter('Filename', "/" . $filename)->first();
        } else {
            $file = File::get()->filter('filename', $filename)->first();
        }
        
		if ($file && $file->canView()) {
            if (!$file->CDNFile && !$file->FilePointer) {
                return $this->httpError(404);
            }
			// Permission passed redirect to file
            $redirectLink = '';
            if ($file->getViewType() != CDNFile::ANYONE_PERM) {
                if ($file->hasMethod('getSecureURL')) {
                    $redirectLink = $file->getSecureURL(180);
                } 
                if (!strlen($redirectLink)) {
                    // can we stream it?
                    return $this->sendFile($file);
                }
            } else {
                $redirectLink = $file->getURL();
            }

            if ($redirectLink && trim($redirectLink, '/') != $request->getURL()) {
                $response->redirect($redirectLink);
            } else {
                return $this->httpError(404);
            }
		} else {
            if (class_exists('SecureFileController')) {
                $handoff = SecureFileController::create();
                return $handoff->handleRequest($request, $model);
            } elseif ($file instanceof File) {
                // Permission failure
                Security::permissionFailure($this, 'You are not authorised to access this resource. Please log in.');
            } else {
                // File doesn't exist
                $response = new SS_HTTPResponse('File Not Found', 404);
            }
        }

		return $response;
	}
public function handleRequest ( SS_HTTPRequest $ request , DataModel $ model ) { $ response = new SS_HTTPResponse ( ) ; $ filename = $ request -> getURL ( ) ; if ( strpos ( $ filename , 'cdnassets' ) === 0 ) { $ filename = 'assets/' . substr ( $ filename , strlen ( 'cdnassets/' ) ) ; } $ file = null ; if ( strpos ( $ filename , '_resampled' ) !== false ) { $ file = ContentServiceAsset :: get ( ) -> filter ( 'Filename' , $ filename ) -> first ( ) ; } else if ( strpos ( $ filename , '/_versions/' ) !== false ) { $ file = FileVersion :: get ( ) -> filter ( 'Filename' , "/" . $ filename ) -> first ( ) ; } else { $ file = File :: get ( ) -> filter ( 'filename' , $ filename ) -> first ( ) ; } if ( $ file && $ file -> canView ( ) ) { if ( ! $ file -> CDNFile && ! $ file -> FilePointer ) { return $ this -> httpError ( 404 ) ; } // Permission passed redirect to file $ redirectLink = '' ; if ( $ file -> getViewType ( ) != CDNFile :: ANYONE_PERM ) { if ( $ file -> hasMethod ( 'getSecureURL' ) ) { $ redirectLink = $ file -> getSecureURL ( 180 ) ; } if ( ! strlen ( $ redirectLink ) ) { // can we stream it? return $ this -> sendFile ( $ file ) ; } } else { $ redirectLink = $ file -> getURL ( ) ; } if ( $ redirectLink && trim ( $ redirectLink , '/' ) != $ request -> getURL ( ) ) { $ response -> redirect ( $ redirectLink ) ; } else { return $ this -> httpError ( 404 ) ; } } else { if ( class_exists ( 'SecureFileController' ) ) { $ handoff = SecureFileController :: create ( ) ; return $ handoff -> handleRequest ( $ request , $ model ) ; } elseif ( $ file instanceof File ) { // Permission failure Security :: permissionFailure ( $ this , 'You are not authorised to access this resource. Please log in.' ) ; } else { // File doesn't exist $ response = new SS_HTTPResponse ( 'File Not Found' , 404 ) ; } } return $ response ; }
Process all incoming requests passed to this controller, checking
that the file exists and passing the file through if possible.
Process all incoming requests passed to this controller checking that the file exists and passing the file through if possible .