Reflex — File Browser

Reflexv0.1 / Reflexv0.1 / Reflex / system / scripts / download.mrc

download.mrc — 9.38 KB — Download this file

;=======================================================================================================
;
;    HTTP Downloader v1.2b by ^BeAsT^ 
;                              edited by Cypher
;
;=======================================================================================================
;
;    /download [-oh] <name> <url> [directory] [$cr alias]
;
;        -o will overwrite a file if it exists. useful if the download becomes corrupted.
;        -h will hide the progress dialog.
;
;           NAME is used to reference the download in the signal 'download'.
;           URL is the path to the file on the HTTP server.
;           If DIRECTORY is not supplied then a default directory of $sysdir $+ /download/ 
;         is used. The file will be named the same as it is on the http server.
;           The ALIAS, if provided will be run on completion with the filepath of
;         the download as its parameters ($1-).
;
;=======================================================================================================
;
;    <Cypher> All i added was a progress dialog and the -h flag to hide it aswell as an 
;             alias which runs when the download is complete. ^BeAsT^ did all the hard 
;             work. The original unmodified snippet can be found on http://mircscripts.org/
;
;=======================================================================================================

on *:signal:download: {
  if ($1 == init) && (!$download($2).hide) { 
    dialog -m download $+ $2 download
  }
  elseif ($1 == write) {
    if ($dialog(download $+ $2)) && (!$download($2).hide) {
      dialog -t download $+ $2 Downloading File - $download($2).pc $+ %
      did -a download $+ $2 1 $download($2).pc
      did -o download $+ $2 2 1 Recieving: $gettok($download($2).file,-1,92)
      did -o download $+ $2 8 1 $download($2).file
      did -o download $+ $2 3 1 Size: $bytes($gettok($download($2).rcvd,-1,92),k3).suf
      did -o download $+ $2 5 1 of $bytes($gettok($download($2).size,-1,92),k3).suf
      did -o download $+ $2 6 1 Tramsfer Rate: $bytes($gettok($download($2).cps,-1,92),k3).suf
    }
  }
  elseif ($1 == complete) {
    if ($download($2).alias) {   
      if ($dialog(download $+ $2)) { dialog -x $v1 }
      $download($2).alias $shortfn($download($2).file) 
    }
    elseif ($dialog(download $+ $2)) && (!$download($2).hide) { 
      if ($right($download($2).file,3) == mrc) { did -o $dialog(download $+ $2) 9 1 Load }
      did -e $dialog(download $+ $2) 9
    }
  }
  elseif ($1 == fail) {
    if ($3- == Requested Range Not Satisfiable) {
      if ($download($2).alias) { $download($2).alias $shortfn($download($2).file)  }  
      else { run $shortfn($download($2).file) }
      return 
    }
    else {
      if ($dialog(download $+ $2)) && (!$download($2).hide) { dialog -x download $+ $2 }
      echo -a download fail: $2 $+ : $3- 
    }
  }
}
dialog Download {
  title "Downloading File"
  size -1 -1 150 68
  option dbu
  text "", 1, 6 18 138 9
  text "Recieving:", 2, 6 6 138 9
  text "Size:", 3, 6 31 66 8
  box "", 4, 2 0 146 53
  text "of", 5, 76 31 68 8
  text "Transfer Rate:", 6, 6 42 67 8
  button "Cancel", 7, 111 55 37 12, cancel
  text "filename", 8, 6 42 67 8, hide
  button "Open", 9, 72 55 37 12, disable ok
}
on *:dialog:download*:init:0: {
  mdx SetMircVersion $version
  mdx MarkDialog $dname
  mdx SetDialog $dname Icon
  mdx SetControlMDX 1 progressbar smooth > $mdx_ctl_gen
  did -a $dname 1 0 0 100
  did -a $dname 1 barcolor $color($color(background))
  var %n = $right($dname,-8)
  did -o $dname 2 1 Recieving: $gettok($download(%n).file,-1,92)
}
on *:dialog:download*:sclick:7: { sockclose $dname }
on *:dialog:download*:sclick:9: {
  var %f = $did($dname,8) 
  $iif($right(%f,3) == mrc,.load -rs,run) %f
}

