I left off the 'Dynamic download one'

Similar to the other routine.

C_BOOLEAN($0;$requestIsTypeACGI)
C_LONGINT($1;$streamRef;$FileRef;$DocSize)
C_TEXT($fileName;$extension;$outFolderPath)

$requestIsTypeACGI:=False
$streamRef:=$1
$FileRef:=0

READ ONLY(*)

READ WRITE([Utility_INT])

  `we'll grab the incoming http header from the stream
C_LONGINT($err)
C_TEXT($headerTxt)
$err:=ITK_TCPRcv
($streamRef;$headerTxt;32000;1;0;<>A4D_CRLF+<>A4D_CRLF;2*60*60)

$start:=Position(" ";$headerTxt)+1
$numChars:=Position("HTTP";$headerTxt)-1-$start
$URL:=Substring($headerTxt;$start;$numChars)

C_TEXT($savedFullURL)
$savedFullURL:=$URL

$pos:=Position("/";$URL)
If ($pos>0)
  $delimiter:="/"
Else
  $delimiter:="\"
End if

$start:=Position("downloadFileDynamic";$URL)
$URL:=Substring($URL;Position("downloadFileDynamic";$URL)+20)
C_TEXT($UserIDURL)
$UserIDURL:=$URL
  ` this should be the USER ID
$URL:=Substring($URL;1;Position($delimiter;$URL)-1)

C_BLOB($Response)

C_LONGINT($user_ID_l)
  ` Get User ID from URL
$pos:=Position("CID";$UserIDURL)
If ($pos>1)
  $tmp:=Substring($UserIDURL;$pos+3)
  $pos:=Position("/";$tmp)
  If ($pos>0)
    $user_ID_l:=Num(Substring($tmp;1;$pos-1))
      ` A4D_WRITE TO CONSOLE ("User ID :  "+String($user_ID_l))
  End if
End if

C_TEXT($Util_Ref_kf)
  ` Get User ID from URL
$pos:=Position("UID";$UserIDURL)
If ($pos>1)
  $Util_Ref_kf:=Substring($UserIDURL;$pos+3;14)
    `  A4D_WRITE TO CONSOLE ("Utility Keyfield = "+$Util_Ref_kf)
End if

C_LONGINT($PrivilegeError)
$PrivilegeError:=-1
If ($Util_Ref_kf#"")
  $PrivilegeError:=0
  QUERY([Utility_INT];[Utility_INT]KeyField=$Util_Ref_kf)
End if

If (Records in selection([Utility_INT])>0)  ` no error.
  C_BLOB($fullResponse)
  TEXT TO BLOB("HTTP/1.0 200 OK"+<>A4D_CRLF;$fullResponse;Text without
length ;*)
  TEXT TO BLOB("Cache-Control:no-cache"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
  TEXT TO BLOB("Pragma:no-cache"+<>A4D_CRLF;$fullResponse;Text without
length ;*)
  TEXT TO BLOB("Expires:"+ITK_Secs2RFC (ITK_Date2Secs (Current date;Current
time;1);1)+<>A4D_CRLF;$fullResponse;Text without length ;*)

  C_STRING(31;fileName_s31)
  C_TEXT($filePath)
  $filePath:=[Utility_INT]Text3
    `Check for ext, otherwise find file and use first with name+ext
  $fileName:=gGetShortName ($filePath;True)
  $extension:=gGetFileExtension ($fileName)
  If ($extension="")
    $outFolderPath:=gGetParentName ($filePath;True)
    $fileName:=$fileName+".@"
    <>SystemDocError:=0
    ARRAY TEXT($PSJ_DocList;0)
    ON ERR CALL("ERR_SystemsDocsError")
    DOCUMENT LIST($outFolderPath;$PSJ_DocList)
    ON ERR CALL("")
    $find:=Find in array($PSJ_DocList;$fileName)
    If ($find>0)
      $filePath:=$outFolderPath+$PSJ_DocList{$find}
    End if
  End if

    `  A4D_WRITE TO CONSOLE ($filePath)

  If ($filePath#"")
    $FileRef:=LDR_OpenFile ($filePath;2)
    $DocSize:=LDR_FileSize ($FileRef)

    If ($FileRef>0)
      TEXT TO BLOB("Content-Length:"+String(0)+<>A4D_CRLF;$fullResponse;Text
without length ;*)
      TEXT TO
BLOB("Content-Type:application/octet-stream"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
    Else
      TEXT TO BLOB("Sorry, unable to load asset.";$Response;Text without
length ;*)
      TEXT TO BLOB("Content-Length:"+String(BLOB
size($Response))+<>A4D_CRLF;$fullResponse;Text without length ;*)
      TEXT TO BLOB("Content-Type:text/plain"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
    End if
  Else   ` URL or privilege error.
    TEXT TO BLOB("Sorry, unable to load asset.";$Response;Text without
length ;*)
    TEXT TO BLOB("Content-Length:"+String(BLOB
size($Response))+<>A4D_CRLF;$fullResponse;Text without length ;*)
    TEXT TO BLOB("Content-Type:text/plain"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
  End if

    `We need one extra CRLF to separate the headers from the body
  TEXT TO BLOB(<>A4D_CRLF;$fullResponse;Text without length ;*)
  $err:=ITK_TCPSendBlob ($streamRef;$fullResponse)

  If ($FileRef>0)
    C_BLOB(a4d_tmpBlob)
    C_LONGINT($maxSize)
    $maxSize:=(1024*1024)
    Repeat
      $bytesRead:=LDR_ReadFile ($FileRef;a4d_tmpBlob;$maxSize)
      $err:=ITK_TCPSendBlob ($streamRef;a4d_tmpBlob)
      SET BLOB SIZE(a4d_tmpBlob;0)
    Until (($bytesRead#$maxSize) | ($err#0))
    $err:=LDR_CloseFile ($FileRef)
  Else
    $err:=ITK_TCPSendBlob ($streamRef;$Response)
  End if

  If (($err=0) & ($FileRef>0))  ` success
      ` change record type to Web Image Download
    [Utility_INT]RecordType:="Web Image Download"
    [Utility_INT]Text3:="Dynamic Download"
    [Utility_INT]Date1:=Current date
    QUERY([Contact_Names_BASE];[Contact_Names_BASE]Contact_ID=$user_ID_l)
    [Utility_INT]Text2:=[Contact_Names_BASE]NameConcat
    SAVE RECORD([Utility_INT])
    $parentPath:=gGetParentName ($filePath)
    $ret:=Util_Delete ($filePath)
    $folderSize:=gGetFolderSize ($parentPath)
    If ($folderSize=0)
      If ($parentPath[[Length($parentPath)]]="\")
        $parentPath:=Substring($parentPath;1;(Length($parentPath)-1))
      End if
      $ret:=Util_FinderDelete ($parentPath)
    End if
  Else
    If ($FileRef>0)
      DELETE RECORD([Utility_INT])  ` remove record for $FileRef  = 0
    End if
  End if

Else   ` NO Current session for user or user ID invalid
  C_BLOB($fullResponse;$Response)
  TEXT TO BLOB("HTTP/1.0 200 OK"+<>A4D_CRLF;$fullResponse;Text without
length ;*)
  TEXT TO BLOB("Cache-Control:no-cache"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
  TEXT TO BLOB("Pragma:no-cache"+<>A4D_CRLF;$fullResponse;Text without
length ;*)
  TEXT TO BLOB("Expires:"+ITK_Secs2RFC (ITK_Date2Secs (Current date;Current
time;1);1)+<>A4D_CRLF;$fullResponse;Text without length ;*)

  TEXT TO BLOB("Sorry, unable to load asset.";$Response;Text without length
;*)

  TEXT TO BLOB("Content-Length:"+String(BLOB
size($Response))+<>A4D_CRLF;$fullResponse;Text without length ;*)
  TEXT TO BLOB("Content-Type:text/plain"+<>A4D_CRLF;$fullResponse;Text
without length ;*)
  TEXT TO BLOB(<>A4D_CRLF;$fullResponse;Text without length ;*)

  $err:=ITK_TCPSendBlob ($streamRef;$fullResponse)
  $err:=ITK_TCPSendBlob ($streamRef;$Response)
End if

Unload (->[Utility_INT])



Reply via email to