using PWAPPv2.Source.API; using PWAPPv2.Source.DataObjects; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mail; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace PWAPPv2.Source.Attachments { public class FileHandler { public List SupportedImageTypes; public List SupportedVideoTypes; public List SupportedDocumentTypes; public List Attachments; private APICredentials credentials; private string filterString; public FileHandler(APICredentials apiCredentials) { credentials = apiCredentials; SupportedImageTypes = new List(); SupportedVideoTypes = new List(); SupportedDocumentTypes = new List(); SupportedImageTypes.Clear(); SupportedImageTypes.Add(".jpeg"); SupportedImageTypes.Add(".jpg"); SupportedImageTypes.Add(".png"); //SupportedImageTypes.Add(".tiff"); SupportedDocumentTypes.Clear(); SupportedDocumentTypes.Add(".pdf"); Attachments = new List(); filterString = "Attachment files ("; string filter = "|"; foreach (string imageType in SupportedImageTypes) { filterString += "*" + imageType + ","; filter += "*" + imageType + ";"; } foreach (string videoType in SupportedVideoTypes) { filterString += "*" + videoType + ","; filter += "*" + videoType + ";"; } foreach (string documentType in SupportedDocumentTypes) { filterString += "*" + documentType + ","; filter += "*" + documentType + ";"; } filterString = filterString.Remove(filterString.Length - 1, 1); filterString += ")" + filter; } public void AddFile() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = filterString; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if (openFileDialog.ShowDialog() == DialogResult.OK) { foreach (string filename in openFileDialog.FileNames) { if(SupportedImageTypes.Contains(Path.GetExtension(filename))) { try { Attachments.Add(new DataObjects.Attachment(credentials, new DataObjects.PWImage(filename))); } catch (NullReferenceException) { } } if(SupportedDocumentTypes.Contains(Path.GetExtension(filename))) { try { Attachments.Add(new Source.DataObjects.Attachment(credentials, new Source.DataObjects.PWPdf(filename))); } catch(NullReferenceException) { } } } } } public void SendFilesAsAttachments(APIConnection connection, string token) { if(Attachments.Count <= 0) { return; } foreach(DataObjects.Attachment attachment in Attachments) { PWAttachment att = null; if (attachment.image != null) { att = AttachmentFactory.BuildAttachment(credentials, token, attachment.image); } else if (attachment.pdf != null) { att = AttachmentFactory.BuildAttachment(credentials, token, attachment.pdf); } if (att == null) { throw new AttachmentTypeNotYetSupportedException(); } string json = JsonSerializer.Serialize(att); connection.SendPostWithCredsInHeader("api/PWAttachment", json); } } } public class AttachmentTypeNotYetSupportedException : Exception { } }