Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Detecting file types
Message
De
16/10/2012 20:02:19
 
 
À
16/10/2012 19:09:01
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01555137
Message ID:
01555140
Vues:
42
The AVI support seems to work. As for the TXT file type, this cannot be done at the signature level. So, we can only rely on the extension.

Here is the class:
Namespace Framework

    Public Class FileType

        Public cExtension As String = ""
        Public cFile As String = ""
        Public cMessage As String = ""
        Public nNoFileType As Integer = 0
        Private oApp As Framework.App = Nothing
        Private oProcess As Framework.LXProcess = Nothing

        ' This is when we access the class in a desktop mode
        Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
        End Sub

        ' This is when we access the class in a Web or Web Service mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
        End Sub

        ' Return the file type
        ' expC1 File name with the full path
        Public Function FileType() As Boolean
            Dim lcDirectory As String = ""
            Dim lcExtension As String = ""
            Dim lcFile As String = ""
            Dim lcString As String = ""
            Dim lcValue As String = ""
            Dim lnLocation As Integer = 0
            Dim loDirectoryFunction As Framework.DirectoryFunction = Nothing
            Dim loFileDirectory As Framework.FileDirectory = Nothing
            Dim loFileFunction As Framework.FileFunction = Nothing
            Dim loXml As Framework.XML = Nothing
            Dim loZip As Framework.Zip = Nothing

            ' Get the proper definition as per the current scope
            If oProcess Is Nothing Then
                loDirectoryFunction = New Framework.DirectoryFunction(oApp)
                loFileDirectory = New Framework.FileDirectory(oApp)
                loFileFunction = New Framework.FileFunction(oApp)
                loXml = New Framework.XML(oApp)
                loZip = New Framework.Zip(oApp)
            Else
                loDirectoryFunction = New Framework.DirectoryFunction(oProcess)
                loFileDirectory = New Framework.FileDirectory(oProcess)
                loFileFunction = New Framework.FileFunction(oProcess)
                loXml = New Framework.XML(oProcess)
                loZip = New Framework.Zip(oProcess)
            End If

            ' Reset everything
            cExtension = ""
            cMessage = ""
            nNoFileType = 0

            ' Initialization
            lcFile = Trim(cFile)

            ' Read it as a binary
            lcString = oApp.FileToStrBinary(lcFile)

            ' If we have less than four characters
            If lcString.Length < 4 Then
                Return False
            End If

            ' If this is a JPG
            If Left(lcString, 3) = Chr(255) + Chr(216) + Chr(255) Then
                cExtension = "JPG"
                nNoFileType = 6
                Return True
            End If

            ' If this is a GIF
            If Left(lcString, 3) = "GIF" Then
                cExtension = "GIF"
                nNoFileType = 7
                Return True
            End If

            ' If this is a EMF
            If Mid(lcString, 42, 3) = "EMF" Then
                cExtension = "EMF"
                nNoFileType = 13
                Return True
            End If

            ' If this is a WMF
            If Left(lcString, 4) = Chr(215) + Chr(205) + Chr(198) + Chr(154) Then
                cExtension = "WMF"
                nNoFileType = 14
                Return True
            End If

            ' If this is a TIF
            If Left(lcString, 4) = Chr(77) + Chr(77) + Chr(0) + Chr(42) Then
                cExtension = "TIF"
                nNoFileType = 15
                Return True
            End If

            ' If this is a PNG
            If Left(lcString, 4) = Chr(137) + "PNG" Then
                cExtension = "PNG"
                nNoFileType = 12
                Return True
            End If

            ' If this is a BMP
            If Left(lcString, 2) = "BM" Then
                cExtension = "BMP"
                nNoFileType = 16
                Return True
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "CWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                cExtension = "SWF"
                nNoFileType = 8
                Return True
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "FWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                cExtension = "SWF"
                nNoFileType = 8
                Return True
            End If

            ' If this is a XML
            If UCase(Left(lcString, 6)) = "<?XML " Then
                cExtension = "XML"
                nNoFileType = 10
                Return True
            End If

            ' If this is a DOC
            If Left(lcString, 8) = Chr(208) + Chr(207) + Chr(17) + Chr(224) + Chr(161) + Chr(177) + Chr(26) + Chr(225) Then
                cExtension = "DOC"
                nNoFileType = 1
                Return True
            End If

            ' If this is a PDF
            If UCase(Left(lcString, 5)) = "%PDF-" Then
                cExtension = "PDF"
                nNoFileType = 2
                Return True
            End If

            ' If this is a Excel
            If Left(lcString, 8) = Chr(208) + Chr(207) + Chr(17) + Chr(224) + Chr(161) + Chr(177) + Chr(26) + Chr(225) Then
                cExtension = "XLS"
                nNoFileType = 3
                Return True
            End If

            ' If this is a Microsoft Office 2007 document
            If Left(lcString, 14) = Chr(80) + Chr(75) + Chr(3) + Chr(4) + Chr(20) + Chr(0) + Chr(6) + Chr(0) + _
             Chr(8) + Chr(0) + Chr(0) + Chr(0) + Chr(33) + Chr(0) Then

                ' If we have a temporary directory
                If oApp.cTempDir.Length > 0 Then

                    ' Get a temporary file name
                    lcDirectory = oApp.GenerateFileName()

                    ' Get the full long directory name
                    lcDirectory = oApp.cTempDir + lcDirectory

                    ' If we can create a temporary directory under Temp
                    If oApp.CreateDirectory(lcDirectory) Then

                        ' Zip class needs an extension, otherwise, it will try to zip a file name ending with .zip
                        loFileFunction.cSource = lcFile
                        loFileFunction.cDestination = lcFile + ".zip"
                        loFileFunction.lLogError = False
                        If loFileFunction.CopyFile() Then

                            ' If we can unzip the file
                            loZip.cFile = lcFile + ".zip"
                            loZip.cDirectory = lcDirectory
                            loZip.lUseCommandPrompt = True
                            If loZip.Unzip() Then

                                ' Delete the temporary file
                                If Not loFileFunction.DeleteFile(lcFile + ".zip", False) Then
                                End If

                                ' If we can load the Xml
                                If loXml.LoadXmlFromFile(lcDirectory + "\[Content_Types].xml") Then

                                    ' Delete the temporary files which were created in the temporary directory
                                    loFileDirectory.cDirectory = lcDirectory
                                    loFileDirectory.GetFile()
                                    If Not loFileDirectory.DeleteFiles() Then
                                    End If

                                    ' Delete the temporary directory
                                    If Not loDirectoryFunction.DeleteDirectory(lcDirectory) Then
                                    End If

                                    ' If we can add the namespace
                                    If loXml.AddNamespace("http://schemas.openxmlformats.org/package/2006/content-types", "ns") Then

                                        ' If the attribute exists
                                        If loXml.IsXMLNodeAttribute("//ns:Types/ns:Override[1]", "PartName") Then

                                            ' Get the value of the attribute
                                            If loXml.GetXMLNodeAttributeValue("//ns:Types/ns:Override[1]", "PartName") Then
                                                lcValue = loXml.cNodeAttribute

                                                ' Based on the type
                                                Select Case lcValue

                                                    ' Word
                                                    Case "/word/document.xml"
                                                        cExtension = "DOCX"
                                                        nNoFileType = 17
                                                        Return True

                                                        ' Excel
                                                    Case "/xl/workbook.xml"
                                                        cExtension = "XLSX"
                                                        nNoFileType = 18
                                                        Return True

                                                        ' PowerPoint
                                                    Case "/ppt/presentation.xml"
                                                        cExtension = "PPTX"
                                                        nNoFileType = 19
                                                        Return True

                                                    Case Else
                                                        Return False

                                                End Select

                                            Else
                                                Return False
                                            End If

                                        Else
                                            Return False
                                        End If

                                    Else
                                        Return False
                                    End If

                                Else
                                    Return False
                                End If

                            Else
                                Return False
                            End If

                        Else
                            Return False
                        End If

                    Else
                        Return False
                    End If

                Else
                    Return False
                End If

                Return False
            End If

            ' If this is a ZIP
            If Left(lcString, 4) = "PK" + Chr(3) + Chr(4) Then
                cExtension = "ZIP"
                nNoFileType = 9
                Return True
            End If

            ' If this is a Windows AVI Video
            If Left(lcString, 4) = Chr(82) + Chr(73) + Chr(70) + Chr(70) And _
             Mid(lcString, 9, 8) = Chr(65) + Chr(86) + Chr(73) + Chr(32) + Chr(76) + Chr(73) + Chr(83) + Chr(84) Then
                cExtension = "AVI"
                nNoFileType = 4
                Return True
            End If

            ' If this is a Powerpoint file
            If Mid(lcString, 513, 4) = Chr(160) + Chr(70) + Chr(29) + Chr(240) Then
                cExtension = "PPT"
                nNoFileType = 5
                Return True
            End If

            ' If we have not found any type with the binary approach, we will try to rely on the extension
            lnLocation = oApp.RAt(".", lcFile)

            ' If we have found the extension
            If lnLocation > 0 Then

                ' Get the extension
                lcExtension = Mid(lcFile, lnLocation + 1)

                ' Make it uppercase
                lcExtension = UCase(lcExtension)

                ' If this is a TXT file
                If lcExtension = "TXT" Then
                    cExtension = "TXT"
                    nNoFileType = 11
                    Return True
                End If

            End If

            Return True
        End Function

    End Class

End Namespace
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform