<%
' these are the error constants returned by the component
Const MBE_INVALID_INPUT_FILE = 1
Const MBE_NO_MACBINARY    = 2
Const MBE_MAPPING_FAILED = 3
Const MBE_VIEW_MAP_FAILED = 4
Const MBE_NO_DATA_FORK = 5
Const MBE_DATA_FORK_SIZE = 6
Const MBE_HEADER_ERROR = 7
Const MBE_DEST_FILE_CREATE = 8
Const MBE_DEST_WRITE = 9

' next line is the file to be converted, second line does the conversion
' you can put the code of this page in a single script file and remove
' only these two lines to make it suitable for INCLUDE
FileName = Server.MapPath("mactestfile.jpg")
ConvertMacFileOnMacBrowser(FileName)

' Create a random file name
Function RandomFileName()
 Dim fs
 Set fs = Server.CreateObject("Scripting.FileSystemObject")
 RandomFileName = CStr(fs.GetTempName)
 Set fs = Nothing
End Function

' Copies a file by overwriting the existing one
Function CopyFileWithOverWrite(source, destination)  
 Dim Fs
 Set FS = CreateObject("Scripting.FileSystemObject")
 On Error Resume Next
 FS.CopyFile source, destination, True
 If Err.Number <> 0 Then
  CopyFileWithOverWrite = False
 Else
  CopyFileWithOverWrite = True
 End If 
 Set Fs = Nothing
End Function

' delete a file
Function DeleteFile(file)
 Dim Fs
 Set FS = CreateObject("Scripting.FileSystemObject")
 FS.DeleteFile file
 Set FS = Nothing
 If Err.Number <> 0 Then
  DeleteFile = False
 Else
  DeleteFile = True
 End If
End Function

' returns: 0 ... All OK
' other values = Error Code of Softwing.MacBinary object
' File: Filename with absolute Path , D:\web\datei.gif
Function ConvertMacFile(File)
 Dim strCurrentTempFile
 Dim bRetVal
 Dim ErrNum
 Dim strPos
 strPos = InstrRev(File,"\")
 If strPos <> 0 Then
  strDir = Mid(File,1,InstrRev(File,"\")-1)
 Else
  strDir = Server.MapPath("/")
 End If
 strCurrentTempFile = strDir & "\" & RandomFileName
 
 ErrNum = Err.Number
 On Error Resume Next
 
 Set MacBin = CreateObject("Softwing.MacBinary") 
 bRetVal = MacBin.ConvertFromMacBinary(File,strCurrentTempFile)
 Set MacBin = Nothing
 ConvertMacFile = Err.Number - vbObjectError
 
 If Err.Number = 0 Then
  ConvertMacFile = 0
  CopyFileWithOverWrite strCurrentTempFile,File
  DeleteFile strCurrentTempFile
 End If
 Err.Number = ErrNum
End Function

Function ConvertMacFileOnMacBrowser(File)  
 Dim Browser
 Dim RetVal
 Browser = Request.ServerVariables("HTTP_USER_AGENT")
 
 ' remove the following line if you want to convert always...
 If Instr(1,Browser,"Mac") > 0 AND Instr(1,Browser,"MSIE") > 0 Then
  RetVal = ConvertMacFile(File)
  strError = ""
  Select Case RetVal
   Case MBE_INVALID_INPUT_FILE
    ' File could not be found
    strError = "File " & File & " not found."
   Case MBE_NO_MACBINARY
    ' File size is less than 128 Bytes and therefore
    ' no MacBinary File
   Case MBE_MAPPING_FAILED
    ' Internal Error in the Component
    ' Maybe you installed it on another
    ' Operating System than Windows NT
    strError = "Internal Component Error."
   Case MBE_VIEW_MAP_FAILED
    ' Internal Error in the Component
    ' Maybe you installed it on another
    ' Operating System than Windows NT
    strError = "Internal Component Error."
   Case MBE_NO_DATA_FORK
    ' Data Fork is indicated as size 0
   Case MBE_DATA_FORK_SIZE
    ' Data Fork size is indicated to be greater than
    ' File size
   Case MBE_HEADER_ERROR
    ' Error in the MacBinary file header
    ' Bytes 0 74 and 82 are not 0 - This is not a MacBinary I, II or III file
   Case MBE_DEST_FILE_CREATE
    ' Temporary destination file could not be created
    strError "Temporary destination file could not be created."
   Case MBE_DEST_WRITE
    ' Error while writing the temporary destination file
    strError "Error occured during writing the temporary destination file."
   Case Else
    ' Unknown Error
    strError = "Unknown Error occured."
  End Select
  If strError <> "" Then
   Response.Write "<br>" & strError & "<BR>"
   Response.End
  End If
 End If
End Function
%>