Exemplo n.º 1
0
        public async Task Upload(string userId,
                                 string connectionId,
                                 string roomName,
                                 string file,
                                 string contentType,
                                 Stream stream)
        {
            UploadResult result;
            ChatMessage  message;

            try
            {
                result = await _processor.HandleUpload(file, contentType, stream, stream.Length);

                if (result == null)
                {
                    string messageContent = String.Format(LanguageResources.UploadFailed, Path.GetFileName(file));
                    _hubContext.Clients.Client(connectionId).postMessage(messageContent, "error", roomName);
                    return;
                }
                else if (result.UploadTooLarge)
                {
                    string messageContent = String.Format(LanguageResources.UploadTooLarge, Path.GetFileName(file), (result.MaxUploadSize / 1048576f).ToString("0.00"));
                    _hubContext.Clients.Client(connectionId).postMessage(messageContent, "error", roomName);
                    return;
                }

                // Add the message to the persistent chat
                message = _service.AddMessage(userId, roomName, result.Url);

                // Keep track of this attachment
                _service.AddAttachment(message, file, contentType, stream.Length, result);
            }
            catch (Exception ex)
            {
                string messageContent = String.Format(LanguageResources.UploadFailedException, Path.GetFileName(file), ex.Message);
                _hubContext.Clients.Client(connectionId).postMessage(messageContent, "error", roomName);
                return;
            }

            var messageViewModel = new MessageViewModel(message);

            // Notify all clients for the uploaded url
            _hubContext.Clients.Group(roomName).addMessage(messageViewModel, roomName);

            _resourceProcessor.ProcessUrls(new[] { result.Url }, _hubContext.Clients, roomName, message.Id);
        }
Exemplo n.º 2
0
        public async Task Upload(string userId,
                                 string connectionId,
                                 string roomName,
                                 string file,
                                 string contentType,
                                 Stream stream)
        {
            if (stream.Length > _settings.MaxFileUploadBytes)
            {
                _hubContext.Clients.Client(connectionId).postMessage("Unable to upload " + Path.GetFileName(file) + " because it exceeded the maximum size allowed.", "error", roomName);
                return;
            }

            UploadResult result;
            ChatMessage  message;

            try
            {
                result = await _processor.HandleUpload(file, contentType, stream);

                if (result == null)
                {
                    _hubContext.Clients.Client(connectionId).postMessage("Failed to upload " + Path.GetFileName(file) + ".", "error", roomName);
                    return;
                }

                // Add the message to the persistent chat
                message = _service.AddMessage(userId, roomName, result.Url);

                // Keep track of this attachment
                _service.AddAttachment(message, file, contentType, stream.Length, result);
            }
            catch (Exception ex)
            {
                _hubContext.Clients.Client(connectionId).postMessage("Failed to upload " + Path.GetFileName(file) + ". " + ex.Message, "error", roomName);
                return;
            }

            var messageViewModel = new MessageViewModel(message);

            // Notify all clients for the uploaded url
            _hubContext.Clients.Group(roomName).addMessage(messageViewModel, roomName);

            _resourceProcessor.ProcessUrls(new[] { result.Url }, _hubContext.Clients, roomName, message.Id);
        }