Der Login läuft jetzt mit RestSharp.
Die Akzentfarbe von den Chatgruppen vom Server wird berücksichtigt. Bilder sind im richtigen Seitenverhältnis skaliert. Ein Bild, das man gerade erst verschickt hat und das man öffnen will, bevor es auf dem Server angekommen ist, wird jetzt erst angezeigt, sobald es auf dem Server ist. Die Textbox für eine neue Nachricht wird nur angezeigt, wenn eine Gruppe ausgewählt ist und wenn man das Recht hat Nachrichten an diese Gruppe zu verschicken. Diverse Codeverbesserungen.
This commit is contained in:
@@ -20,9 +20,16 @@ namespace ChatController.Utilities
|
||||
public static readonly string GetChatGroupsUrl = "/api/chat/groups";
|
||||
public static readonly string LoadMessagesForGroupUrl = "/api/chat/messages?groupid=";
|
||||
public static readonly string LoadOwnChatNewsUrl = "/api/ownchat/news/";
|
||||
public static readonly string SendMessageUrl = "/api/chat/messages/send";
|
||||
|
||||
public static readonly string UrlPageParameter = "&page=";
|
||||
|
||||
public static readonly int NotificationTimeout = 1500;
|
||||
|
||||
public static readonly string ContentTypeKey = "content-type";
|
||||
public static readonly string FormUrlEncodedContentTypeValue = "application/x-www-form-urlencoded";
|
||||
public static readonly string MultipartContentTypeValue = "multipart/form-data";
|
||||
|
||||
public static readonly string TenantAndChatCodeFileName = "ownChat.txt";
|
||||
}
|
||||
}
|
||||
|
||||
21
ChatController/Utilities/Extensions/ControlExtensions.cs
Normal file
21
ChatController/Utilities/Extensions/ControlExtensions.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace ChatController.Utilities.Extensions
|
||||
{
|
||||
public static class ControlExtensions
|
||||
{
|
||||
private static readonly Action _EmptyDelegate = delegate { };
|
||||
|
||||
public static void Dispatch(this DispatcherObject p, Action action)
|
||||
{
|
||||
p.Dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
|
||||
}
|
||||
|
||||
public static void RefreshUI(this UIElement uiElement)
|
||||
{
|
||||
uiElement.Dispatcher.Invoke(DispatcherPriority.Render, _EmptyDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
131
ChatController/Utilities/FileUtils.cs
Normal file
131
ChatController/Utilities/FileUtils.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ChatController.Utilities
|
||||
{
|
||||
public class FileUtils
|
||||
{
|
||||
public static string ScaleImage(string pFile, string pFormat, long maxUploadSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] mediaFile;
|
||||
using(Stream reader = File.OpenRead(pFile))
|
||||
{
|
||||
mediaFile = Utils.ReadFully(reader);
|
||||
}
|
||||
|
||||
var memoryStream = new MemoryStream(mediaFile);
|
||||
var image = Image.FromStream(memoryStream);
|
||||
|
||||
float width, height;
|
||||
|
||||
if(image.Height < image.Width)
|
||||
{
|
||||
if(image.Height > 1080)
|
||||
{
|
||||
var factor = (float)image.Height / 1080;
|
||||
|
||||
height = 1080;
|
||||
|
||||
width = image.Width / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
else if(image.Height < 1080 && image.Width < 1080)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(image.Width > 1080)
|
||||
{
|
||||
var factor = (float)image.Width / 1080;
|
||||
|
||||
width = 1080;
|
||||
|
||||
height = image.Height / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
|
||||
var scaledBitmap = new Bitmap(image, new Size((int)width, (int)height));
|
||||
|
||||
const int orientationId = 0x0112;
|
||||
|
||||
if(image.PropertyIdList.Contains(orientationId))
|
||||
{
|
||||
var item = image.GetPropertyItem(orientationId);
|
||||
|
||||
scaledBitmap.SetPropertyItem(item);
|
||||
}
|
||||
|
||||
using(var graphics = Graphics.FromImage(image))
|
||||
{
|
||||
graphics.Clear(Color.Transparent);
|
||||
|
||||
graphics.InterpolationMode = InterpolationMode.Low;
|
||||
|
||||
graphics.DrawImage(scaledBitmap, (int)width, (int)height);
|
||||
}
|
||||
|
||||
var filePath = Path.GetTempPath() + Guid.NewGuid() + pFormat;
|
||||
|
||||
if(pFormat.Equals(".JPG") || pFormat.Equals(".JPE") || pFormat.Equals(".JPEG") || pFormat.Equals(".BMP"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Jpeg);
|
||||
}
|
||||
else if(pFormat.Equals(".PNG"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Png);
|
||||
}
|
||||
else if(pFormat.Equals(".GIF"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Gif);
|
||||
}
|
||||
|
||||
if(File.Exists(filePath))
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
var fileInfoLength = fileInfo.Length;
|
||||
|
||||
if(maxUploadSize < fileInfoLength)
|
||||
{
|
||||
File.Delete(filePath);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
memoryStream.Dispose();
|
||||
memoryStream.Close();
|
||||
|
||||
return filePath;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CheckFileSize(string pathToFile, long maxFileSize)
|
||||
{
|
||||
var fileInfo = new FileInfo(pathToFile);
|
||||
|
||||
return fileInfo.Length > maxFileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
ChatController/Utilities/OwnChatEnums.cs
Normal file
12
ChatController/Utilities/OwnChatEnums.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace ChatController.Utilities
|
||||
{
|
||||
public enum ChatMessageType
|
||||
{
|
||||
Message,
|
||||
Image,
|
||||
DefaultImage,
|
||||
Document,
|
||||
Hyperlink,
|
||||
Separator
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
@@ -6,7 +7,6 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
@@ -19,16 +19,91 @@ namespace ChatController.Utilities
|
||||
{
|
||||
public class Utils
|
||||
{
|
||||
public static DateTime DefaultDate = new DateTime(1,1,1);
|
||||
public static string AuthToken { get; set; }
|
||||
|
||||
public static string Tenant { get; set; }
|
||||
|
||||
public static DateTime DefaultDate = new DateTime(1, 1, 1);
|
||||
|
||||
public static ImageSource AvatarToImageSourceConverter(string pAvatarPath, bool pIsEmployee, bool pIsGroup)
|
||||
{
|
||||
return CreateImageSourceFromPath(pAvatarPath, pIsGroup, pIsEmployee);
|
||||
}
|
||||
|
||||
public static ImageSource CreateChatGroupPictureImageSource(string pPicturePath, bool pIsGroup, bool pIsEmployee)
|
||||
public static Bitmap ConvertAvatarToBitmap(string linkToPicture, bool isEmployee, bool isGroup)
|
||||
{
|
||||
return CreateImageSourceFromPath(pPicturePath, pIsGroup, pIsEmployee);
|
||||
Bitmap bitmap = null;
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(linkToPicture))
|
||||
{
|
||||
var client = new RestClient(linkToPicture);
|
||||
|
||||
var request = new RestRequest(Method.GET)
|
||||
{
|
||||
ResponseWriter = responseStream =>
|
||||
{
|
||||
try
|
||||
{
|
||||
bitmap = new Bitmap(responseStream);
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
request.AddHeader(Constants.Token, AuthToken);
|
||||
request.AddHeader(Constants.CustomerId, Tenant);
|
||||
|
||||
client.DownloadData(request);
|
||||
}
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
var defaultBitmap = (BitmapImage) GetDefaultImageSource(isGroup, isEmployee);
|
||||
|
||||
using(var outStream = new MemoryStream())
|
||||
{
|
||||
BitmapEncoder enc = new BmpBitmapEncoder();
|
||||
enc.Frames.Add(BitmapFrame.Create(defaultBitmap));
|
||||
enc.Save(outStream);
|
||||
|
||||
return new Bitmap(outStream);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DownloadProfilePictureAsync(string avatarPath, bool isEmployee, bool isGroup, Action<ImageSource> callback)
|
||||
{
|
||||
var url = avatarPath;
|
||||
|
||||
var client = new RestClient(url);
|
||||
var request = new RestRequest
|
||||
{
|
||||
ResponseWriter = stream =>
|
||||
{
|
||||
var bitmap = new BitmapImage();
|
||||
|
||||
var localStream = new MemoryStream();
|
||||
|
||||
stream.CopyTo(localStream);
|
||||
|
||||
localStream.Position = 0;
|
||||
|
||||
bitmap.BeginInit();
|
||||
|
||||
bitmap.StreamSource = localStream;
|
||||
|
||||
bitmap.EndInit();
|
||||
|
||||
bitmap.Freeze();
|
||||
|
||||
callback?.Invoke(bitmap);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static ImageSource GetDefaultImageSource(bool pIsGroup, bool pIsEmployee)
|
||||
@@ -43,7 +118,7 @@ namespace ChatController.Utilities
|
||||
{
|
||||
defaultImage = Constants.CustomerDefaultImagePath;
|
||||
}
|
||||
|
||||
|
||||
var resultBitmapImage = new BitmapImage();
|
||||
|
||||
resultBitmapImage.BeginInit();
|
||||
@@ -60,6 +135,7 @@ namespace ChatController.Utilities
|
||||
Bitmap bitmap = null;
|
||||
|
||||
var client = new RestClient(pPathToImageFile);
|
||||
|
||||
var request = new RestRequest(Method.GET)
|
||||
{
|
||||
ResponseWriter = stream =>
|
||||
@@ -75,6 +151,9 @@ namespace ChatController.Utilities
|
||||
}
|
||||
};
|
||||
|
||||
request.AddHeader(Constants.Token, AuthToken);
|
||||
request.AddHeader(Constants.CustomerId, Tenant);
|
||||
|
||||
client.DownloadData(request);
|
||||
|
||||
if (bitmap != null)
|
||||
@@ -92,9 +171,6 @@ namespace ChatController.Utilities
|
||||
bitmapImage.StreamSource = memoryStream;
|
||||
bitmapImage.EndInit();
|
||||
|
||||
memoryStream.Dispose();
|
||||
memoryStream.Close();
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
}
|
||||
@@ -102,42 +178,6 @@ namespace ChatController.Utilities
|
||||
|
||||
return GetDefaultImageSource(pIsGroup, pIsEmployee);
|
||||
}
|
||||
|
||||
public static ImageSource CreateImageSourceFromSmallerImage(string pUri)
|
||||
{
|
||||
Bitmap bitmap;
|
||||
var request = WebRequest.Create(pUri);
|
||||
using(var response = request.GetResponse())
|
||||
{
|
||||
using(var stream = response.GetResponseStream())
|
||||
{
|
||||
if(stream == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
bitmap = new Bitmap(stream);
|
||||
}
|
||||
}
|
||||
|
||||
using(var memoryStream = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(memoryStream, ImageFormat.Png);
|
||||
memoryStream.Position = 0;
|
||||
|
||||
var bitmapImage = new BitmapImage();
|
||||
|
||||
bitmapImage.BeginInit();
|
||||
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmapImage.StreamSource = memoryStream;
|
||||
bitmapImage.EndInit();
|
||||
|
||||
memoryStream.Dispose();
|
||||
memoryStream.Close();
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ReadStream(WebResponse response)
|
||||
{
|
||||
@@ -226,7 +266,6 @@ namespace ChatController.Utilities
|
||||
graphics.DrawIcon(pIcon, 0, 0);
|
||||
|
||||
graphics.Dispose();
|
||||
|
||||
|
||||
bitmap.Save("icon.ico", ImageFormat.Icon);
|
||||
|
||||
@@ -289,10 +328,6 @@ namespace ChatController.Utilities
|
||||
return Icon.FromHandle(bitmap.GetHicon());
|
||||
}
|
||||
|
||||
public static DateTime FromUnixTimeStamp(long pUnixTimeStamp)
|
||||
{
|
||||
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(pUnixTimeStamp);
|
||||
}
|
||||
public static byte[] ReadFully(Stream pStream)
|
||||
{
|
||||
using (var memStream = new MemoryStream())
|
||||
@@ -326,5 +361,97 @@ namespace ChatController.Utilities
|
||||
return RotateFlipType.RotateNoneFlipNone;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
|
||||
{
|
||||
var visualCollection = new List<T>();
|
||||
GetVisualChildCollection(parent as DependencyObject, visualCollection);
|
||||
|
||||
return visualCollection;
|
||||
}
|
||||
|
||||
public static void GetVisualChildCollection<T>(DependencyObject parent, ICollection<T> visualCollection) where T : Visual
|
||||
{
|
||||
var count = VisualTreeHelper.GetChildrenCount(parent);
|
||||
for(var i = 0; i < count; i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
|
||||
if(child is T item)
|
||||
{
|
||||
visualCollection.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetVisualChildCollection(child, visualCollection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* try
|
||||
{
|
||||
using (var form = new Form())
|
||||
{
|
||||
using (var ms = new MemoryStream(e.Result))
|
||||
{
|
||||
var image = System.Drawing.Image.FromStream(ms);
|
||||
|
||||
using (var bitmap = new Bitmap(image))
|
||||
{
|
||||
short orient = 0;
|
||||
const int orientationId = 0x0112;
|
||||
|
||||
if (image.PropertyIdList.Contains(orientationId))
|
||||
{
|
||||
var item = image.GetPropertyItem(orientationId);
|
||||
|
||||
orient = BitConverter.ToInt16(item.Value, 0);
|
||||
|
||||
bitmap.SetPropertyItem(item);
|
||||
}
|
||||
|
||||
var flipType = Utils.OrientationToFlipType(orient.ToString());
|
||||
|
||||
bitmap.RotateFlip(flipType);
|
||||
|
||||
form.StartPosition = FormStartPosition.CenterScreen;
|
||||
form.ClientSize = bitmap.Size;
|
||||
form.FormBorderStyle = FormBorderStyle.Sizable;
|
||||
form.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
|
||||
|
||||
using (var pictureBox = new PictureBox())
|
||||
{
|
||||
pictureBox.Dock = DockStyle.Fill;
|
||||
pictureBox.Image = bitmap;
|
||||
|
||||
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
|
||||
|
||||
form.Controls.Add(pictureBox);
|
||||
|
||||
downloadCompletedCallback?.Invoke();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public Bitmap ConvertStreamToBitmap(Stream imageStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
var image = Image.FromStream(imageStream);
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user