compile function httpSvr_GetReposImage( httpServer, requestID, requestHeaders )
/*
   ****************************************************************************
   ** IF YOU WANT TO MODIFY THIS FOR YOUR OWN APPLICATIONS PLEASE USE A COPY **
   ** DO NOT CHANGE THIS PROGRAM AS IT MAY BE OVERWRITTEN BY FUTURE UPDATES! **
   ****************************************************************************  
   
   ** Copyright (C) 2012-2022 Revelation Software Inc. All Rights Reserved **
   
   Author   : Wile C Coyote - Super Genius
   Date     : August 2022
   Purpose  : Simple function for the HTTPSERVER control to return 
            : repository images.
   
   Query Parameters
   ================
   
          classid : Repository CLASSID of the image (defaults to "PNG")
   [req]  entid   : Repository ENTITYID of the image
          useFile : If TRUE$ ("1") then return the image via the server's
                  : SetResponseContentFile method, otherwise return via the 
                  : normal SetResponseContent method.
          dpi     : Specifies the requested DPI for the image - this can be 
                  : an actual DPI (like 96,192 etc) or a percentage (100%,
                  : 200%) etc (Defaults to 96)
   
   Comments
   ========
   
   Amended  Date        Reason
   =======  ====        ======
*/
   #pragma precomp event_precomp

   declare function repository, rti_ResolvePath, rti_ErrorText, rti_UC
   $insert ps_HTTPServer_Equates
   $insert repository_Equates
   $insert reposErrors
   $insert rti_SSP_Equates
   $insert logical
   
   equ BASE_DPI$ to 96
   
   errStat  = FALSE$
   errInfo  = ""
   
   classID  = ""
   entID    = ""
   dpi      = ""
   bUseFile = FALSE$
   mimeType = ""
   
   // NOTE: We're expecting this to be a GET request - if it's POST then we
   // need to extract these arguments from the request content as they won't 
   // be in the query names and values fields.
   
   queryNames = requestHeaders<PS_HSVR_REQHDR_QUERYNAMES$>
   
   locateC "classid" in queryNames using @vm setting pos then
      classID = rti_UC( trim( requestHeaders<PS_HSVR_REQHDR_QUERYVALUES$,pos> ) )
   end
   
   locateC "entid" in queryNames using @vm setting pos then
      entID = rti_UC( trim( requestHeaders<PS_HSVR_REQHDR_QUERYVALUES$,pos> ) )
   end
   
   locateC "useFile" in queryNames using @vm setting pos then
     bUseFile = trim( requestHeaders<PS_HSVR_REQHDR_QUERYVALUES$,pos> )
   end
   
   locateC "dpi" in queryNames using @vm setting pos then
     dpi = trim( requestHeaders<PS_HSVR_REQHDR_QUERYVALUES$,pos> )
   end
   
   if bLen( classID ) else
      classID = "PNG"
   end
   
   if bLen( dpi ) then
      if ( dpi[-1,1] = "%" ) then
         convert "%" to "" in dpi
         if num( dpi ) then
            dpi = int( ( dpi / 100 ) * BASE_DPI$ )
         end else
            dpi = ""
         end
      end
   end
   
   if ( dpi ) then
     if ( dpi < BASE_DPI$ ) then
        dpi = BASE_DPI$
     end
   end else
      dpi = BASE_DPI$
   end
   
   begin case
      case ( classID == "JPG" )
         mimeType = "image/jpeg"
      case ( classID == "GIF" )
         mimeType = "image/gif"
      case ( classID == "BMP" )
         mimeType = "image/bmp"
      case ( classID == "PNG" )
         mimeType = "image/png"
      case OTHERWISE$
         null
   end case
       
   if bLen( entID ) then
      reposID  = @appID<1> : "*IMAGE*" : classID : "*" : entID
      
      call set_Status( SETSTAT_OK$ )
      
      fileNames = repository( "GETSUBKEY", reposID )
      if get_Status( errInfo ) then
         if ( errInfo<1,1> == REP_ENT_NOEXISTS_ERR$ ) then
            @httpServer->setResponseStatus( requestID, 404 )
         end else
            goSub setHTTPError
         end
      end else
      
         goSub resolveFileNameForDPI
      
         if bLen( filename ) then
            fileName = rti_ResolvePath( fileName, "" )
            
            if bUseFile then
            
               @httpServer->setResponseFile( requestID, fileName )
               @httpServer->setResponseHeader( requestID, "Content-Type", mimeType )
            
            end else
               osRead fileContents from fileName then
                  @httpServer->setResponseContent( requestID, fileContents )
                  @httpServer->setResponseHeader( requestID, "Content-Type", mimeType )
               end else
                  @httpServer->setResponseStatus( requestID, 404 )
               end
            end
         end else
            @httpServer->setResponseStatus( requestID, 404 )
         end
         
      end
   end else
      @httpServer->setResponseStatus( requestID, 404 )
   end 

return TRUE$

///////////////////////////////////////////////////////////////////////////////
// resolveFileNameForDPI subroutine
//
// Finds the best fitting image file for the DPI requested fro the list of 
// files defined in the repository:
//
//    <0,1>  96    (100%)
//    <0,2>  120   (125%)
//    <0,3>  144   (150%)
//    <0,4>  168   (175%)
//    <0,5>  192   (200%)   <-- After this point we step up in 50% increments
//    <0,6>  240   (250%)
//    <0,7>  288   (300%)
//    <0,8>  336   (350%)
//    <0,9>  384   (400%)
//    <0,10> 432   (450%)
//    <0,11> 480   (500%)
//
// If we can't find an exact match then we go for the next highest we can find
//
// ----------------------------------------------------------------------------
// [i] dpi       : DPI requested
// [i] fileNames : @vm'd list of file names to search
// [o] fileName  : Resolved file name
// ----------------------------------------------------------------------------
resolveFileNameForDPI:

   fileName = ""
   dpiTest  = BASE_DPI$
   
   eofNames = bLen( fileNames )
   pos      = 1
   loop
      fileName_ = fileNames[pos,@vm,TRUE$]; pos = bCol2()+1
      if bLen( fileName_ ) then
         transfer fileName_ to fileName
         begin case
            case ( dpiTest == dpi )
               return
            case ( dpiTest > dpi )
               return
            case OTHERWISE$
               null
         end case
      end
   until ( pos > eofNames )
      // After 200% DPI we go up in 50% steps, otherwise we go up in 25% steps
      if ( dpiTest >= 192 ) then
         dpiTest += 48
      end else
         dpiTest += 24
      end
   repeat

return

///////////////////////////////////////////////////////////////////////////////

setHTTPError:
   
   errInfo = rti_ErrorText( "SP", errInfo )
   @httpServer->setResponseContent( requestID, errInfo )
   @httpServer->setResponseHeader( requestID, "Content-Type", "text/plain" )
   @httpServer->setResponseStatus( requestID, 500 )

return

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


