app\models\Document::getDirectFileUrl PHP Method

getDirectFileUrl() public static method

public static getDirectFileUrl ( $path, $disk, boolean $prioritizeSpeed = false ) : null | string
$path
$disk
$prioritizeSpeed boolean
return null | string
    public static function getDirectFileUrl($path, $disk, $prioritizeSpeed = false)
    {
        $adapter = $disk->getAdapter();
        $fullPath = $adapter->applyPathPrefix($path);
        if ($adapter instanceof \League\Flysystem\AwsS3v3\AwsS3Adapter) {
            $client = $adapter->getClient();
            $command = $client->getCommand('GetObject', ['Bucket' => $adapter->getBucket(), 'Key' => $fullPath]);
            return (string) $client->createPresignedRequest($command, '+10 minutes')->getUri();
        } else {
            if (!$prioritizeSpeed && $adapter instanceof \League\Flysystem\Rackspace\RackspaceAdapter) {
                $secret = env('RACKSPACE_TEMP_URL_SECRET');
                if ($secret) {
                    $object = $adapter->getContainer()->getObject($fullPath);
                    if (env('RACKSPACE_TEMP_URL_SECRET_SET')) {
                        // Go ahead and set the secret too
                        $object->getService()->getAccount()->setTempUrlSecret($secret);
                    }
                    $url = $object->getUrl();
                    $expiry = strtotime('+10 minutes');
                    $urlPath = urldecode($url->getPath());
                    $body = sprintf("%s\n%d\n%s", 'GET', $expiry, $urlPath);
                    $hash = hash_hmac('sha1', $body, $secret);
                    return sprintf('%s?temp_url_sig=%s&temp_url_expires=%d', $url, $hash, $expiry);
                }
            }
        }
        return null;
    }

Usage Example

Example #1
0
 public function getLogoURL($cachebuster = false)
 {
     if (!$this->hasLogo()) {
         return null;
     }
     $disk = $this->getLogoDisk();
     $adapter = $disk->getAdapter();
     if ($adapter instanceof \League\Flysystem\Adapter\Local) {
         // Stored locally
         $logo_url = str_replace(public_path(), url('/'), $adapter->applyPathPrefix($this->logo), $count);
         if ($cachebuster) {
             $logo_url .= '?no_cache=' . time();
         }
         if ($count == 1) {
             return str_replace(DIRECTORY_SEPARATOR, '/', $logo_url);
         }
     }
     return Document::getDirectFileUrl($this->logo, $this->getLogoDisk());
 }