Exemplo n.º 1
0
        private void SaveAddedPin(HttpContext context)
        {
            int          board            = int.Parse(context.Request.Params["board"]);
            string       desc             = context.Request.Params["desc"];
            string       img              = context.Request.Params["img"];
            string       destRel          = Common.UploadedImageRelPath + Common.UserID;
            string       dest             = Common.UploadedImagePath + Common.UserID;
            string       destFileName     = Guid.NewGuid().ToString() + ".jpg";
            string       destFileFullPath = Path.Combine(dest, destFileName);
            WebClient    client           = new WebClient();
            MemoryStream ms = new MemoryStream(client.DownloadData(img));

            using (Image image = Image.FromStream(ms))
            {
                if (!Directory.Exists(dest))
                {
                    Directory.CreateDirectory(dest);
                }
                image.Save(destFileFullPath, ImageFormat.Jpeg);
            }
            string relPath = Common.UserID + "/" + destFileName;

            byte[] _d = File.ReadAllBytes(destFileFullPath);
            ulong  _crc64;
            ulong  _murmur2;
            ulong  _fnv1a;
            short  height, width;

            CalcHash(_d, out _crc64, out _murmur2, out _fnv1a, out height, out width);
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    Lipstick.dbml.Images i = new Lipstick.dbml.Images();
                    i.CRC64              = _crc64;
                    i.MURMUR2            = _murmur2;
                    i.Tagged             = true;
                    i.FNV1a              = _fnv1a;
                    i.Image_Height       = height;
                    i.Image_Width        = width;
                    i.Uploaded           = true;
                    i.RelativeImage_Path = relPath;
                    Lipstick.dbml.LipstickDataContext _context = this.GetLipstickContext2;
                    _context.Images.InsertOnSubmit(i);
                    _context.SubmitChanges();
                    Lipstick.dbml.BoardsImagesMapping bim = new Lipstick.dbml.BoardsImagesMapping();
                    bim.BoardID     = board;
                    bim.ImageID     = i.ID;
                    bim.Image_Title = desc;
                    bim.Source      = img;
                    bim.UserID      = Common.UserID.Value;
                    _context.BoardsImagesMapping.InsertOnSubmit(bim);
                    _context.SubmitChanges();
                    scope.Complete();
                    context.Response.Write(JsonConvert.SerializeObject(new { PinID = (_crc64 + (ulong)bim.ID).ToString() }));
                }
                catch (Exception e)
                {
                    throw;
                }
                finally
                {
                    scope.Dispose();
                }
            }
        }