Example #1
0
    public MessageViewModel(UserViewModel sender, string fileName, FileDescription fileDescription, RoomViewModel room)
      : this(Room.SpecificMessageId, room, true)
    {
      NotifierContext.DownloadProgress += ClientDownloadProgress;
      NotifierContext.PostedFileDeleted += ClientPostedFileDeleted;

      Sender = sender;
      File = fileDescription;
      Progress = 0;
      Title = string.Format(From, DateTime.Now.ToString(TimeFormat));

      string sizeDim = string.Empty;
      float size = 0;

      if (fileDescription.Size < 1024)
      {
        sizeDim = ByteStr;
        size = fileDescription.Size;
      }

      if (fileDescription.Size >= 1024 && fileDescription.Size < 1024 * 1024)
      {
        sizeDim = KByteStr;
        size = fileDescription.Size / 1024.0f;
      }

      if (fileDescription.Size >= 1024 * 1024)
      {
        sizeDim = MByteStr;
        size = fileDescription.Size / (1024.0f * 1024.0f);
      }
      
      Text = fileName + string.Format(SizeFormat, size, sizeDim);
      Type = MessageType.File;
      DownloadFileCommand = new Command(DownloadFile, Obj => ClientModel.Client != null);
    }
Example #2
0
 public void DownloadFile(string path, string roomName, FileDescription file)
 {
     ClientModel.API.DownloadFile(path, roomName, file);
 }
Example #3
0
 public void RemoveFileFromRoom(string roomName, FileDescription file)
 {
     ClientModel.API.RemoveFileFromRoom(roomName, file);
 }
Example #4
0
 public void AddFileMessage(UserViewModel sender, FileDescription file)
 {
     AddMessage(new MessageViewModel(sender, file.Name, file, this));
 }
Example #5
0
 public void CancelDownloading(FileDescription file, bool leaveLoadedPart)
 {
     ClientModel.API.CancelDownloading(file, leaveLoadedPart);
 }
Example #6
0
        public bool Equals(FileDescription file)
        {
            if (file == null)
            return false;

              return owner.Equals(file.owner) && id.Equals(file.id);
        }
Example #7
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);
              }
        }
Example #8
0
        public void RemoveFileFromRoom(string roomName, FileDescription file)
        {
            if (string.IsNullOrEmpty(roomName))
            throw new ArgumentException("roomName");

              if (file == null)
            throw new ArgumentNullException("file");

              using (var client = ClientModel.Get())
              {
            var postedFile = client.PostedFiles.FirstOrDefault(current => current.File.Equals(file));
            if (postedFile == null)
              return;

            client.PostedFiles.Remove(postedFile);
            postedFile.Dispose();
              }

              var sendingContent = new ServerRemoveFileFromRoomCommand.MessageContent { RoomName = roomName, File = file };
              ClientModel.Client.SendMessage(ServerRemoveFileFromRoomCommand.CommandId, sendingContent);
        }
Example #9
0
        public void DownloadFile(string path, string roomName, FileDescription file)
        {
            if (string.IsNullOrEmpty(roomName))
            throw new ArgumentException("roomName");

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

              if (file == null)
            throw new ArgumentNullException("file");

              if (File.Exists(path))
            throw new ArgumentException("Файл уже существует");

              using (var client = ClientModel.Get())
              {
            if (client.DownloadingFiles.Exists(dFile => dFile.File.Equals(file)))
              throw new ModelException(ErrorCode.FileAlreadyDownloading, file);

            if (client.User.Equals(file.Owner))
              throw new ArgumentException("Нельзя скачивать свой файл.");

            client.DownloadingFiles.Add(new DownloadingFile { File = file, FullName = path });
              }

              var sendingContent = new ClientReadFilePartCommand.MessageContent
              {
            File = file,
            Length = AsyncClient.DefaultFilePartSize,
            RoomName = roomName,
            StartPartPosition = 0,
              };

              ClientModel.Peer.SendMessage(file.Owner.Nick, ClientReadFilePartCommand.CommandId, sendingContent);
        }
Example #10
0
        public void CancelDownloading(FileDescription file, bool leaveLoadedPart = true)
        {
            if (file == null)
            throw new ArgumentNullException("file");

              using (var client = ClientModel.Get())
              {
            var downloadingFile = client.DownloadingFiles.FirstOrDefault(current => current.File.Equals(file));
            if (downloadingFile == null)
              return;

            var filePath = downloadingFile.FullName;
            downloadingFile.Dispose();

            client.DownloadingFiles.Remove(downloadingFile);

            if (File.Exists(filePath) && !leaveLoadedPart)
              File.Delete(filePath);
              }
        }