Exemplo n.º 1
0
        public void SaveData()
        {
            var userInDb = dbContext.Users.Single(u => u.Id == UserId);

            if (!string.IsNullOrEmpty(PhoneNumber))
            {
                userInDb.PhoneNumber = PhoneNumber;
                IsPhoneNumberValid   = true;
            }
            if (!string.IsNullOrEmpty(DSNPhone))
            {
                userInDb.DSNPhone = DSNPhone;
                IsDsnPhoneValid   = true;
            }
            if (!string.IsNullOrEmpty(CarePointName))
            {
                userInDb.CarePointName = CarePointName;
                IsCarepointNameValid   = true;
            }
            if (!string.IsNullOrEmpty(Bio))
            {
                userInDb.Bio = Bio;
                IsBioValid   = true;
            }
            if (PicFile != null && PicFile.ContentLength > 0)
            {
                string fileExt = Path.GetExtension(PicFile.FileName).ToLower();
                if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".jpeg" || fileExt == ".gif")
                {
                    string originalFileName = PicFile.FileName;
                    string picGuid          = Guid.NewGuid().ToString().Replace("-", "");
                    string newFileName      = string.Concat(picGuid, "-id-", UserId);
                    string imageFilePath    = Path.Combine(HttpContext.Current.Server.MapPath(@"~\Content\Images\Profile_Pics"), newFileName + fileExt);
                    //Compress & archive old pictures
                    if (userInDb.ProfilePic != null)
                    {
                        string        oldPicUrl = HttpContext.Current.Server.MapPath(userInDb.ProfilePic);
                        FileInfo      oldFile   = new FileInfo(oldPicUrl);
                        FileInfo      zipFile;
                        DirectoryInfo archiveDestination = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Content\Images\Profile_Pics\ArchivedPics"));
                        using (FileStream stream = oldFile.OpenRead())
                        {
                            using (FileStream zipStream = File.Create(oldFile.FullName + ".gz"))
                            {
                                using (GZipStream compressionStream = new GZipStream(zipStream, CompressionMode.Compress))
                                {
                                    stream.CopyTo(compressionStream);
                                    zipFile = new FileInfo(oldFile.FullName + ".gz");
                                }
                            }
                            zipFile.MoveTo(archiveDestination.FullName + Path.DirectorySeparatorChar + Path.GetFileName(zipFile.FullName));
                            File.Delete(oldFile.FullName + ".gz");
                        }
                        File.Delete(oldFile.FullName);
                    }
                    PicFile.SaveAs(imageFilePath);
                    userInDb.ProfilePic = Path.Combine(@"\Content\Images\Profile_Pics", newFileName + fileExt);
                    ProfilePicUrl       = userInDb.ProfilePic;
                }
            }
            dbContext.SaveChanges();
        }