private Document SaveToImageFolder(string fileSourcePath, LetterMerge letterCur) { if (letterCur.ImageFolder == 0) //This shouldn't happen { return(new Document()); } string rawBase64 = ""; if (PrefC.AtoZfolderUsed == DataStorageType.InDatabase) { rawBase64 = Convert.ToBase64String(File.ReadAllBytes(fileSourcePath)); } Document docSave = new Document(); docSave.DocNum = Documents.Insert(docSave); docSave.ImgType = ImageType.Document; docSave.DateCreated = DateTime.Now; docSave.PatNum = PatCur.PatNum; docSave.DocCategory = letterCur.ImageFolder; docSave.Description = letterCur.Description + docSave.DocNum; //no extension. docSave.RawBase64 = rawBase64; //blank if using AtoZfolder docSave.FileName = ODFileUtils.CleanFileName(letterCur.Description) + GetFileExtensionForWordDoc(fileSourcePath); string fileDestPath = ImageStore.GetFilePath(docSave, ImageStore.GetPatientFolder(PatCur, ImageStore.GetPreferredAtoZpath())); FileAtoZ.Copy(fileSourcePath, fileDestPath, FileAtoZSourceDestination.LocalToAtoZ); Documents.Update(docSave); return(docSave); }
private void butSavePDFToImages_Click(object sender, EventArgs e) { if (gridMain.ListGridRows.Count == 0) { MsgBox.Show(this, "Grid is empty."); return; } //Get image category to save to. First image "Statement(S)" category. List <Def> listImageCatDefs = Defs.GetDefsForCategory(DefCat.ImageCats, true).Where(x => x.ItemValue.Contains("S")).ToList(); if (listImageCatDefs.IsNullOrEmpty()) { MsgBox.Show(this, "No image category set for Statements."); return; } string tempFile = PrefC.GetRandomTempFile(".pdf"); CreatePDF(tempFile); Patient patCur = _fam.GetPatient(PatNum); string rawBase64 = ""; if (PrefC.AtoZfolderUsed == DataStorageType.InDatabase) { rawBase64 = Convert.ToBase64String(File.ReadAllBytes(tempFile)); } Document docSave = new Document(); docSave.DocNum = Documents.Insert(docSave); docSave.ImgType = ImageType.Document; docSave.DateCreated = DateTime.Now; docSave.PatNum = PatNum; docSave.DocCategory = listImageCatDefs.FirstOrDefault().DefNum; docSave.Description = $"ServiceDateView" + docSave.DocNum + $"{docSave.DateCreated.Year}_{docSave.DateCreated.Month}_{docSave.DateCreated.Day}"; docSave.RawBase64 = rawBase64; //blank if using AtoZfolder string fileName = ODFileUtils.CleanFileName(docSave.Description); string filePath = ImageStore.GetPatientFolder(patCur, ImageStore.GetPreferredAtoZpath()); while (FileAtoZ.Exists(FileAtoZ.CombinePaths(filePath, fileName + ".pdf"))) { fileName += "x"; } FileAtoZ.Copy(tempFile, ODFileUtils.CombinePaths(filePath, fileName + ".pdf"), FileAtoZSourceDestination.LocalToAtoZ); docSave.FileName = fileName + ".pdf"; //file extension used for both DB images and AtoZ images Documents.Update(docSave); try { File.Delete(tempFile); //cleanup the temp file. } catch (Exception ex) { ex.DoNothing(); } MsgBox.Show(this, "PDF saved successfully."); }
///<summary>Throws exceptions. Creates a new file inside of the email attachment path (inside OpenDentImages) and returns an EmailAttach object ///referencing the new file. If isOutbound is true, then the file will be saved to the "Out" subfolder, otherwise the file will be saved to the ///"In" subfolder. The displayFileName will always contain valid file name characters, because it is either a hard coded value or is based on an ///existing valid file name. If a file already exists matching the actualFileName, then an exception will occur. Set actualFileName to empty ///string to generate a unique actual file name. If the actual file name is generated, then actual file name will end with the displayFileName, ///so that the actual files are easier to locate and have the same file extension as the displayedFileName.</summary> public static EmailAttach CreateAttach(string displayedFileName, string actualFileName, byte[] arrayData, bool isOutbound) { //No need to check RemotingRole; no call to db. EmailAttach emailAttach = new EmailAttach(); emailAttach.DisplayedFileName = displayedFileName; actualFileName = ODFileUtils.CleanFileName(actualFileName); //Clean the actual file name for the OS. if (String.IsNullOrEmpty(emailAttach.DisplayedFileName)) { //This could only happen for malformed incoming emails, but should not happen. Name uniqueness is virtually guaranteed below. //The actual file name will not have an extension, so the user will be asked to pick the program to open the attachment with when //the attachment is double-clicked. emailAttach.DisplayedFileName = "attach"; } string attachDir = GetAttachPath(); string subDir = "In"; if (isOutbound) { subDir = "Out"; } if (!CloudStorage.IsCloudStorage && !Directory.Exists(ODFileUtils.CombinePaths(attachDir, subDir))) { Directory.CreateDirectory(ODFileUtils.CombinePaths(attachDir, subDir)); } if (String.IsNullOrEmpty(actualFileName)) { while (String.IsNullOrEmpty(emailAttach.ActualFileName) || FileAtoZ.Exists(FileAtoZ.CombinePaths(attachDir, emailAttach.ActualFileName))) { //Display name is tacked onto actual file name last as to ensure file extensions are the same. emailAttach.ActualFileName = FileAtoZ.CombinePaths(subDir, DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.TimeOfDay.Ticks.ToString() + "_" + MiscUtils.CreateRandomAlphaNumericString(4) + "_" + ODFileUtils.CleanFileName(emailAttach.DisplayedFileName)); } } else { //The caller wants a specific actualFileName. Use the given name as is. emailAttach.ActualFileName = FileAtoZ.CombinePaths(subDir, actualFileName); } string attachFilePath = FileAtoZ.CombinePaths(attachDir, emailAttach.ActualFileName); if (FileAtoZ.Exists(attachFilePath)) { throw new ApplicationException("Email attachment could not be saved because a file with the same name already exists."); } try { FileAtoZ.WriteAllBytes(attachFilePath, arrayData); } catch (Exception ex) { try { if (FileAtoZ.Exists(attachFilePath)) { FileAtoZ.Delete(attachFilePath); } } catch { //We tried our best to delete the file, and there is nothing else to try. } throw ex; //Show the initial error message, even if the Delete() failed. } return(emailAttach); }