<cfinclude template = '../__Security-Block.cfm' />
<cfsetting enablecfoutputonly="Yes">
<cfprocessingdirective suppresswhitespace="Yes">
 <cfparam name='url.id' default='-1' />

 <cfquery  name='getFile' datasource='#Application.DataSrc#'>
  SELECT FileName, FileType, File, Title, Dup_file_name
  FROM   #table_files#
  WHERE  id = '#url.id#'
  AND    AgencyId = '#session.AgencyId#'
 </cfquery>
 
  <cfquery  name='getFile_name' datasource='#Application.DataSrc#'>
  SELECT *
  FROM   #table_files#
  WHERE  id = '#url.id#'
  AND    AgencyId = '#session.AgencyId#' AND Created >= '2017-04-26' and  Created < '2017-05-18'
 </cfquery>
 
 
   <cfquery  name='getFilename' datasource='#Application.DataSrc#'>
  SELECT *
  FROM   #table_files#
  WHERE  id = '#url.id#'
  AND    AgencyId = '#session.AgencyId#' AND  Created >= '2017-05-18'
 </cfquery>
 
 <!--- Check if file exists in regular S3 locations --->
 <cfset fileExistsInRegularS3 = false />
 <cfset isAIIntakeFile = false />
 <cfset useAIS3Bucket = false />
 
 <!--- Check if file exists in regular S3 locations (getFile_name or getFilename) --->
 <cfif getFile_name.Recordcount gt 0 OR getFilename.Recordcount gt 0>
     <cfset fileExistsInRegularS3 = true />
 </cfif>
 
 <!--- Check if file exists locally (in database File field) --->
 <cfset fileExistsLocally = false />
 <cfif getFile.recordcount GT 0>
     <cfif structKeyExists(getFile, "File") AND isDefined("getFile.File") AND len(getFile.File) GT 0>
         <cfset fileExistsLocally = true />
     </cfif>
 </cfif>
 
 <!--- Check if Title contains 'AI Intake' --->
 <cfif getFile.recordcount GT 0 AND structKeyExists(getFile, "Title") AND len(trim(getFile.Title)) GT 0>
     <cfif findNoCase("AI Intake", getFile.Title) GT 0>
         <cfset isAIIntakeFile = true />
     </cfif>
 </cfif>
 
 <!--- If file doesn't exist in regular locations AND Title contains 'AI Intake', use AI S3 bucket --->
 <cfif NOT fileExistsInRegularS3 AND NOT fileExistsLocally AND isAIIntakeFile AND getFile.recordcount GT 0 AND structKeyExists(getFile, "Dup_file_name") AND len(trim(getFile.Dup_file_name)) GT 0>
     <cfset useAIS3Bucket = true />
 </cfif>
 
<!---
  <cfheader name="Content-Disposition" value="attachment; filename=#getFile.FileName#">
  <cfcontent variable="#ToBinary(getFile.File)#" />
---><!--- <cfif CGI.REMOTE_ADDR EQ '115.97.87.14'>
<cfabort>
</cfif> --->

<!--- If file doesn't exist in regular locations AND Title contains 'AI Intake', download from AI S3 bucket --->
<cfif isAIIntakeFile >
    <!--- AI Intake files from s3-pattii-extractaiapi-assets bucket --->
    <cfset ai_s3_bucket = "s3-pattii-extractaiapi-assets" />
    <cfset ai_s3_accesskey = "AKIA435RTMNDOUH4J3YW" />
    <cfset ai_s3_secretkey = "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" />
    <cfset ai_s3_path = "agency_" & session.AgencyId />
    
    <cftry>
        <cfheader name="Content-Disposition" value="attachment; filename=#Rereplace(getFile.FileName,',','','all')#" />
        <cfcontent type="#getFile.FileType#" file="s3://#ai_s3_accesskey#:#ai_s3_secretkey#@#ai_s3_bucket#/#ai_s3_path#/#getFile.Dup_file_name#" />
        <cfcatch type="any">
            <!--- If AI S3 file doesn't exist, try regular file handling --->
            <cfset useAIS3Bucket = false />
            <!--- Fall through to regular file handling below --->
        </cfcatch>
    </cftry>
</cfif>

<!--- Regular file handling if not using AI S3 bucket or if AI S3 bucket failed --->
<cfif NOT useAIS3Bucket>
    <cfif getFile_name.Recordcount gt 0>
        <cfheader name="Content-Disposition" value="attachment; filename=#Rereplace(getFile_name.FileName,',','','all')#" />
        <cfcontent type="#getFile.FileType#" file="/s3://#Request.bucket_accesskey#:#Request.bucket_secretkey#@s3.amazonaws.com/homecare/#getFile_name.Dup_file_name#" />
    
    <cfelseif getFilename.Recordcount gt 0 >
        <cfset path = 'Patient_files/agency_'&#session.AgencyId# />
        <cfheader name="Content-Disposition" value="attachment; filename=#Rereplace(getFilename.FileName,',','','all')#" />
        <cfcontent type="#getFile.FileType#" file="s3://#Request.bucket_accesskey#:#Request.bucket_secretkey#@#Request.bucket_name#/#path#/#getFilename.Dup_file_name#" />
    
    <cfelseif getFile.recordcount GT 0 AND fileExistsLocally>
        <cfheader name="content-length" value="#ArrayLen(getFile.File)#" />
        <cfheader name="content-disposition" value="attachment; filename=#Rereplace(getFile.FileName,',','','all')#" />
        <cfcontent type="#getFile.FileType#" variable="#getFile.File#" />
    <cfelse>
        <!--- File not found --->
        <cfheader name="Content-Type" value="text/html" />
        <cfoutput>
            <h2>File Not Found</h2>
            <p>The requested file could not be found.</p>
        </cfoutput>
    </cfif>
</cfif>
</cfprocessingdirective>
