コード例 #1
0
        /// <summary>
        /// Invoked when a message stanza has been received.
        /// </summary>
        /// <param name="stanza">The stanza which has been received.</param>
        /// <returns>true to intercept the stanza or false to pass the stanza
        /// on to the next handler.</returns>
        public bool Input(Message message)
        {
            if (message.Type == MessageType.Management)
            {
                // Do we receive an user invitation ?
                if (message.Data["userinvite"] != null)
                {
                    XmlElement e = message.Data["userinvite"];

                    try
                    {
                        string invitationId = e.GetAttribute("id");
                        string action       = e.GetAttribute("action"); // 'create', 'update', 'delete'
                        string type         = e.GetAttribute("type");   // 'received', 'sent'
                        string status       = e.GetAttribute("status"); // 'canceled', 'accepted' , 'pending'

                        UserInvitation.Raise(this, new UserInvitationEventArgs(invitationId, action, type, status));
                    }
                    catch (Exception)
                    {
                    }
                }
                // Do we receive message about conversation management
                else if (message.Data["conversation"] != null)
                {
                    XmlElement e = message.Data["conversation"];

                    string conversationID            = e.GetAttribute("id");
                    string action                    = e.GetAttribute("action"); // 'create', 'update', 'delete'
                    Dictionary <string, string> data = new Dictionary <string, string>();

                    if (e["type"] != null)
                    {
                        data.Add("type", e["type"].InnerText);
                    }

                    if (e["peerId"] != null)
                    {
                        data.Add("peerId", e["peerId"].InnerText);
                    }

                    if (e["peer"] != null)
                    {
                        data.Add("jid_im", e["peer"].InnerText);
                    }

                    if (e["mute"] != null)
                    {
                        data.Add("mute", e["mute"].InnerText);
                    }

                    if (e["lastMessageText"] != null)
                    {
                        data.Add("lastMessageText", e["lastMessageText"].InnerText);
                    }

                    if (e["lastMessageSender"] != null)
                    {
                        data.Add("lastMessageSender", e["lastMessageSender"].InnerText);
                    }

                    if (e["lastMessageDate"] != null)
                    {
                        data.Add("lastMessageDate", e["lastMessageDate"].InnerText);
                    }

                    if (e["unreceivedMessageNumber"] != null)
                    {
                        data.Add("unreceivedMessageNumber", e["unreceivedMessageNumber"].InnerText);
                    }

                    if (e["unreadMessageNumber"] != null)
                    {
                        data.Add("unreadMessageNumber", e["unreadMessageNumber"].InnerText);
                    }

                    ConversationManagement.Raise(this, new ConversationManagementEventArgs(conversationID, action, data));
                }
                // Do we receive message about favorite management
                else if (message.Data["favorite"] != null)
                {
                    XmlElement e = message.Data["favorite"];

                    string id       = e.GetAttribute("id");
                    string action   = e.GetAttribute("action"); // 'create', 'update', 'delete'
                    string type     = e.GetAttribute("type");   // 'user', 'room', 'bot'
                    string position = e.GetAttribute("position");
                    string peerId   = e.GetAttribute("peer_id");
                    FavoriteManagement.Raise(this, new FavoriteManagementEventArgs(id, action, type, position, peerId));
                }
                // Do we receive message about userpassword management
                else if (message.Data["userpassword"] != null)
                {
                    XmlElement e      = message.Data["userpassword"];
                    string     action = e.GetAttribute("action"); // 'update' only ?
                    if (action == "update")
                    {
                        PasswordUpdated.Raise(this, new EventArgs());
                    }
                }
                // Do we receive message about room/bubble management
                else if (message.Data["room"] != null)
                {
                    XmlElement e       = message.Data["room"];
                    string     roomId  = e.GetAttribute("roomid");
                    string     roomJid = e.GetAttribute("roomjid");

                    string userJid   = e.GetAttribute("userjid");                         // Not empty if user has been accepted / invited / unsubscribed / deleted
                    string status    = e.GetAttribute("status");                          // Not empty if user has been accepted / invited / unsubscribed / deleted
                    string privilege = e.GetAttribute("privilege");                       // Not empty if user has changed it's role: user / moderator / guest

                    string topic = e.GetAttribute("topic");                               // Not empty if room updated
                    string name  = e.GetAttribute("name");                                // Not empty if room updated

                    string lastAvatarUpdateDate = e.GetAttribute("lastAvatarUpdateDate"); // Not empty if avatar has been updated. if deleted "null" string

                    string avatarAction = "";
                    if (e["avatar"] != null)
                    {
                        avatarAction = e["avatar"].GetAttribute("action");
                    }

                    RoomManagement.Raise(this, new RoomManagementEventArgs(roomId, roomJid, userJid, status, privilege, name, topic, lastAvatarUpdateDate, avatarAction));
                }
                // Do we receive message about visualvoicemail
                else if (message.Data["visualvoicemail"] != null)
                {
                    // WE DO NOTHING HERE
                    // WE USE "file" message and file descrptior to manage voice message

                    //XmlElement e = message.Data["visualvoicemail"];

                    //string msgId = e.GetAttribute("msgid");
                    //string action = e.GetAttribute("action");

                    //string fileId = e["fileid"]?.InnerText;
                    //string url = e["url"]?.InnerText;
                    //string mimeType = e["mime"]?.InnerText;
                    //string fileName = e["filename"]?.InnerText;
                    //string size = e["size"]?.InnerText;
                    //string md5 = e["md5sum"]?.InnerText;
                    //string duration = e["duration"]?.InnerText;

                    ////log.LogDebug("duration:[{0}]", duration);
                    //VoiceMailManagement.Raise(this, new VoiceMailManagementEventArgs(msgId, fileId, action, url, mimeType, fileName, size, md5, duration));
                }
                // Do we receive message about file
                else if (message.Data["file"] != null)
                {
                    XmlElement e = message.Data["file"];

                    string action = e.GetAttribute("action");
                    string fileId = e["fileid"]?.InnerText;

                    FileManagement.Raise(this, new FileManagementEventArgs(fileId, action));
                }
                // Do we receive message about thumbnail
                else if (message.Data["thumbnail"] != null)
                {
                    XmlElement e = message.Data["thumbnail"];

                    string fileId    = e["fileid"]?.InnerText;
                    string widthStr  = e["originalwidth"]?.InnerText;
                    string heightStr = e["originalheight"]?.InnerText;

                    int width  = 0;
                    int height = 0;

                    try
                    {
                        int.TryParse(widthStr, out width);
                        int.TryParse(heightStr, out height);
                    }
                    catch (Exception exc)
                    {
                        log.LogWarning("[Input] Exception occurred for thumbnail: [{0}]", Util.SerializeException(exc));
                    }

                    ThumbnailManagement.Raise(this, new ThumbnailEventArgs(fileId, width, height));
                }
                else if (message.Data["channel"] != null)
                {
                    XmlElement e = message.Data["channel"];

                    string jid       = message.To.GetBareJid().ToString();
                    string channelId = e.GetAttribute("channelid");

                    string action = "";
                    string type   = "";

                    // Check avatar node
                    if (e["avatar"] != null)
                    {
                        type   = "avatar";
                        action = e["avatar"].GetAttribute("action");
                    }
                    else
                    {
                        action = e.GetAttribute("action");
                        if (e["type"] != null)
                        {
                            type = e["type"].InnerText;
                        }
                    }

                    ChannelManagement.Raise(this, new ChannelManagementEventArgs(jid, channelId, action, type));
                }
                else if (message.Data["channel-subscription"] != null)
                {
                    XmlElement e = message.Data["channel-subscription"];

                    string jid = e.GetAttribute("jid");

                    string channelId = e.GetAttribute("channelid");
                    string action    = e.GetAttribute("action");
                    string type      = ""; // Never Type info receive in this case

                    ChannelManagement.Raise(this, new ChannelManagementEventArgs(jid, channelId, action, type));
                }
                else if (message.Data["group"] != null)
                {
                    XmlElement e = message.Data["group"];
                    GroupManagement.Raise(this, new MessageEventArgs(e.ToXmlString()));
                }
                else if (message.Data["recordingfile"] != null)
                {
                    XmlElement e = message.Data["recordingfile"];
                    RecordingFile.Raise(this, new MessageEventArgs(e.ToXmlString()));
                }
                else
                {
                    log.LogInformation("[Input] Message not managed");
                }
                // Since it's a Management message, we prevent next handler to parse it
                return(true);
            }
            else if (message.Type == MessageType.Chat)
            {
                if (message.Data["x", "jabber:x:conference"] != null)
                {
                    XmlElement e;

                    String roomId, roomJid, roomName;
                    String userid, userjid, userdisplayname;
                    String subject;

                    userid  = userjid = userdisplayname = "";
                    subject = "";

                    e      = message.Data["x", "jabber:x:conference"];
                    roomId = e.GetAttribute("roomid");
                    if (String.IsNullOrEmpty(roomId))
                    {
                        roomId = e.GetAttribute("thread");
                    }

                    roomJid  = e.GetAttribute("jid");
                    roomName = e.GetAttribute("name");

                    e = message.Data["x", "jabber:x:bubble:conference:owner"];
                    if (e != null)
                    {
                        userid          = e.GetAttribute("userid");
                        userjid         = e.GetAttribute("jid");
                        userdisplayname = e.GetAttribute("displayname");
                    }

                    e = message.Data["subject"];
                    if (e != null)
                    {
                        subject = e.InnerText;
                    }

                    // Since it's a room invitation, we prevent next handler to parse it
                    RoomInvitation.Raise(this, new RoomInvitationEventArgs(roomId, roomJid, roomName, userid, userjid, userdisplayname, subject));

                    return(true);
                }
            }
            else if (message.Type == MessageType.Headline)
            {
                // Do we receive an event of pubsub type ?
                if (message.Data["event", "http://jabber.org/protocol/pubsub#event"] != null)
                {
                    XmlElement e = message.Data["event", "http://jabber.org/protocol/pubsub#event"];
                    if (e["items"] != null)
                    {
                        ChanneItemManagement.Raise(this, new MessageEventArgs(e["items"].ToXmlString()));
                        return(true);
                    }
                    else if (e["update"] != null)
                    {
                        ChanneItemManagement.Raise(this, new MessageEventArgs(e["update"].ToXmlString()));
                        return(true);
                    }
                }
            }

            // Pass the message on to the next handler.
            return(false);
        }
