Beispiel #1
0
        private void backgroundWorkerCopy_DoWork(object sender, DoWorkEventArgs e)
        {
            int counter = 0;

            backgroundWorkerCopy.ReportProgress(0);
            amountOfBytesCopied = 0;
            byte[] buffer = new byte[bufferLenght];
            if (destinationPath != null && destinationSystemUtility != null && !destinationSystemUtility.DirectoryExists(destinationPath))
            {
                destinationSystemUtility.CreateDirectory(destinationPath);
            }
            startDateTime = DateTime.Now;
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                if (backgroundWorkerCopy.CancellationPending)
                {
                    e.Cancel = true;
                    e.Result = counter;
                    return;
                }
                string destinationFile = null;
                if (destinationPath != null)
                {
                    if (useDateTimeForFileNames == false)
                    {
                        destinationFile = System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(sourceFiles[i]));
                    }
                    else
                    {
                        destinationFile = System.IO.Path.Combine(destinationPath, GetNewFileName()) + System.IO.Path.GetExtension(sourceFiles[i]);
                        int index = 1;
                        while (System.IO.File.Exists(destinationFile))
                        {
                            destinationFile = System.IO.Path.Combine(destinationPath, GetNewFileName() + " (" + (++index).ToString() + ")") + System.IO.Path.GetExtension(sourceFiles[i]);
                        }
                    }
                }
                else if (destinationFiles != null)
                {
                    destinationFile = destinationFiles[i];
                }

                if (saveAction != null)
                {
                    try
                    {
                        saveAction(sourceFiles[i]);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("خطا در ذخیره فایل" + "\r\n" + sourceFiles[i] + "\r\n" + "\r\n" + ex.Message);
                    }
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(sourceFiles[i]);
                    amountOfBytesCopied += fileInfo.Length;
                    backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                }
                else
                {
                    System.IO.FileStream sourceStream      = null;
                    System.IO.FileStream destinationStream = null;
                    try
                    {
                        sourceStream      = System.IO.File.OpenRead(sourceFiles[i]);
                        destinationStream = destinationSystemUtility.CreateFile(destinationFile);
                        int readCount;
                        do
                        {
                            if (backgroundWorkerCopy.CancellationPending)
                            {
                                e.Cancel = true;
                                if (sourceStream != null)
                                {
                                    sourceStream.Close();
                                    sourceStream.Dispose();
                                }
                                if (destinationStream != null)
                                {
                                    destinationStream.Close();
                                    destinationStream.Dispose();
                                }
                                try
                                {
                                    destinationSystemUtility.DeleteFile(destinationFile);
                                }
                                catch { }
                                e.Result = counter;
                                return;
                            }
                            readCount = sourceStream.Read(buffer, 0, buffer.Length);
                            destinationStream.Write(buffer, 0, readCount);

                            amountOfBytesCopied += readCount;
                            backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                        }while (readCount > 0);
                    }
                    finally
                    {
                        if (sourceStream != null)
                        {
                            sourceStream.Close();
                            sourceStream.Dispose();
                        }
                        if (destinationStream != null)
                        {
                            destinationStream.Close();
                            destinationStream.Dispose();
                        }
                    }
                }
                counter++;
            }
            backgroundWorkerCopy.ReportProgress(100);
            e.Result = counter;
        }
        internal static Document AddDocument(string personnelNumber, string file, int?parentDocumentID, bool attachToDossier, Njit.Program.ComponentOne.Enums.SaveFormats imageFormat, Njit.Program.ComponentOne.Enums.CompressionTypes imageCompression, ArchiveTab archivetab)
        {
            System.IO.FileInfo fileInfo = new FileInfo(file);
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize > 0)
            {
                if (fileInfo.Length / 1024 > Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize)
                {
                    throw new Exception(string.Format("حجم فایل نباید از {0} کیلوبایت بیشتر باشد\r\nحجم این فایل {1} کیلوبایت است", Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize, fileInfo.Length / 1024));
                }
            }
            long   number        = Controller.Archive.DocumentController.GetNewDocumentNumber(personnelNumber);
            string fileExtension = Path.GetExtension(file).ToLower();

            Model.Archive.Dossier dossier = Controller.Archive.DossierController.Select(personnelNumber);
            if (imageExtensions.Contains(fileExtension))
            {
                switch (imageFormat)
                {
                case Njit.Program.ComponentOne.Enums.SaveFormats.None:
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.OnePdf:
                    if (fileExtension != ".pdf")
                    {
                        fileExtension = ".pdf";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Pdf.CreatePDF.FromImages(new Image[] { image }, newfile, 0, Njit.Pdf.CreatePDF.PageOrientation.عمودی, Njit.Pdf.CreatePDF.PageSize.OriginalSize);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.Pdf:
                    if (fileExtension != ".pdf")
                    {
                        fileExtension = ".pdf";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Pdf.CreatePDF.FromImages(new Image[] { image }, newfile, 0, Njit.Pdf.CreatePDF.PageOrientation.عمودی, Njit.Pdf.CreatePDF.PageSize.OriginalSize);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.OneMultiTiff:
                    if (fileExtension != ".tiff" && fileExtension != ".tif")
                    {
                        fileExtension = ".tiff";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Tiff, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.Tiff:
                    if (fileExtension != ".tiff" && fileExtension != ".tif")
                    {
                        fileExtension = ".tiff";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Tiff, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.JPEG:
                    if (fileExtension != ".jpg" && fileExtension != ".jpeg")
                    {
                        fileExtension = ".jpg";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Jpeg, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.PNG:
                    if (fileExtension != ".png")
                    {
                        fileExtension = ".png";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Png, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.BMP:
                    if (fileExtension != ".bmp")
                    {
                        fileExtension = ".bmp";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Bmp, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                default:
                    throw new Exception();
                }
            }
            string fileNameToSaveInDatabase = Njit.Common.PublicMethods.ReplaceInvalidPathAndFileNameChars(personnelNumber, "-") + "-" + number.ToString() + fileExtension;
            string destinationFile          = null;

            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase == false)
            {
                destinationFile = Path.Combine(dossier.FilesPathOrDatabaseName, fileNameToSaveInDatabase);
                Njit.Common.SystemUtility sysUtility = Njit.Program.Options.GetSystemUtility(DataAccess.ArchiveDataAccess.GetNewInstance().Connection, Setting.Program.ThisProgram.NetworkName, Setting.Program.ThisProgram.NetworkPort);
                int i = 1;
                while (sysUtility.FileExists(destinationFile))
                {
                    fileNameToSaveInDatabase = Njit.Common.PublicMethods.ReplaceInvalidPathAndFileNameChars(personnelNumber, "-") + "-" + number.ToString() + " (" + i.ToString() + ")" + fileExtension;
                    destinationFile          = Path.Combine(dossier.FilesPathOrDatabaseName, fileNameToSaveInDatabase);
                    i++;
                }
            }

            Model.Archive.Document document = Model.Archive.Document.GetNewInstance(personnelNumber, number.ToString(), fileNameToSaveInDatabase, archivetab.ID, attachToDossier, parentDocumentID, Controller.Archive.DocumentController.GetMaxDocumentIndex(personnelNumber) + 1);

            var archiveDc = Model.Archive.ArchiveDataClassesDataContext.GetNewInstance();

            Model.ArchiveDocument.DocumentDataClassesDataContext documentDc = null;
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase)
            {
                documentDc = Model.ArchiveDocument.DocumentDataClassesDataContext.GetNewInstance(Setting.Archive.ThisProgram.LoadedArchiveSettings.DocumentsPathOrDatabaseName);
            }

            archiveDc.Connection.Open();
            archiveDc.Transaction = archiveDc.Connection.BeginTransaction();
            if (documentDc != null)
            {
                documentDc.Connection.Open();
                documentDc.Transaction = documentDc.Connection.BeginTransaction();
            }
            try
            {
                archiveDc.Documents.InsertOnSubmit(document);
                archiveDc.SubmitChanges();
                archiveDc.Transaction.Commit();
                if (documentDc != null)
                {
                    Model.ArchiveDocument.Document doc = Model.ArchiveDocument.Document.GetNewInstance(document.ID, new System.Data.Linq.Binary(System.IO.File.ReadAllBytes(file)));
                    documentDc.Documents.InsertOnSubmit(doc);
                    documentDc.SubmitChanges();
                    documentDc.Transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                archiveDc.Transaction.Rollback();
                if (documentDc != null)
                {
                    documentDc.Transaction.Rollback();
                }
                throw new Exception("خطا در ثبت اطلاعات" + "\r\n\r\n" + ex.Message);
            }
            finally
            {
                archiveDc.Connection.Close();
                if (documentDc != null)
                {
                    documentDc.Connection.Close();
                }
            }
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase == false)
            {
                FileStream serverFileStream = null;
                FileStream clientFileStream = null;
                try
                {
                    Njit.Common.SystemUtility sysUtility = Njit.Program.Options.GetSystemUtility(DataAccess.ArchiveDataAccess.GetNewInstance().Connection, Setting.Program.ThisProgram.NetworkName, Setting.Program.ThisProgram.NetworkPort);
                    if (!sysUtility.DirectoryExists(dossier.FilesPathOrDatabaseName))
                    {
                        sysUtility.CreateDirectory(dossier.FilesPathOrDatabaseName);
                    }
                    serverFileStream = sysUtility.CreateFile(destinationFile);
                    clientFileStream = File.OpenRead(file);
                    byte[] buffre    = new byte[1 * 1024 * 1024];
                    int    readCount = 0;
                    do
                    {
                        readCount = clientFileStream.Read(buffre, 0, buffre.Length);
                        serverFileStream.Write(buffre, 0, readCount);
                    }while (readCount > 0);
                    clientFileStream.Close();
                    serverFileStream.Close();
                    clientFileStream.Dispose();
                    serverFileStream.Dispose();
                }
                catch (Exception ex)
                {
                    if (clientFileStream != null)
                    {
                        clientFileStream.Dispose();
                    }
                    if (serverFileStream != null)
                    {
                        serverFileStream.Dispose();
                    }
                    throw new Exception("خطا در ذخیره فایل" + "\r\n" + file + "\r\n\r\n" + ex.Message);
                }
            }
            return(document);
        }
Beispiel #3
0
        private void backgroundWorkerCopy_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorkerCopy.ReportProgress(0);
            amountOfBytesCopied = 0;
            byte[] buffer = new byte[bufferLenght];
            if (destinationPath != null && !destinationSystemUtility.DirectoryExists(destinationPath))
            {
                destinationSystemUtility.CreateDirectory(destinationPath);
            }
            startDateTime = DateTime.Now;
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                if (backgroundWorkerCopy.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                string destinationFile;
                if (destinationPath != null)
                {
                    destinationFile = System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(sourceFiles[i]));
                }
                else
                {
                    destinationFile = destinationFiles[i];
                }
                if (this.overWrite || (!this.overWrite && !destinationSystemUtility.FileExists(destinationFile)))
                {
                    System.IO.FileStream sourceStream      = null;
                    System.IO.FileStream destinationStream = null;
                    try
                    {
                        sourceStream      = sourceSystemUtility.OpenFile(sourceFiles[i]);
                        destinationStream = destinationSystemUtility.CreateFile(destinationFile);
                        int readCount;
                        do
                        {
                            if (backgroundWorkerCopy.CancellationPending)
                            {
                                e.Cancel = true;
                                if (sourceStream != null)
                                {
                                    sourceStream.Close();
                                    sourceStream.Dispose();
                                }
                                if (destinationStream != null)
                                {
                                    destinationStream.Close();
                                    destinationStream.Dispose();
                                }
                                try
                                {
                                    destinationSystemUtility.DeleteFile(destinationFile);
                                }
                                catch { }
                                return;
                            }
                            readCount = sourceStream.Read(buffer, 0, buffer.Length);
                            destinationStream.Write(buffer, 0, readCount);

                            amountOfBytesCopied += readCount;
                            backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                        }while (readCount > 0);
                    }
                    finally
                    {
                        if (sourceStream != null)
                        {
                            sourceStream.Close();
                            sourceStream.Dispose();
                        }
                        if (destinationStream != null)
                        {
                            destinationStream.Close();
                            destinationStream.Dispose();
                        }
                    }
                }
            }
            backgroundWorkerCopy.ReportProgress(100);
        }