goomatt33 205b3d8014 Modified how attachments are pushed to the API.
Added support for BMP and GIF image types.
2024-03-25 13:21:14 -04:00

164 lines
6.2 KiB
C#

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<string> SupportedImageTypes;
public List<string> SupportedVideoTypes;
public List<string> SupportedDocumentTypes;
public List<Source.DataObjects.Attachment> Attachments;
private APICredentials credentials;
private string filterString;
public FileHandler(APICredentials apiCredentials)
{
credentials = apiCredentials;
SupportedImageTypes = new List<string>();
SupportedVideoTypes = new List<string>();
SupportedDocumentTypes = new List<string>();
SupportedImageTypes.Clear();
SupportedImageTypes.Add(".jpeg");
SupportedImageTypes.Add(".jpg");
SupportedImageTypes.Add(".png");
SupportedImageTypes.Add(".tiff");
SupportedImageTypes.Add(".tif");
SupportedImageTypes.Add(".gif");
SupportedImageTypes.Add(".bmp");
SupportedDocumentTypes.Clear();
SupportedDocumentTypes.Add(".pdf");
Attachments = new List<Source.DataObjects.Attachment>();
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;
PWBaseAttachment ba = null;
List<PWFileUpload> fileUploads = new List<PWFileUpload>();
if (attachment.image != null)
{
//att = AttachmentFactory.BuildAttachment(credentials, token, attachment.image);
ba = AttachmentFactory.BuildBaseAttachment(token, attachment.image);
//fileUploads.Add(attachment.image.GetFileUploads(attachment.co))
string json = JsonSerializer.Serialize(ba);
string contId = connection.SendPostWithCredsInHeader("api/PWAddAttachment", json);
contId = contId.Replace("\"", "").Replace("\\n", "").Trim();
fileUploads = attachment.image.GetFileUploads(token, contId);
foreach(PWFileUpload file in fileUploads)
{
json = JsonSerializer.Serialize(file);
connection.SendPostWithCredsInHeader("api/PWUploadFile", json);
}
}
else if (attachment.pdf != null)
{
//att = AttachmentFactory.BuildAttachment(credentials, token, attachment.pdf);
ba = AttachmentFactory.BuildBaseAttachment(token, attachment.pdf);
string json = JsonSerializer.Serialize(ba);
string contId = connection.SendPostWithCredsInHeader("api/PWAddAttachment", json);
fileUploads = attachment.pdf.GetFileUploads(token, contId);
foreach (PWFileUpload file in fileUploads)
{
json = JsonSerializer.Serialize(file);
connection.SendPostWithCredsInHeader("api/PWUploadFile", json);
}
}
//if (att == null)
//{
// throw new AttachmentTypeNotYetSupportedException();
//}
//string json = JsonSerializer.Serialize(att);
//connection.SendPostWithCredsInHeader("api/PWAttachment", json);
//json = JsonSerializer.Serialize(ba);
//connection.SendPostWithCredsInHeader("api/PWAddAttachment", json);
}
}
}
public class AttachmentTypeNotYetSupportedException : Exception { }
}