コード例 #2
0
        public override void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;

            HttpResponse response = context.Response;

            string fileName = request.QueryString["fileName"] == null ? string.Empty : request.QueryString["fileName"];

            string id = request.QueryString["id"] == null ? string.Empty : request.QueryString["id"];

            int targetWidth  = request.QueryString["targetWidth"] == null ? 100 : Convert.ToInt32(request.QueryString["targetWidth"]);
            int targetHeight = request.QueryString["targetHeight"] == null ? 100 : Convert.ToInt32(request.QueryString["targetHeight"]);

            string attachmentEntityClassName = request.QueryString["attachmentEntityClassName"] == null ? "X3Platform.AttachmentStorage.AttachmentFileInfo" : request.QueryString["attachmentEntityClassName"];

            if (string.IsNullOrEmpty(fileName))
            {
                // 以参数形式生成图片
                if (!string.IsNullOrEmpty(id))
                {
                    // 读取附件原始信息

                    IAttachmentFileInfo attachment = (IAttachmentFileInfo)SpringContext.Instance.Application.GetObject(attachmentEntityClassName);;

                    attachment.Restore(id);

                    if (attachment.FileData == null)
                    {
                        response.StatusCode = 404;
                        response.Write("Not Found");
                        return;
                    }

                    string defaultThumbnail = string.Format("{0}_{1}x{2}", attachment.FileType.Replace(".", ""), targetWidth, targetHeight);

                    if (AttachmentStorageConfigurationView.Instance.DefaultThumbnails.IndexOf(defaultThumbnail) > -1)
                    {
                        response.Redirect(AttachmentStorageConfigurationView.Instance.VirtualUploadFolder + "thumbnails/" + defaultThumbnail + ".png");
                        response.End();
                    }

                    fileName = string.Format("{0}_{1}x{2}{3}", id, targetWidth, targetHeight, attachment.FileType);

                    Stream stream = ByteHelper.ToStream(attachment.FileData);

                    // 创建缩略图
                    ThumbnailManagement.CreateThumbnail(id, stream, attachment.FileType, targetWidth, targetHeight);
                }
            }
            else
            {
                // 直接以文件名方式访问
                if (!ThumbnailManagement.IsExist(fileName))
                {
                    response.StatusCode = 404;
                    response.Write("Not Found");
                    return;
                }
            }

            response.Redirect(AttachmentStorageConfigurationView.Instance.VirtualUploadFolder + "thumbnails/" + fileName);
        }
