Пример #1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //变量
            HttpPostedFile imgFile;
            int groupId = 0;
            long accountId = 0;
            long albumId = 0;
            string noticeContent = String.Empty;
            string gId = "";
            string alId = "";
            string aId = "";

            noticeContent = context.Request.QueryString["noticeContent"].ToString().Trim();
            aId = context.Request.QueryString["accountId"].ToString();
            alId = context.Request.QueryString["albumId"].ToString();
            gId = context.Request.QueryString["groupId"].ToString();

            if (context.Request.QueryString["noticeContent"] != null)
            {
                noticeContent = context.Server.UrlDecode(noticeContent);
            }

            if (!long.TryParse(aId, out accountId))
            {
                return;
            }

            if (!long.TryParse(alId, out albumId))
            {
                return;
            }

            if (!int.TryParse(gId, out groupId))
            {
                return;
            }

            CY.UME.Core.Business.Account account = CY.UME.Core.Business.Account.Load(accountId);
            if (account == null)
            {
                return;
            }

            if (account.Id == 0)
            {
                context.Response.Write("用户登录超时,请重新登录");
                return;
            }

            CY.UME.Core.Business.Group group = CY.UME.Core.Business.Group.Load(groupId);

            if (group == null)
            {
                return;
            }

            if (group.AddPicturePermission == 1 && !group.CheckIsGroupMember(account))
            {
                return;
            }
            else if (group.AddPicturePermission == 2 && !group.CheckIsManager(account))
            {
                return;
            }

            CY.UME.Core.Business.Album album = CY.UME.Core.Business.Album.Load(albumId);

            if (album == null || album.Id == 0)
            {
                album = group.CreateGroupAlbum();
            }

            //获取上传的文件并保存
            if (context.Request.Files.Count > 0)
            {

                imgFile = context.Request.Files[0];

                if (imgFile == null)
                {
                    return;
                }

                string imgExtention = CY.Utility.Common.FileUtility.GetFileExtension(imgFile.FileName).ToLower();
                string imgName = CY.Utility.Common.FileUtility.GetFileName(imgFile.FileName);

                string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;
                string dirPath = appPath + "/Content/Group/Album/" + groupId + "/";
                string fileName = "/Content/Group/Album/" + groupId + "/" + DateTime.Now.ToString("yyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();//相册图片以加时间命名

                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath);
                if (!di.Exists)
                {
                    di.Create();
                }

                string bImgName = appPath + fileName + "_big" + imgExtention;
                string mImgName = appPath + fileName + "_middle" + imgExtention;
                string sImgName = appPath + fileName + "_small" + imgExtention;

                imgFile.SaveAs(appPath + fileName + imgExtention);//临时保存原始图片

                Bitmap bmp = new Bitmap(appPath + fileName + imgExtention);
                int width = bmp.Width;
                int height = bmp.Height;

                bmp.Dispose();

                if (height <= width && width >= 600)
                {
                    height = Convert.ToInt32(width > 600 ? (600f / width) * height : height);
                    width = 600;
                }
                else if (width < height && height > 600)
                {
                    width = Convert.ToInt32(height > 600 ? (600f / height) * width : height);
                    height = 600;
                }

                // 将原始图压缩为大缩略图
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, bImgName, width, height);
                }

                if (width > 200)
                {
                    height = Convert.ToInt32(width > 200 ? (200f / width) * height : height);
                    width = 200;
                }

                // 生成图片(好友上传图片最新通知中显示)
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, sImgName, width, height);
                }

                if (height <= width && width >= 100)
                {
                    height = Convert.ToInt32(width > 100 ? (100f / width) * height : height);
                    width = 100;
                }
                else if (width < height && height > 100)
                {
                    width = Convert.ToInt32(height > 100 ? (100f / height) * width : height);
                    height = 100;
                }

                // 将大图片压缩为中等缩略图
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, mImgName, width, height);
                }

                try
                {
                    File.Delete(appPath + fileName + imgExtention);
                }
                catch
                {
                    ;
                }

                //将路径及图片信息保存到数据库
                CY.UME.Core.Business.Picture pic = new Picture();

                pic.AlbumId = album.Id;
                pic.BigPath = fileName + "_big" + imgExtention;
                pic.DateCreated = DateTime.Now;
                pic.MiddlePath = fileName + "_middle" + imgExtention;
                pic.Name = CY.Utility.Common.StringUtility.HTMLEncode(imgName.Substring(0, imgName.Length > 30 ? 30 : imgName.Length));
                pic.SmallPath = fileName + "_small" + imgExtention;

                pic.Save();

                //保存pictureextend
                CY.UME.Core.Business.PictureExtend pe = new PictureExtend();

                pe.Id = pic.Id;
                pe.AccountId = account.Id;
                pe.ActivityId = "group";
                pe.ActivityPicId = "0";

                pe.Save();

                context.Response.Write("1");

                if (group.ViewPermission == 1)
                {
                    IList<CY.UME.Core.Business.Friendship> fsList = account.GetFriendships();

                    foreach (CY.UME.Core.Business.Friendship fs in fsList)
                    {
                        List<CY.UME.Core.Business.Notice> noticeList = CY.UME.Core.Business.Notice.GetAllNoticeByAccountAndFriendAndType(account, fs.Friend, "grouppicture", false);

                        foreach (CY.UME.Core.Business.Notice notice in noticeList)
                        {

                            if (notice != null && DateTime.Compare(DateTime.Now, notice.DateCreated.AddMinutes(10)) <= 0)
                            {
                                string strPicTemp = notice.InstanceId.Split(',')[0];
                                long picIdTemp = 0;

                                if (long.TryParse(strPicTemp, out picIdTemp))
                                {
                                    CY.UME.Core.Business.Picture picTemp = CY.UME.Core.Business.Picture.Load(picIdTemp);

                                    if (picTemp != null && picTemp.AlbumId == pic.AlbumId)
                                    {
                                        notice.InstanceId += "," + pic.Id;

                                        notice.Save();

                                        break;
                                    }
                                }
                            }

                            CY.UME.Core.Business.Notice n = new CY.UME.Core.Business.Notice();

                            n.AccountId = fs.FriendId;
                            n.AuthorId = fs.AccountId;
                            n.Content = "上传了新照片";
                            n.DateCreated = DateTime.Now;
                            n.InstanceId = pic.Id.ToString();
                            n.IsReaded = false;
                            n.Type = "grouppicture";

                            n.Save();
                        }
                    }
                }
            }
        }
