modification for file download from genome browser
Project: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/repo Commit: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/commit/d5533b06 Tree: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/tree/d5533b06 Diff: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/diff/d5533b06 Branch: refs/heads/dreg-gateway Commit: d5533b068720e5ea1770ab16811539cfd8106ad9 Parents: 2c64ac0 Author: root <root@osboxes> Authored: Sat Feb 11 04:53:36 2017 +0000 Committer: root <root@osboxes> Committed: Sat Feb 11 04:53:36 2017 +0000 ---------------------------------------------------------------------- app/libraries/PartialDownload.php | 174 --------------------------------- app/libraries/basecode.php | 98 +++++++++++++++++++ app/routes.php | 40 ++++---- 3 files changed, 118 insertions(+), 194 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d5533b06/app/libraries/PartialDownload.php ---------------------------------------------------------------------- diff --git a/app/libraries/PartialDownload.php b/app/libraries/PartialDownload.php deleted file mode 100644 index eb10775..0000000 --- a/app/libraries/PartialDownload.php +++ /dev/null @@ -1,174 +0,0 @@ -<?php -/* - -The following byte serving code is (C) 2004 Razvan Florian. You may find the latest version at -http://www.coneural.org/florian/papers/04_byteserving.php - -*/ -function set_range($range, $filesize, &$first, &$last){ - /* - Sets the first and last bytes of a range, given a range expressed as a string - and the size of the file. - - If the end of the range is not specified, or the end of the range is greater - than the length of the file, $last is set as the end of the file. - - If the begining of the range is not specified, the meaning of the value after - the dash is "get the last n bytes of the file". - - If $first is greater than $last, the range is not satisfiable, and we should - return a response with a status of 416 (Requested range not satisfiable). - - Examples: - $range='0-499', $filesize=1000 => $first=0, $last=499 . - $range='500-', $filesize=1000 => $first=500, $last=999 . - $range='500-1200', $filesize=1000 => $first=500, $last=999 . - $range='-200', $filesize=1000 => $first=800, $last=999 . - - */ - $dash=strpos($range,'-'); - $first=trim(substr($range,0,$dash)); - $last=trim(substr($range,$dash+1)); - if ($first=='') { - //suffix byte range: gets last n bytes - $suffix=$last; - $last=$filesize-1; - $first=$filesize-$suffix; - if($first<0) $first=0; - } else { - if ($last=='' || $last>$filesize-1) $last=$filesize-1; - } - if($first>$last){ - //unsatisfiable range - header("Status: 416 Requested range not satisfiable"); - header("Content-Range: */$filesize"); - exit; - } -} - -function buffered_read($file, $bytes, $buffer_size=1024){ - /* - Outputs up to $bytes from the file $file to standard output, $buffer_size bytes at a time. - */ - $bytes_left=$bytes; - while($bytes_left>0 && !feof($file)){ - if($bytes_left>$buffer_size) - $bytes_to_read=$buffer_size; - else - $bytes_to_read=$bytes_left; - $bytes_left-=$bytes_to_read; - $contents=fread($file, $bytes_to_read); - echo $contents; - flush(); - } -} - -function byteserve($filename){ - /* - Byteserves the file $filename. - - When there is a request for a single range, the content is transmitted - with a Content-Range header, and a Content-Length header showing the number - of bytes actually transferred. - - When there is a request for multiple ranges, these are transmitted as a - multipart message. The multipart media type used for this purpose is - "multipart/byteranges". - */ - - $fileext = strtoupper( pathinfo($filename, PATHINFO_EXTENSION) ); - $filesize=filesize($filename); - $file=fopen($filename,"rb"); - - $ranges=NULL; - if ($_SERVER['REQUEST_METHOD']=='GET' && isset($_SERVER['HTTP_RANGE']) && $range=stristr(trim($_SERVER['HTTP_RANGE']),'bytes=')){ - $range=substr($range,6); - $boundary='g45d64df96bmdf4sdgh45hf5';//set a random boundary - $ranges=explode(',',$range); - } - - if($ranges && count($ranges)){ - header("HTTP/1.1 206 Partial content"); - header("Accept-Ranges: bytes"); - if(count($ranges)>1){ - /* - More than one range is requested. - */ - - //compute content length - $content_length=0; - foreach ($ranges as $range){ - set_range($range, $filesize, $first, $last); - $content_length+=strlen("\r\n--$boundary\r\n"); -if ($fileext=="PDF") - $content_length+=strlen("Content-type: application/pdf\r\n"); -else - $content_length+=strlen("Content-type: application/octet-stream\r\n"); - $content_length+=strlen("Content-range: bytes $first-$last/$filesize\r\n\r\n"); - $content_length+=$last-$first+1; - } - $content_length+=strlen("\r\n--$boundary--\r\n"); - - //output headers - header("Content-Length: $content_length"); - //see http://httpd.apache.org/docs/misc/known_client_problems.html for an discussion of x-byteranges vs. byteranges - header("Content-Type: multipart/x-byteranges; boundary=$boundary"); - - //output the content - foreach ($ranges as $range){ - set_range($range, $filesize, $first, $last); - echo "\r\n--$boundary\r\n"; -if ($fileext=="PDF") - echo "Content-type: application/pdf\r\n"; -else - echo "Content-type: application/octet-stream\r\n"; - echo "Content-range: bytes $first-$last/$filesize\r\n\r\n"; - fseek($file,$first); - buffered_read ($file, $last-$first+1); - } - echo "\r\n--$boundary--\r\n"; - } else { - /* - A single range is requested. - */ - $range=$ranges[0]; - set_range($range, $filesize, $first, $last); - header("Content-Length: ".($last-$first+1) ); - header("Content-Range: bytes $first-$last/$filesize"); -if ($fileext=="PDF") - header("Content-Type: application/pdf"); -else - header("Content-Type: application/octet-stream"); - fseek($file,$first); - buffered_read($file, $last-$first+1); - } - } else{ - //no byteserving - header("Accept-Ranges: bytes"); - header("Content-Length: $filesize"); -if ($fileext=="PDF") - header("Content-Type: application/pdf"); -else - header("Content-Type: application/octet-stream"); - readfile($filename); - } - fclose($file); -} - -function serve($filename, $download=0){ - //Just serves the file without byteserving - //if $download=true, then the save file dialog appears - $filesize=filesize($filename); - header("Content-Length: $filesize"); - header("Content-Type: application/octet-stream"); - $filename_parts=pathinfo($filename); - if($download) header('Content-disposition: attachment; filename='.$filename_parts['basename']); - readfile($filename); -} - -//unset magic quotes; otherwise, file contents will be modified -//set_magic_quotes_runtime(0); - -//do not send cache limiter header -//ini_set('session.cache_limiter','none'); - http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d5533b06/app/libraries/basecode.php ---------------------------------------------------------------------- diff --git a/app/libraries/basecode.php b/app/libraries/basecode.php new file mode 100644 index 0000000..bab9fd2 --- /dev/null +++ b/app/libraries/basecode.php @@ -0,0 +1,98 @@ +<?php +class Base32 +{ + const BITS_5_RIGHT = 31; + protected static $CHARS = '0123456789abcdefghijklmnopqrstuv'; + + public static function encode($data) + { + $dataSize = strlen($data); + $res = ''; + $remainder = 0; + $remainderSize = 0; + + for ($i = 0; $i < $dataSize; $i++) + { + $b = ord($data[$i]); + $remainder = ($remainder << 8) | $b; + $remainderSize += 8; + while ($remainderSize > 4) + { + $remainderSize -= 5; + $c = $remainder & (self::BITS_5_RIGHT << $remainderSize); + $c >>= $remainderSize; + $res .= self::$CHARS[$c]; + } + } + if ($remainderSize > 0) + { + // remainderSize < 5: + $remainder <<= (5 - $remainderSize); + $c = $remainder & self::BITS_5_RIGHT; + $res .= self::$CHARS[$c]; + } + + return $res; + } + + public static function decode($data) + { + $data = strtolower($data); + $dataSize = strlen($data); + $buf = 0; + $bufSize = 0; + $res = ''; + + for ($i = 0; $i < $dataSize; $i++) + { + $c = $data[$i]; + $b = strpos(self::$CHARS, $c); + if ($b === false) + throw new Exception('Encoded string is invalid, it contains unknown char #'.ord($c)); + $buf = ($buf << 5) | $b; + $bufSize += 5; + if ($bufSize > 7) + { + $bufSize -= 8; + $b = ($buf & (0xff << $bufSize)) >> $bufSize; + $res .= chr($b); + } + } + + return $res; + } +} + +function rbase64_encode($string){ + $string = base64_encode($string); + $reversed_string=""; + $string_length = strlen($string); + + for($i=$string_length-1;$i>-1;$i--) + { + if ($string[$i]==='=') + $reversed_string .= '_'; + else + $reversed_string .= $string[$i]; + } + return $reversed_string; +} + + +function rbase64_decode($rstring){ + $string=""; + $string_length = strlen($rstring); + + for($i=$string_length-1;$i>-1;$i--) + { + if ($rstring[$i]==='_') + $string .= '='; + else + $string .= $rstring[$i]; + } + + return base64_decode($string); +} + + +?> http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d5533b06/app/routes.php ---------------------------------------------------------------------- diff --git a/app/routes.php b/app/routes.php index a9f40be..81e1ca5 100755 --- a/app/routes.php +++ b/app/routes.php @@ -171,14 +171,14 @@ Route::get("gbrowser/{filelist}", function($filelist){ $folder_path=$filelist[0]. "ARCHIVE" ; $content = "[ \n"; -include("base32.php"); - + include("libraries/basecode.php"); + for($i=1; $i<3; $i++){ $content = $content . ' { type:"bigwig", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode($filelist[$i*2]). '", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode($filelist[$i*2]). '", name: "'. $filelist[$i*2-1] .'", - fixedscale:{min:0,max:20}, + #fixedscale:{min:0,max:20}, colorpositive:"#B30086", colornegative:"#0000e5", height:100, @@ -186,9 +186,10 @@ include("base32.php"); },'. "\n" ; } - $content = $content . '{ + + $content = $content . '{ type:"bedgraph", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode($folder_path . '/out.dREG.pred.gz').'", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode($folder_path . '/out.dREG.pred.gz').'", name: "dREG informative pos.:", mode: "show", colorpositive:"#B30086", @@ -198,9 +199,9 @@ include("base32.php"); #fixedscale:{min:0, max:1}, },'. "\n"; - $content = $content . '{ + $content = $content . '{ type:"bedgraph", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode( $folder_path . '/out.dREG.peak.gz').'", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode( $folder_path . '/out.dREG.peak.gz').'", name: "dREG Peak Calling:", mode: "show", colorpositive:"#B30086", @@ -210,9 +211,9 @@ include("base32.php"); #fixedscale:{min:0, max:1}, },'. "\n"; - $content = $content . '{ + $content = $content . '{ type:"bigwig", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode( $folder_path . '/out.dREG.HD.imputedDnase.bw').'", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode( $folder_path . '/out.dREG.HD.imputedDnase.bw').'", name: "imputed DNase-I signal:", #fixedscale:{min:0,max:20}, colorpositive:"#00B306", @@ -222,7 +223,7 @@ include("base32.php"); $content = $content . '{ type:"bedgraph", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode( $folder_path . '/out.dREG.HD.relaxed.bed').'", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode( $folder_path . '/out.dREG.HD.relaxed.bed.gz').'", name: "dREG.HD relaxed peaks:", mode: "show", colorpositive:"#0000e5/#B30086", @@ -233,7 +234,7 @@ include("base32.php"); $content = $content . '{ type:"bedgraph", - url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.base32::encode( $folder_path . '/out.dREG.HD.stringent.bed').'", + url:"'.$protocol.'://'. $_SERVER['HTTP_HOST'] .'/gbfile/'.rbase64_encode( $folder_path . '/out.dREG.HD.stringent.bed.gz').'", name: "dREG.HD stringent peaks:", mode: "show", colorpositive:"#0000e5/#B30086", @@ -252,26 +253,25 @@ include("base32.php"); Route::get("gbfile/{file}", function($file){ $filename = pathinfo($file, PATHINFO_FILENAME); $fileext = pathinfo($file, PATHINFO_EXTENSION); -include("base32.php"); + + include("libraries/basecode.php"); if( $fileext != "") - $file = base32::decode( $filename ) .".".$fileext; + $file = rbase64_decode( $filename ) .".".$fileext; else - $file = base32::decode( $filename ); + $file = rbase64_decode( $filename ); if(0 === strpos($file, '/')){ $file = substr($file, 1); } $downloadLink = Config::get('pga_config.airavata')['experiment-data-absolute-path'] . '/' . $file; + if ( !file_exists($downloadLink) ) - return Response::make('', 204); + return Response::make("", 204); else { if ($_SERVER["REQUEST_METHOD"]=="GET") - { - include 'libraries/PartialDownload.php'; - return byteserve( $downloadLink); - } + return Response::download($downloadLink); else return Response::make("", 200) ->header('Content-Length', filesize($downloadLink));