Exemplo n.º 1
0
        public ActionResult SaveImage(string data)
        {
            // Authenticate first
            var authError = Authenticate();

            if (authError != null)
            {
                return(authError);
            }

            // Check to see if type matches
            BaseMessage dataIn = BaseMessage.createFromString(data);

            if (dataIn.type != BaseMessage.API_TYPE_PERSON_IMAGE_ADD)
            {
                return(BaseMessage.createTypeErrorReturn());
            }

            // Everything is in order, start the return
            MobilePostSaveImage mpsi = JsonConvert.DeserializeObject <MobilePostSaveImage>(dataIn.data);

            BaseMessage br = new BaseMessage();

            var imageBytes = Convert.FromBase64String(mpsi.image);

            var person = DbUtil.Db.People.SingleOrDefault(pp => pp.PeopleId == mpsi.id);

            if (person.Picture != null)
            {
                ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.Where(i => i.Id == person.Picture.ThumbId).SingleOrDefault());
                ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.Where(i => i.Id == person.Picture.SmallId).SingleOrDefault());
                ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.Where(i => i.Id == person.Picture.MediumId).SingleOrDefault());
                ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.Where(i => i.Id == person.Picture.LargeId).SingleOrDefault());

                person.Picture.ThumbId  = ImageData.Image.NewImageFromBits(imageBytes, 50, 50).Id;
                person.Picture.SmallId  = ImageData.Image.NewImageFromBits(imageBytes, 120, 120).Id;
                person.Picture.MediumId = ImageData.Image.NewImageFromBits(imageBytes, 320, 400).Id;
                person.Picture.LargeId  = ImageData.Image.NewImageFromBits(imageBytes).Id;
            }
            else
            {
                var newPicture = new Picture();

                newPicture.ThumbId  = ImageData.Image.NewImageFromBits(imageBytes, 50, 50).Id;
                newPicture.SmallId  = ImageData.Image.NewImageFromBits(imageBytes, 120, 120).Id;
                newPicture.MediumId = ImageData.Image.NewImageFromBits(imageBytes, 320, 400).Id;
                newPicture.LargeId  = ImageData.Image.NewImageFromBits(imageBytes).Id;

                person.Picture = newPicture;
            }

            DbUtil.Db.SubmitChanges();

            br.error = 0;
            br.data  = "Image updated.";
            br.id    = mpsi.id;
            br.count = 1;

            return(br);
        }
        public ActionResult SaveImage(string data)
        {
            // Authenticate first
            var result = AuthenticateUser();

            if (!result.IsValid)
            {
                return(AuthorizationError(result));
            }

            BaseMessage         dataIn = BaseMessage.createFromString(data);
            MobilePostSaveImage mpsi   = JsonConvert.DeserializeObject <MobilePostSaveImage>(dataIn.data);

            BaseMessage br = new BaseMessage();

            var imageBytes = Convert.FromBase64String(mpsi.image);

            var person = DbUtil.Db.People.SingleOrDefault(pp => pp.PeopleId == mpsi.id);

            if (person.Picture != null)
            {
                if (ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.ThumbId) != null)
                {
                    ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.ThumbId));
                }

                if (ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.ThumbId) != null)
                {
                    ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.SmallId));
                }

                if (ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.ThumbId) != null)
                {
                    ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.MediumId));
                }

                if (ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.ThumbId) != null)
                {
                    ImageData.DbUtil.Db.Images.DeleteOnSubmit(ImageData.DbUtil.Db.Images.SingleOrDefault(i => i.Id == person.Picture.LargeId));
                }

                person.Picture.ThumbId  = Image.NewImageFromBits(imageBytes, 50, 50).Id;
                person.Picture.SmallId  = Image.NewImageFromBits(imageBytes, 120, 120).Id;
                person.Picture.MediumId = Image.NewImageFromBits(imageBytes, 320, 400).Id;
                person.Picture.LargeId  = Image.NewImageFromBits(imageBytes).Id;
            }
            else
            {
                var newPicture = new Picture();

                newPicture.ThumbId  = Image.NewImageFromBits(imageBytes, 50, 50).Id;
                newPicture.SmallId  = Image.NewImageFromBits(imageBytes, 120, 120).Id;
                newPicture.MediumId = Image.NewImageFromBits(imageBytes, 320, 400).Id;
                newPicture.LargeId  = Image.NewImageFromBits(imageBytes).Id;

                person.Picture = newPicture;
            }

            DbUtil.Db.SubmitChanges();

            br.error = 0;
            br.data  = "Image updated.";
            br.id    = mpsi.id;
            br.count = 1;

            return(br);
        }