public void SetChatPhoto(int chatId, InfoFile info) { string path = FreePath(Path.Combine(pathToChatsPhoto, chatId.ToString() + Path.GetExtension(info.Name))); DirectoryInfo directory = new DirectoryInfo(pathToChatsPhoto); if (!directory.Exists) { directory.Create(); } using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { fs.Write(info.Data, 0, info.Data.Length); } repositories.ChatRepos.Get().Where(c => c.Id == chatId).FirstOrDefault().PhotoPath = path; repositories.Save(); var result = directory.GetFiles().Where(d => ((!d.Name.Contains('(') && Path.GetFileNameWithoutExtension(d.Name) == chatId.ToString()) || (d.Name.Contains('(') && d.Name.Replace(d.Name.Substring(d.Name.IndexOf('(')), null) == chatId.ToString())) && d.FullName != path); foreach (var item in result) { try { item.Delete(); } catch (Exception) { } } foreach (var item in TakeClients(chatId)) { foreach (var item2 in callbacks) { if (item.Client.Id == item2.ClientId) { item2.Callback.GetNewChatPhoto(chatId, info); } } } }
public void SetPhoto(int clientId, InfoFile info) { string path = FreePath(Path.Combine(pathToClientsPhoto, clientId.ToString() + Path.GetExtension(info.Name))); DirectoryInfo directory = new DirectoryInfo(pathToClientsPhoto); if (!directory.Exists) { directory.Create(); } using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { fs.Write(info.Data, 0, info.Data.Length); } repositories.ClientRepos.Get().Where(c => c.Id == clientId).FirstOrDefault().PhotoPath = path; repositories.Save(); var result = directory.GetFiles().Where(d => ((!d.Name.Contains('(') && Path.GetFileNameWithoutExtension(d.Name) == clientId.ToString()) || (d.Name.Contains('(') && d.Name.Replace(d.Name.Substring(d.Name.IndexOf('(')), null) == clientId.ToString())) && d.FullName != path); foreach (var item in result) { try { item.Delete(); } catch (Exception) { } } foreach (var item in callbacks) { if (item.ClientId != clientId) { var contacts = TakeContacts(item.ClientId); if (contacts.FirstOrDefault(c => c.Id == clientId) != null) { try { item.Callback.GetNewClientPhoto(clientId, info); } catch (Exception) { } } } } foreach (var item in callbacks) { if (item.ClientId != clientId) { var chats = TakeChats(item.ClientId); foreach (var item2 in chats) { var members = TakeClients(item2.Id); foreach (var item3 in members) { if (item3.ClientId == clientId) { try { item.Callback.GetNewClientPhoto(clientId, info); } catch (Exception) { } } } } } } var pmChats = repositories.ChatRepos.Get().Where(c => c.IsPM && c.ChatMembers.Where(cm => cm.Client.Id == clientId).Count() > 0); foreach (var item in pmChats) { foreach (var item2 in callbacks) { if (item2.ClientId != clientId) { var oponent = item.ChatMembers.FirstOrDefault(cm => cm.Client.Id != clientId); if (oponent != null) { if (oponent.Client.Id == item2.ClientId) { item2.Callback.SetNewPMChatPhoto(item.Id, info); } } } } } }