Пример #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            HttpPostedFile imgFile;
            long accountId = 0;
            CY.UME.Core.Business.Account account=CY.UME.Core.Global.GetCurrentAccount();
            if (account == null)
                {
                    context.Response.Write("{success:false,msg:'用户登录超时'}");
                    return;
                }
            accountId = account.Id;
            CY.UME.Core.Business.Album album=account.GetMyAvatarAlbum(); // 取得当前用户的头像相册
            string picname = context.Request.Form["namepic"];
            string picremar = context.Request.Form["remark"];

            imgFile=context.Request.Files[0];

            string imgExtention = CY.Utility.Common.FileUtility.GetFileExtension(imgFile.FileName).ToLower();
            string imgName = CY.Utility.Common.FileUtility.GetFileName(imgFile.FileName);
            string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;
            string dirPath = appPath + "/Content/Album/" + accountId + "/";
            string fileName ="/Content/Album/" + accountId + "/" + DateTime.Now.ToString("yyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();//相册图片以加时间命名
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath);
                    if (!di.Exists)
                    {
                        di.Create();
                    }

            string bImgName = appPath + fileName + "_big" + imgExtention;
            string mImgName = appPath + fileName + "_middle" + imgExtention;
            string sImgName = appPath + fileName + "_small" + imgExtention;

            imgFile.SaveAs(appPath + fileName + imgExtention);//临时保存原始图片

            Bitmap bmp = new Bitmap(appPath + fileName + imgExtention);
            int width = bmp.Width;
            int height = bmp.Height;

            bmp.Dispose();

            if (height <= width && width>=600)
            {
              height = Convert.ToInt32(width > 600 ? (600f / width) * height : height);
              width = 600;
            }
            else if(width<height && height>600)
            {
             width = Convert.ToInt32(height > 600 ? (600f / height) * width : height);
             height = 600;
            }

                    // 将原始图压缩为大缩略图
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, bImgName, width, height);
                    }

                    if (width > 200)
                    {
                        height = Convert.ToInt32(width > 200 ? (200f / width) * height : height);
                        width = 200;
                    }

                    // 生成图片(好友上传图片最新通知中显示)
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, sImgName, width, height);
                    }

                    if (height <= width && width>=100)
                    {
                        height = Convert.ToInt32(width > 100 ? (100f / width) * height : height);
                        width = 100;
                    }
                    else if(width<height && height>100)
                    {
                        width = Convert.ToInt32(height > 100 ? (100f / height) * width : height);
                        height = 100;
                    }

                    // 将大图片压缩为中等缩略图
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, mImgName, width, height);
                    }

                    try
                    {
                        File.Delete(appPath + fileName +imgExtention);
                    }
                    catch
                    {
                        ;
                    }

                   //更换相册封面

                    if (album.PhotoCount == 0 || album.CoverPath.Trim().Length==0)
                    {
                        album.CoverPath = fileName + "_middle" + imgExtention;
                        album.Save();
                    }

                    //将路径及图片信息保存到数据库
                    CY.UME.Core.Business.Picture pic = new Picture();

                    pic.AlbumId = album.Id;
                    pic.BigPath = fileName + "_big" +imgExtention;
                    pic.DateCreated = DateTime.Now;
                    pic.MiddlePath = fileName+"_middle" + imgExtention;
                    pic.Name =CY.Utility.Common.StringUtility.HTMLEncode(imgName.Substring(0,imgName.Length>30?30:imgName.Length));
                    pic.SmallPath = fileName + "_small" + imgExtention;

                    pic.Save();

                    //添加通知
                    account.SendNoticeToAllFriendAndFollower("picture", "上传了新照片", pic.Id.ToString());

                    /********************第一天活动**************************/
                    try
                    {
                        if (context.Request.QueryString["IsFirstDay"] != null)
                        {
                            string isFirstDayParam = context.Request.QueryString["IsFirstDay"].ToString();
                            if (isFirstDayParam == "myfirstday")
                            {
                                CY.UME.Core.Business.PictureExtend picExtend = new PictureExtend();

                                picExtend.AccountId = account.Id;
                                picExtend.ActivityId = "myfirstday";
                                picExtend.Id = pic.Id;

                                picExtend.Save();
                            }
                        }
                    }
                    catch { ;}
                    /********************第一天活动结束************************/
                    context.Response.Write("{success:true,infor:'图片上传成功!'}");
        }