private void SaveUploadedPin(HttpContext context) { int board = int.Parse(context.Request.Params["board"]); string desc = context.Request.Params["desc"]; string img = context.Request.Params["img"]; IHash fnv1a = HashFactory.Hash64.CreateFNV1a(); IHash crc64 = HashFactory.Checksum.CreateCRC64b(); IHash murmur2 = HashFactory.Hash64.CreateMurmur2(); Uri url = new Uri(img); string image = url.Segments.Last(); string fp = Path.Combine(Common.Temp, Common.UserID.ToString(), image); FileInfo fInfo = new FileInfo(fp); string destRel = Common.UploadedImageRelPath + Common.UserID; string dest = Common.UploadedImagePath + Common.UserID; string destFileName = Common.MoveAndRenameFileIfExists(fInfo, dest); string relPath = Common.UserID + "/" + destFileName; byte[] _d = File.ReadAllBytes(Path.Combine(dest, destFileName)); ulong _crc64; ulong _murmur2; ulong _fnv1a; short height, width; int imageID = 0; CalcHash(_d, out _crc64, out _murmur2, out _fnv1a, out height, out width); using (TransactionScope scope = new TransactionScope()) { try { Data.dbml.DataContext _context = this.GetDataContext2; var imgtb = _context.Images.Where(o => o.FNV1a == _fnv1a && o.CRC64 == _crc64 && o.MURMUR2 == _murmur2).FirstOrDefault(); var theboard = _context.Boards.Where(o => o.ID == board).Single(); if (imgtb == null) //only one image entry can exists { Data.dbml.Images i = new Data.dbml.Images(); i.CRC64 = _crc64; i.MURMUR2 = _murmur2; i.FNV1a = _fnv1a; i.Image_Height = height; i.Image_Width = width; i.Uploaded = true; i.Tagged = true; i.RelativeImage_Path = relPath; _context.Images.InsertOnSubmit(i); _context.SubmitChanges(); imageID = i.ID; } else { imageID = imgtb.ID; } Data.dbml.BoardsImagesMapping bim = new Data.dbml.BoardsImagesMapping(); bim.BoardID = board; bim.ImageID = imageID; bim.Image_Title = desc; bim.UserID = Common.UserID; _context.BoardsImagesMapping.InsertOnSubmit(bim); Data.dbml.CategoryImagesMapping cim = new Data.dbml.CategoryImagesMapping(); cim.CategoryID = theboard.CatID; cim.ImageID = imageID; _context.CategoryImagesMapping.InsertOnSubmit(cim); UpdatePoints(Common.PointsNewPin); _context.SubmitChanges(); UpdateUPCount(); scope.Complete(); context.WriteJsonP(JsonConvert.SerializeObject(new { PinID = ((ulong)bim.ID + _crc64).ToString() })); } catch (Exception e) { throw; } finally { scope.Dispose(); } } }
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; int imageID = 0; CalcHash(_d, out _crc64, out _murmur2, out _fnv1a, out height, out width); using (TransactionScope scope = new TransactionScope()) { try { Data.dbml.DataContext _context = this.GetDataContext2; var imgtb = _context.Images.Where(o => o.FNV1a == _fnv1a).FirstOrDefault(); var theboard = _context.Boards.Where(o => o.ID == board).Single(); if (imgtb == null) //only one image entry can exists { Data.dbml.Images i = new Data.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; _context.Images.InsertOnSubmit(i); _context.SubmitChanges(); imageID = i.ID; } else { imageID = imgtb.ID; } Data.dbml.BoardsImagesMapping bim = new Data.dbml.BoardsImagesMapping(); bim.BoardID = board; bim.ImageID = imageID; bim.Image_Title = desc; bim.Source = img; bim.UserID = Common.UserID.Value; _context.BoardsImagesMapping.InsertOnSubmit(bim); Data.dbml.CategoryImagesMapping cim = new Data.dbml.CategoryImagesMapping(); cim.CategoryID = theboard.CatID; cim.ImageID = imageID; _context.CategoryImagesMapping.InsertOnSubmit(cim); UpdatePoints(Common.PointsNewPin); _context.SubmitChanges(); UpdateUPCount(); scope.Complete(); context.WriteJsonP(JsonConvert.SerializeObject(new { PinID = (_crc64 + (ulong)bim.ID).ToString() })); } catch (Exception e) { throw; } finally { scope.Dispose(); } } }