Пример #1
0
        /// <summary>
        /// Добовляет файл на раздачу.
        /// </summary>
        /// <param name="roomName">Название комнаты в которую добавляется файл.</param>
        /// <param name="path">Путь к добовляемому файлу.</param>
        public void AddFileToRoom(string roomName, string path)
        {
            if (string.IsNullOrEmpty(roomName))
            {
                throw new ArgumentException("roomName");
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("fileName");
            }

            FileInfo info = new FileInfo(path);

            if (!info.Exists)
            {
                return;
            }

            using (var client = ClientModel.Get())
            {
                PostedFile postedFile;
                postedFile = client.PostedFiles.FirstOrDefault(posted =>
                                                               posted.File.Owner.Equals(client.User) &&
                                                               string.Equals(posted.ReadStream.Name, path) &&
                                                               string.Equals(posted.RoomName, roomName));

                // Отправляем на сервер уже созданный файл (нет необходимости создавать новый id)
                if (postedFile != null)
                {
                    var oldSendingContent = new ServerAddFileToRoomCommand.MessageContent {
                        RoomName = roomName, File = postedFile.File
                    };
                    ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.Id, oldSendingContent);
                    return;
                }

                // Создаем новый файл
                int id = 0;
                while (client.PostedFiles.Exists(postFile => postFile.File.ID == id))
                {
                    id = idCreator.Next(int.MinValue, int.MaxValue);
                }

                FileDescription file = new FileDescription(client.User, info.Length, Path.GetFileName(path), id);

                client.PostedFiles.Add(new PostedFile
                {
                    File       = file,
                    RoomName   = roomName,
                    ReadStream = new FileStream(path, FileMode.Open, FileAccess.Read)
                });

                var newSendingContent = new ServerAddFileToRoomCommand.MessageContent {
                    RoomName = roomName, File = file
                };
                ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.Id, newSendingContent);
            }
        }
Пример #2
0
        public void Perform()
        {
            var info = new FileInfo(_fileName);

            if (!info.Exists)
            {
                throw new InvalidOperationException("File not exist.");
            }

            using (var client = ClientModel.Get())
            {
                var posted         = client.Chat.GetOrCreatePostedFile(info, _roomName);
                var sendingContent = new ServerAddFileToRoomCommand.MessageContent {
                    RoomName = _roomName, File = posted.File.ToDto()
                };
                ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.CommandId, sendingContent);
            }
        }
Пример #3
0
        public void AddFileToRoom(string roomName, string path)
        {
            if (string.IsNullOrEmpty(roomName))
            throw new ArgumentException("roomName");

              if (string.IsNullOrEmpty(path))
            throw new ArgumentException("fileName");

              var info = new FileInfo(path);
              if (!info.Exists)
            return;

              using (var client = ClientModel.Get())
              {
            var postedFile = client.PostedFiles.FirstOrDefault(posted =>
              posted.File.Owner.Equals(client.User)
              && string.Equals(posted.ReadStream.Name, path)
              && string.Equals(posted.RoomName, roomName));

            // Отправляем на сервер уже созданный файл (нет необходимости создавать новый id)
            if (postedFile != null)
            {
              var oldSendingContent = new ServerAddFileToRoomCommand.MessageContent { RoomName = roomName, File = postedFile.File };
              ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.CommandId, oldSendingContent);
              return;
            }

            // Создаем новый файл
            var id = 0;
            while (client.PostedFiles.Exists(postFile => postFile.File.Id == id))
              id = idCreator.Next(int.MinValue, int.MaxValue);

            var file = new FileDescription(client.User, info.Length, Path.GetFileName(path), id);

            client.PostedFiles.Add(new PostedFile
            {
              File = file,
              RoomName = roomName,
              ReadStream = new FileStream(path, FileMode.Open, FileAccess.Read)
            });

            var newSendingContent = new ServerAddFileToRoomCommand.MessageContent { RoomName = roomName, File = file };
            ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.CommandId, newSendingContent);
              }
        }
Пример #4
0
        public void AddFileToRoom(string roomName, string path)
        {
            if (string.IsNullOrEmpty(roomName))
            {
                throw new ArgumentException("roomName");
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("fileName");
            }

            var info = new FileInfo(path);

            if (!info.Exists)
            {
                return;
            }

            using (var client = ClientModel.Get())
            {
                var postedFile = client.PostedFiles.FirstOrDefault(posted =>
                                                                   posted.File.Id.Owner.Equals(client.User) &&
                                                                   string.Equals(posted.ReadStream.Name, path) &&
                                                                   string.Equals(posted.RoomName, roomName));

                // If file already exist then send created file on server.
                if (postedFile != null)
                {
                    var oldSendingContent = new ServerAddFileToRoomCommand.MessageContent {
                        RoomName = roomName, File = postedFile.File
                    };
                    ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.CommandId, oldSendingContent);
                    return;
                }

                // Create new file.
                FileId id;
                while (true)
                {
                    id = new FileId(idCreator.Next(int.MinValue, int.MaxValue), client.User.Nick);
                    if (!client.PostedFiles.Exists(postFile => postFile.File.Id == id))
                    {
                        break;
                    }
                }
                var file = new FileDescription(id, info.Length, Path.GetFileName(path));

                client.PostedFiles.Add(new PostedFile
                {
                    File       = file,
                    RoomName   = roomName,
                    ReadStream = new FileStream(path, FileMode.Open, FileAccess.Read)
                });

                var newSendingContent = new ServerAddFileToRoomCommand.MessageContent {
                    RoomName = roomName, File = file
                };
                ClientModel.Client.SendMessage(ServerAddFileToRoomCommand.CommandId, newSendingContent);
            }
        }