public bool PushMessage(string text, Loger loger, int ChatId, string fileName) { //FileDTO file = new JavaScriptSerializer().Deserialize<FileDTO>(JSONfile); using (Meseger ctx = new Meseger()) { User user = ctx.Users.SingleOrDefault(a => a.Login == loger.Login); int id = GetChat(loger, ChatId); Chat chat = ctx.Chats.SingleOrDefault(a => a.Id == id); if (ChatUserExists(loger, chat.Id) && UserLogin(loger)) { Sender sender = ctx.Senders.SingleOrDefault(a => a.Id == user.Id); if (sender == null) { sender = new Sender { User = user }; } Message msg = new Message { Text = text, Sender = sender, DateTime = DateTime.Now, Chat = chat }; if (fileName != String.Empty) { string domain = System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath); string uploadFolder = domain + @"\upload\"; if (System.IO.File.Exists(uploadFolder + fileName)) { string path = domain + $@"\{chat.Guid}\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } System.IO.File.Move(uploadFolder + fileName, path + fileName); if (System.IO.File.Exists(path + fileName)) { Model.File file = new Model.File { Path = path + fileName }; msg.File = file; } } } ctx.Messages.Add(msg); ctx.SaveChanges(); return(true); } return(false); } }
//public Model.File UploaderFile(Loger loger,FileDTO file) //{ // if (UserLogin(loger) && file.FileStream != null && file.ChatId != -1) // { // int realChatId = GetChat(loger, file.ChatId); // Chat chat = new Chat(); // string path = null; // using(Meseger ctx = new Meseger()) // { // chat = ctx.Chats.Find(realChatId); // path = $"./{chat.Guid}"; // if (!Directory.Exists(path)) // { // Directory.CreateDirectory(path); // } // //TransferService transfer = new TransferService(path); // //RemoteFileInfo fileInfo = new RemoteFileInfo(); // //fileInfo.FileByteStream = file.FileStream; // //fileInfo.FileName = file.FileName; // //fileInfo.Length = file.FileInfo.Length; // //transfer.UploadFile(fileInfo); // } // FileInfo info = new FileInfo(path + "/" + file.FileName); // if (info.Exists) // { // return new Model.File { Path = info.FullName }; // } // } // return null; //} public RemoteFileInfo DownloadFile(DownloadRequest request) { Message message = new Message(); Chat chat = new Chat(); using (Meseger ctx = new Meseger()) { message = ctx.Messages.Find(request.IdMessage); chat = message.Chat; } if (UserLogin(request.Loger) && message.Id == request.IdMessage && ChatUserExists(request.Loger, chat.Id)) { Model.File file = new Model.File(); using (Meseger ctx = new Meseger()) { file = ctx.Files.SingleOrDefault(a => a.Id == message.Id); } if (file.Id != message.Id) { return(null); } RemoteFileInfo result = new RemoteFileInfo(); try { //string filePath = Path.Combine(request.FileName, request.FileName); FileInfo fileInfo = new FileInfo(file.Path); // check if exists if (!fileInfo.Exists) { throw new FileNotFoundException("Файл не знайдений", file.Path); } // open stream FileStream stream = new FileStream(file.Path, System.IO.FileMode.Open, FileAccess.Read); // return result result.FileName = fileInfo.Name; result.Length = fileInfo.Length; result.FileByteStream = stream; } catch (Exception ex) { } return(result); } return(null); }