Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesStrange behavior of FileManager.Instance.AddFile/RenameFileStrange behavior of FileManager.Instance.AddFile/RenameFile
Previous
 
Next
New Post
6/13/2018 1:35 PM
 

Hello

I built a module that uploads file, optimize it (if it is a pdf) and then, if the new file is smaller than the original one, the previous file is replaced by the new, smaller, one.

All works fine in development environment.

In production environment, when I replace the file I got “The underlying system threw an exception. The file has not been added.”. I googled and someone solved checking file permission. It is not my case: I can write files in any folders, the issue just occurs if I replace old file with the new one ** in the same call **.

If I upload file and then, in a second call I only optimize pdf, no issue occurs at all.

I got the same error if I rename the file and, in the same call, I compress it.  I suspect that filemanager.instance.rename or filemanager.insance.add does not completely release the file after its call, but I cannot explain why it happens only in production environment.

Below you can find my pdf compression function, can anyone take a look and suggest me a solution?

Thanks


        Public Shared Function CompressPDF2(ByVal pintDocumentID As Integer) As Boolean


            Dim lExistingFile As FileInfo
            lExistingFile = FileManager.Instance.GetFile(pintDocumentID)

            If IsNothing(lExistingFile) Then
                Return False
            End If

            'Comprimo solo i file pdf
            If lExistingFile.Extension.ToLower <> "pdf" Then
                Return False
            End If

            Dim lstrTempDirectory As String = PortalController.GetCurrentPortalSettings().HomeDirectoryMapPath + "temppdf"


            'Creating temporary folder if it does not exists
            If Not System.IO.Directory.Exists(lstrTempDirectory) Then
                Dim lTempDirectoryInfo As System.IO.DirectoryInfo
                lTempDirectoryInfo = System.IO.Directory.CreateDirectory(PortalController.GetCurrentPortalSettings().HomeDirectoryMapPath + "TempPDF")
                If IsNothing(lTempDirectoryInfo) Then
                    Return False
                End If
            End If

            Dim lfolder As DotNetNuke.Services.FileSystem.FolderInfo
            lfolder = FolderManager.Instance.GetFolder(lExistingFile.FolderId)


            If IsNothing(lfolder) Then
                Return False
            End If

            Dim lstrFileNameWithoutExtension As String
            Dim lstrFileNamePDFTemp As String
            lstrFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(lExistingFile.FileName)

            lstrFileNamePDFTemp = lstrFileNameWithoutExtension

            'Got a temporary filename
            Dim lintI As Integer = 2
            While System.IO.File.Exists(lstrTempDirectory + "/" + lstrFileNamePDFTemp + ".pdf.resources")
                lstrFileNamePDFTemp = lstrFileNameWithoutExtension + "_" + lintI.ToString
                lintI += 1
            End While

            Dim reader As New iTextSharp.text.pdf.PdfReader(FileManager.Instance.GetFileContent(lExistingFile))
            Dim lInputStreamOriginal As System.IO.FileStream = New System.IO.FileStream(lstrTempDirectory + "/" + lstrFileNamePDFTemp + ".pdf.resources", System.IO.FileMode.Create)
            Dim stamper As New iTextSharp.text.pdf.PdfStamper(reader, lInputStreamOriginal, iTextSharp.text.pdf.PdfWriter.VERSION_1_5)

            stamper.FormFlattening = True
            stamper.SetFullCompression()
            stamper.Close()

            Try
                lInputStreamOriginal.Close()
                lInputStreamOriginal.Dispose()
            Catch ex As Exception

            End Try

            Try
                stamper.Dispose()
            Catch ex As Exception

            End Try

            Try
                reader.Close()
                reader.Dispose()
            Catch ex As Exception

            End Try

            Dim lstrFileNamePDF As String
            lstrFileNamePDF = Common.sharedfunctions.GetNonExistingsFilenameInAFolder(System.IO.Path.GetFileNameWithoutExtension(lExistingFile.FileName), ".pdf", lfolder)

            Dim fTemp As New System.IO.FileInfo(lstrTempDirectory + "/" + lstrFileNamePDFTemp + ".pdf.resources")

            'In case new file is maller, I replace the old file with it
            If fTemp.Length < lExistingFile.Size Then
                Dim lInputStreamPDF As System.IO.FileStream = System.IO.File.Open(lstrTempDirectory + "/" + lstrFileNamePDFTemp + ".pdf.resources", IO.FileMode.Open)

                FileManager.Instance.AddFile(lfolder, lExistingFile.FileName, lInputStreamPDF, True)

                Try
                    lInputStreamPDF.Close()
                    lInputStreamPDF.Dispose()
                Catch ex As Exception

                End Try
            End If


            'delete temporary file
            System.IO.File.Delete(lstrTempDirectory + "/" + lstrFileNamePDFTemp + ".pdf.resources")

            Return True

        End Function

 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesStrange behavior of FileManager.Instance.AddFile/RenameFileStrange behavior of FileManager.Instance.AddFile/RenameFile


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  

What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out