コード例 #3
0
        public override void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;

            HttpResponse response = context.Response;

            try
            {
                HttpPostedFile file = request.Files["fileData"];

                // 输出类型 id uri base64
                string outputType = request.Form["outputType"];

                string attachmentId              = request.Form["attachmentId"];
                string entityId                  = request.Form["entityId"];
                string entityClassName           = request.Form["entityClassName"];
                string attachmentEntityClassName = request.Form["attachmentEntityClassName"];
                string attachmentFolder          = request.Form["attachmentFolder"];

                // 调整图片大小
                int resize       = request.Form["resize"] == null ? 0 : Convert.ToInt32(request.Form["resize"]);
                int targetWidth  = request.Form["targetWidth"] == null ? 0 : Convert.ToInt32(request.Form["targetWidth"]);
                int targetHeight = request.Form["targetHeight"] == null ? 0 : Convert.ToInt32(request.Form["targetHeight"]);

                // 设置图片缩略图
                int thumbnail       = request.Form["thumbnail"] == null ? 0 : Convert.ToInt32(request.Form["thumbnail"]);
                int thumbnailWidth  = request.QueryString["targetWidth"] == null ? 100 : Convert.ToInt32(request.QueryString["thumbnailWidth"]);
                int thumbnailHeight = request.QueryString["thumbnailHeight"] == null ? 100 : Convert.ToInt32(request.QueryString["thumbnailHeight"]);

                // 匿名用户上传文件的文件, 存放在 anonymous 目录
                if (KernelContext.Current.User == null)
                {
                    attachmentFolder = "anonymous";
                }

                IAttachmentFileInfo attachment = UploadFileHelper.CreateAttachmentFile(
                    entityId,
                    entityClassName,
                    attachmentEntityClassName,
                    attachmentFolder,
                    file);

                // Office 在线客户端上传方式
                // 支持客户端赋值附件标识
                if (!string.IsNullOrEmpty(attachmentId))
                {
                    if (AttachmentStorageContext.Instance.AttachmentFileService.IsExist(attachmentId))
                    {
                        HttpContext.Current.Response.StatusCode        = 500;
                        HttpContext.Current.Response.StatusDescription = "Attachment id already exists.";
                        HttpContext.Current.Response.End();
                    }
                    else
                    {
                        attachment.Id = attachmentId;
                    }
                }

                // 检测文件最小限制
                if (attachment.FileSize < AttachmentStorageConfigurationView.Instance.AllowMinFileSize)
                {
                    HttpContext.Current.Response.StatusCode        = 500;
                    HttpContext.Current.Response.StatusDescription = "Attachment file size is too small.";
                    HttpContext.Current.Response.End();
                }

                // 检测文件最大限制
                if (attachment.FileSize > AttachmentStorageConfigurationView.Instance.AllowMaxFileSize)
                {
                    HttpContext.Current.Response.StatusCode        = 500;
                    HttpContext.Current.Response.StatusDescription = "Attachment file size is too big.";
                    HttpContext.Current.Response.End();
                }

                // 检测文件名后缀限制
                if (!Regex.IsMatch(attachment.FileType, AttachmentStorageConfigurationView.Instance.AllowFileTypes))
                {
                    HttpContext.Current.Response.StatusCode        = 500;
                    HttpContext.Current.Response.StatusDescription = "Attachment file type is invalid.";
                    HttpContext.Current.Response.End();
                }

                if (HttpContext.Current.Response.StatusCode != 500)
                {
                    // 调整图片大小
                    if (resize == 1)
                    {
                        Image image = Image.FromStream(ByteHelper.ToStream(attachment.FileData));

                        attachment.FileData = ThumbnailManagement.Resize(image, attachment.FileType, targetWidth, targetHeight);
                    }

                    if (outputType == "base64")
                    {
                        // 输出 Base64
                        HttpContext.Current.Response.StatusCode = 200;

                        HttpContext.Current.Response.Write(ByteHelper.ToBase64(attachment.FileData));
                    }
                    else
                    {
                        attachment.Save();

                        // 调整图片大小
                        if (thumbnail == 1)
                        {
                            Image image = Image.FromStream(ByteHelper.ToStream(attachment.FileData));

                            attachment.FileData = ThumbnailManagement.Resize(image, attachment.FileType, thumbnailWidth, thumbnailHeight);

                            var fileName = string.Format("{0}_{1}x{2}{3}", attachment.Id, thumbnailWidth, thumbnailHeight, attachment.FileType);

                            Stream stream = ByteHelper.ToStream(attachment.FileData);

                            // 创建缩略图
                            ThumbnailManagement.CreateThumbnail(attachment.Id, stream, attachment.FileType, thumbnailWidth, thumbnailHeight);
                        }

                        HttpContext.Current.Response.StatusCode = 200;

                        if (outputType == "uri")
                        {
                            // 输出 uri
                            HttpContext.Current.Response.Write(attachment.VirtualPath.Replace("{uploads}", AttachmentStorageConfigurationView.Instance.VirtualUploadFolder));
                        }
                        else
                        {
                            // 输出 id
                            HttpContext.Current.Response.Write(attachment.Id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // If any kind of error occurs return a 500 Internal Server error
                HttpContext.Current.Response.StatusCode        = 500;
                HttpContext.Current.Response.StatusDescription = "An error occured";
                HttpContext.Current.Response.End();

                KernelContext.Log.Error(ex.Message, ex);
            }
        }