alias download {
  if (!$isid) {
    var %a
    if ($numtok($1-,13) == 2) { var %a = $gettok($1-,2,13) | tokenize 32 $gettok($1-,1,13) }
    var %4 , %h
    if ($left($1,1) == -) {
      if (o isin $1) { %4 = $true }
      if (h isin $1) { %h = $true }
      tokenize 32 $2-
    }
    var %1 = download $+ $1 , %2 = $longfn($3-)
    if (!$2) { var %3 = insufficient parameters }
    elseif ($sock(%1)) { var %3 = $+(',$1,') name in use }
    elseif (!$isdir(%2)) { var %2 = $longfn($sysdir) $+ \download\ }
    if (%3) { if ($show) { echo $color(info) -se * /download: %3 } | return }
    unset % [ $+ [ %1 $+ .* ] ]
    set % [ $+ [ %1 $+ .file ] ] $+(%2,$iif($right(%2,1) != $chr(92),$chr(92)),$gettok($2,-1,47))
    set % [ $+ [ %1 $+ .url ] ] http:// $+ $remove($2,http://))
    set % [ $+ [ %1 $+ .ctime ] ] $ctime 0
    set % [ $+ [ %1 $+ .status ] ] Connecting
    if (%a) { set % [ $+ [ %1 $+ .alias ] ] %a }
    if (%h) { set % [ $+ [ %1 $+ .hide ] ] $true }
    if (%4) { .remove $+(",$dl.var(%1,file),") }
    sockopen %1 $gettok($remove($2,http://),1,47) 80
  }
  else {
    if ($1 == 0) { return $sock(download*,0) }
    if ($iif($1 isnum,$sock(download*,$1),$sock(download $+ $1))) {
      var %1 = $ifmatch,%2 = $dl.var(%1,file),%3 = $dl.var(%1,size),%4 = $file(%2).size
      if (!$prop) { return $right(%1,-8) }
      elseif ($prop == ip) { return $sock(%1).ip }
      elseif ($prop == status) { return $dl.var(%1,status) }
      elseif ($prop == url) { return $dl.var(%1,url) }
      elseif ($prop == file) { return %2 }
      elseif ($prop == alias) { return $dl.var(%1,alias) }
      elseif ($prop == hide) { return $dl.var(%1,hide) }
      elseif ($prop == type) { return $dl.var(%1,type) }
      elseif ($prop == size) { return %3 }
      elseif ($prop == rcvd) { return %4 }
      elseif ($prop == cps) { return $int($calc(%4 / ($ctime - $dl.var(%1,ctime,2)))) }
      elseif ($prop == pc) { return $int($calc($file(%2).size * 100 / %3)) }
      elseif ($prop == secs) { return $calc($ctime - $dl.var(%1,ctime,1)) }
    }
  }
}
alias -l dl.var { return $gettok(% [ $+ [ $+($1,.,$2) ] ],$iif(!$3,1-,$3),32) }
alias -l dl.fail { var %1 = $right($1,-8) | .signal -n download fail %1 $2- | close -d %1 }
alias -l dl.httpcode {
  var %1 = $1 | goto %1
  :100 | return Continue
  :101 | return Switching Protocols
  :200 | return OK
  :201 | return Created
  :202 | return Accepted
  :203 | return Non-Authoritative Information
  :204 | return No Content
  :205 | return Reset Content
  :206 | return Partial Content
  :300 | return Multiple Choices
  :301 | return Moved Permanently
  :302 | return Moved Temporarily
  :303 | return See Other
  :304 | return Not Modified
  :305 | return Use Proxy
  :400 | return Bad Request
  :401 | return Unauthorized
  :402 | return Payment Required
  :403 | return Forbidden
  :404 | return Not Found
  :405 | return Method Not Allowed
  :406 | return Not Acceptable
  :407 | return Proxy Authentication Required
  :408 | return Request Time-out
  :409 | return Conflict
  :410 | return Gone
  :411 | return Length Required
  :412 | return Precondition Failed
  :413 | return Request Entity Too Large
  :414 | return Request-URI Too Large
  :415 | return Unsupported Media Type
  :416 | return Requested Range Not Satisfiable
  :500 | return Internal Server Error
  :501 | return Not Implemented
  :502 | return Bad Gateway
  :503 | return Service Unavailable
  :504 | return Gateway Time-out
  :505 | return HTTP Version not supported
  :%1 | return
}
alias close {
  if ($1 == -d) {
    var %1 = download $+ $2
    if ($sock(%1)) { unset % [ $+ [ %1 $+ .* ] ] | sockclose %1 }
  }
  else { close $1- }
}

on *:sockopen:download*:{
  if ($sockerr) { dl.fail $sockname Unable to Connect | return }
  var %1 = $dl.var($sockname,url),%2 = $dl.var($sockname,file)
  set % [ $+ [ $sockname $+ .status ] ] Requesting File
  sockwrite -tn $sockname GET %1 HTTP/1.1
  sockwrite -tn $sockname Host: $gettok($remove(%1,http://),1,47)
  sockwrite -tn $sockname Accept: *.*, */*
  sockwrite -tn $sockname Range: $+(bytes=,$iif($exists(%2),$file(%2).size,0),-)
  sockwrite -tn $sockname Connection: close
  sockwrite -tn $sockname $crlf
  .signal -n download init $right($sockname,-8)
}
on *:sockclose:download*:{ if ($dl.var($sockname,status) != done) { dl.fail $sockname Disconnected } }
on *:sockread:download*:{
  if ($sockerr) { dl.fail $sockname Disconnected | return }
  if ($dl.var($sockname,status) != downloading) {
    var %1 | sockread %1 | tokenize 32 %1
    if ($1 == HTTP/1.1) { if ($2 != 200 && $2 != 206) { dl.fail $sockname $iif($dl.httpcode($2),$ifmatch,$3-) } }
    elseif ($1 == Content-Range:) { set % [ $+ [ $sockname $+ .size ] ] $gettok($3,-1,47) }
    elseif ($1 == Content-Type:) { set % [ $+ [ $sockname $+ .type ] ] $2- }
    elseif (!$1) {
      set % [ $+ [ $sockname $+ .ctime ] ] $dl.var($sockname,ctime,1) $ctime
      set % [ $+ [ $sockname $+ .status ] ] Downloading
      return
    }
  }
  else {
    var %1 = $dl.var($sockname,file)
    :sockread
    sockread &1
    if (!$sockbr) { return }
    bwrite $+(",%1,") -1 &1
    .signal -n download write $right($sockname,-8)
    unset % [ $+ [ $sockname $+ .resume ] ]
    if ($file(%1).size < $dl.var($sockname,size)) { goto sockread }
    else {
      var %1 = $right($sockname,-8),%2 = $dl.var($sockname,file)
      set % [ $+ [ $sockname $+ .status ] ] Done
      .signal -n download complete %1
      close -d %1
    }
  }
}
on *:exit:{ unset %download* | sockclose download* }
on *:unload:{ unset %download* | sockclose download* }