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