예제 #1
0
        public static bool AreFilesEqual(string filePath1, string filePath2)
        {
            if (filePath1 == null || filePath2 == null)
            {
                return(false);
            }

            string fileType1 = Path.GetExtension(filePath1).ToLower();
            string fileType2 = Path.GetExtension(filePath2).ToLower();

            if (fileType1 != fileType2)
            {
                return(false);
            }

            if (fileType1 == ".odt")
            {
                var extractPath1 = new TmpPath().Path;
                var extractPath2 = new TmpPath().Path;
                ZipFile.ExtractToDirectory(filePath1, extractPath1);
                ZipFile.ExtractToDirectory(filePath2, extractPath2);
                File.Delete(Path.Combine(extractPath1, "meta.xml"));
                File.Delete(Path.Combine(extractPath2, "meta.xml"));
                return(AreDirectoriesEqual(extractPath1, extractPath2));
            }
            else
            {
                if (filePath1.Length != filePath2.Length)
                {
                    return(false);
                }
                var fs1    = new FileStream(filePath1, FileMode.Open);
                var fs2    = new FileStream(filePath2, FileMode.Open);
                var bytes1 = new byte[2048];
                var bytes2 = new byte[2048];
                for (int offset = 0; offset < fs1.Length; offset += 2048)
                {
                    int readCount1 = fs1.Read(bytes1, 0, (int)Math.Min(2048, fs1.Length - offset));
                    int readCount2 = fs2.Read(bytes2, 0, (int)Math.Min(2048, fs1.Length - offset));
                    for (int i = 0; i < bytes1.Length; ++i)
                    {
                        if (bytes1[i] != bytes2[i])
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
        public async Task <JsonResult> ApplyNow()
        {
            try
            {
                var appUser = await userManager.GetUserAsync(HttpContext.User);

                var document        = dbContext.GetDocument(appUser, "documentName");
                var documentEmail   = (DocumentEmail)dbContext.GetDbObject("DocumentEmail", appUser, document);
                var userValues      = (UserValues)dbContext.GetDbObject("UserValues", appUser, document);
                var customVariables = (IEnumerable <CustomVariable>)dbContext.GetDbObject("CustomVariables", appUser, document);
                var documentFiles   = (IEnumerable <DocumentFile>)dbContext.GetDbObject("DocumentFiles", appUser, document);
                var sentApplication =
                    new SentApplication
                {
                    Document   = document,
                    UserValues = userValues,
                    SentDate   = DateTime.Now
                };
                dbContext.Add(sentApplication);

                var pdfFilePaths  = new List <string>();
                var dict          = getVariableDict(document.Employer, userValues, documentEmail, customVariables, document.JobName);
                var tmpDirectory  = new TmpPath().Path;
                var userDirectory = new UserPath(appUser.Id).UserDirectory;
                foreach (var documentFile in documentFiles)
                {
                    var currentFile        = Path.Combine(userDirectory, documentFile.Path);
                    var extension          = Path.GetExtension(currentFile).ToLower();
                    var convertedToPdfPath = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_{Guid.NewGuid().ToString()}.pdf");
                    if (extension == ".odt")
                    {
                        var tmpPath2 = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_replaced_{Guid.NewGuid().ToString()}{extension}");

                        FileConverter.ReplaceInOdt(currentFile, tmpPath2, dict);
                        currentFile = tmpPath2;
                    }
                    if (FileConverter.ConvertTo(currentFile, convertedToPdfPath))
                    {
                        pdfFilePaths.Add(convertedToPdfPath);
                    }
                    else
                    {
                        throw new Exception("Failed to convert file " + documentFile.Name);
                    }
                }

                var mergedPath = Path.Combine(new TmpPath().Path, "merged_" + Guid.NewGuid().ToString() + ".pdf");
                if (!FileConverter.MergePdfs(pdfFilePaths, mergedPath))
                {
                    throw new Exception("Failed to merge pdfs.");
                }

                var newEmployer = new Employer();
                dbContext.Add(newEmployer);
                var documentCopy = new Document(document)
                {
                    Employer = newEmployer
                };
                dbContext.Add(documentCopy);
                dbContext.Add(new UserValues(userValues)
                {
                    AppUser = appUser
                });
                dbContext.Add(new DocumentEmail(documentEmail)
                {
                    Document = documentCopy
                });

                foreach (var customVariable in customVariables)
                {
                    dbContext.Add(new CustomVariable(customVariable)
                    {
                        Document = documentCopy
                    });
                }
                foreach (var documentFile in documentFiles)
                {
                    dbContext.Add(new DocumentFile(documentFile)
                    {
                        Document = documentCopy
                    });
                }
                var attachments = new List <EmailAttachment>();
                if (pdfFilePaths.Count >= 1)
                {
                    var attachmentName = FileConverter.ReplaceInString(documentEmail.AttachmentName, dict);
                    if (attachmentName.Length >= 1 && !attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))
                    {
                        attachmentName = attachmentName + ".pdf";
                    }
                    else if (!(attachmentName.Length >= 4 && attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase)))
                    {
                        attachmentName = "Bewerbung.pdf";
                    }
                    attachments.Add(new EmailAttachment {
                        Path = mergedPath, Name = attachmentName
                    });
                }
                HelperFunctions.SendEmail(
                    new EmailData
                {
                    Attachments = attachments,
                    Body        = FileConverter.ReplaceInString(documentEmail.Body, dict),
                    Subject     = FileConverter.ReplaceInString(documentEmail.Subject, dict),
                    ToEmail     = document.Employer.Email,
                    FromEmail   = userValues.Email,
                    FromName    =
                        (userValues.Degree == "" ? "" : userValues.Degree + " ") +
                        userValues.FirstName + " " + userValues.LastName
                }
                    );
                dbContext.SaveChanges();
                return(Json(new { status = 0 }));
            }
            catch (Exception err)
            {
                log.Error("", err);
                return(Json(new { status = 1 }));
            }
        }
예제 #3
0
        public static UploadedFileData GetUploadedFileData
            (string userId,
            string filePath,
            IEnumerable <string> dbFileNames,
            IEnumerable <string> diskFileNames)
        {
            try
            {
                string userDirectory = new UserPath(userId).UserDirectory;
                var    fileName      = Path.GetFileName(filePath);
                var    extension     = Path.GetExtension(filePath).ToLower();
                switch (extension)
                {
                case ".pdf":
                {
                    var unusedDiskFileName = FindUnusedFileName(fileName, diskFileNames);
                    var savePath           = Path.Combine(userDirectory, unusedDiskFileName);
                    var unusedDbFileName   = FindUnusedFileName(fileName, dbFileNames);
                    return
                        (new UploadedFileData
                        {
                            OriginalFilePath = filePath,
                            SavedFileName = unusedDiskFileName,
                            DisplayedFileName = unusedDbFileName,
                            ConvertAndSave =
                                () =>
                            {
                                if (MergePdfs(new[] { filePath }, Path.Combine(new TmpPath().Path, "mypdf.pdf")))
                                {
                                    File.Copy(filePath, savePath, true);
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        });
                }

                case ".odt":
                {
                    var unusedDiskFileName = FindUnusedFileName(fileName, diskFileNames);
                    var savePath           = Path.Combine(userDirectory, unusedDiskFileName);
                    var unusedDbFileName   = FindUnusedFileName(fileName, dbFileNames);
                    return
                        (new UploadedFileData
                        {
                            OriginalFilePath = filePath,
                            SavedFileName = unusedDiskFileName,
                            DisplayedFileName = unusedDbFileName,
                            ConvertAndSave =
                                () =>
                            {
                                var tmpPath = new TmpPath();
                                var path = Path.Combine(tmpPath.Path, "mypdf.pdf");
                                if (ConvertTo(filePath, path))
                                {
                                    File.Copy(filePath, savePath, true);
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        });
                }

                case ".doc":
                case ".docx":
                {
                    var unusedDiskFileName = FindUnusedFileName(Path.ChangeExtension(fileName, ".odt"), diskFileNames);
                    var savePath           = Path.Combine(userDirectory, unusedDiskFileName);
                    var unusedDbFileName   = FindUnusedFileName(Path.ChangeExtension(fileName, ".odt"), dbFileNames);
                    return
                        (new UploadedFileData
                        {
                            OriginalFilePath = filePath,
                            SavedFileName = unusedDiskFileName,
                            DisplayedFileName = unusedDbFileName,
                            ConvertAndSave =
                                () =>
                            {
                                var tmpPath = new TmpPath();
                                var outputPath = Path.Combine(tmpPath.Path, "myodt.odt");
                                if (ConvertTo(filePath, outputPath))
                                {
                                    File.Copy(outputPath, savePath, true);
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        });
                }

                default:
                    throw new Exception("Filetype not found " + extension);
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }