/* * Dont be misleading it get the shardKey we need on each request * BUT sets the DomainManager's context to look at the correct shard */ private string getShardKey() { string shardKey = Sharding.FindShard(User); db = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring); ((EntityDomainManager <Companies>)DomainManager).Context = db; return(shardKey); }
private string getShardKey() { string shardKey = Sharding.FindShard(User); db = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring); ((FavoritesDomainManager)DomainManager).setContext(db); ((FavoritesDomainManager)DomainManager).User = User; return(shardKey); }
public async Task <HttpResponseMessage> Post(string contactId) { string shardKey = Sharding.FindShard(User); mpbdmContext <Guid> db = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring); // Security issue check company Contacts contact = db.Set <Contacts>().Include("Groups").Where(s => s.Id == contactId && s.Groups.CompaniesID == shardKey).FirstOrDefault(); if (contact == null) { this.Request.CreateResponse(HttpStatusCode.BadRequest, "Contact doesnt't exist!"); } CloudStorageAccount acc = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["Azure"].ConnectionString); CloudBlobClient blobClient = acc.CreateCloudBlobClient(); CloudBlobContainer photoContainer = blobClient.GetContainerReference("images"); await photoContainer.CreateIfNotExistsAsync(); var provider = new AzureBlobMultipartFormDataStreamProvider(photoContainer); await this.Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.FileData) { //the LocalFileName is going to be the absolute Uri of the blob (see GetStream) //use it to get the blob info to return to the client var blob = await photoContainer.GetBlobReferenceFromServerAsync(file.LocalFileName); var fileNameGuid = Guid.NewGuid().ToString(); // Copy to get new URL ICloudBlob newBlob = null; if (blob is CloudBlockBlob) { newBlob = photoContainer.GetBlockBlobReference(fileNameGuid); } else { newBlob = photoContainer.GetPageBlobReference(fileNameGuid); } //Initiate blob copy await newBlob.StartCopyFromBlobAsync(blob.Uri); ////Now wait in the loop for the copy operation to finish //while (true) //{ // newBlob.FetchAttributes(); // if (newBlob.CopyState.Status != CopyStatus.Pending) // { // break; // } // //Sleep for a second may be // System.Threading.Thread.Sleep(1000); //} blob.Delete(); await newBlob.FetchAttributesAsync(); string url = newBlob.Uri.ToString(); //// DELETING ANY OLD BLOBS //if (contact.ImageUrl != null) //{ // var oldBlob = photoContainer.GetBlobReferenceFromServer(contact.ImageUrl); // oldBlob.Delete(); //} //////////////////////////// //contact.ImageUrl = url; contact.ImageUrl = newBlob.Name.ToString(); try { db.SaveChanges(); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "CannotSaveChanges!")); } } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task <HttpResponseMessage> Post() { string shardKey = Sharding.FindShard(User); mpbdmContext <Guid> db = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring); // Security issue check company var user = User as ServiceUser; Users userEntity = db.Set <Users>().Where(s => s.Id == user.Id).FirstOrDefault(); if (userEntity == null) { this.Request.CreateResponse(HttpStatusCode.BadRequest, "User doesnt't exist!"); } CloudStorageAccount acc = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["Azure"].ConnectionString); CloudBlobClient blobClient = acc.CreateCloudBlobClient(); CloudBlobContainer photoContainer = blobClient.GetContainerReference("images"); await photoContainer.CreateIfNotExistsAsync(); var provider = new AzureBlobMultipartFormDataStreamProvider(photoContainer); await this.Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.FileData) { var blob = await photoContainer.GetBlobReferenceFromServerAsync(file.LocalFileName); var fileNameGuid = Guid.NewGuid().ToString(); ICloudBlob newBlob = null; if (blob is CloudBlockBlob) { newBlob = photoContainer.GetBlockBlobReference(fileNameGuid); } else { newBlob = photoContainer.GetPageBlobReference(fileNameGuid); } await newBlob.StartCopyFromBlobAsync(blob.Uri); blob.Delete(); await newBlob.FetchAttributesAsync(); string url = newBlob.Uri.ToString(); //// DELETING ANY OLD BLOBS //if (userEntity.ImageUrl != null) //{ // var oldBlob = photoContainer.GetBlobReferenceFromServer(userEntity.ImageUrl); // oldBlob.Delete(); //} //////////////////////////// // UPDATE imageUrl of user //userEntity.ImageUrl = url; userEntity.ImageUrl = newBlob.Name.ToString(); try { db.SaveChanges(); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "CannotSaveChanges!")); } } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task <HttpResponseMessage> Post(string groupsId) { CloudStorageAccount acc = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["Azure"].ConnectionString); CloudBlobClient blobClient = acc.CreateCloudBlobClient(); CloudBlobContainer photoContainer = blobClient.GetContainerReference("temp"); await photoContainer.CreateIfNotExistsAsync(); var provider = new AzureBlobMultipartFormDataStreamProvider(photoContainer); await this.Request.Content.ReadAsMultipartAsync(provider); //var photos = new List<PhotoViewModel>(); foreach (var file in provider.FileData) { //the LocalFileName is going to be the absolute Uri of the blob (see GetStream) //use it to get the blob info to return to the client var blob = await photoContainer.GetBlobReferenceFromServerAsync(file.LocalFileName); await blob.FetchAttributesAsync(); string url = blob.Uri.ToString(); //provider.GetStream(this.RequestContext); //FileStream fs = new FileStream(); //blob.DownloadToStream(fs); //FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read); //HttpClient cl = new HttpClient(); Stream ss = new MemoryStream(); blob.DownloadToStream(ss); HSSFWorkbook templateWorkbook = new HSSFWorkbook(ss); HSSFSheet sheet = (HSSFSheet)templateWorkbook.GetSheet("Sheet1"); string shardKey = Sharding.FindShard(User); mpbdmContext <Guid> db = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring); for (int i = 1; true; i++) { var row = sheet.GetRow(i); if (row == null) { break; } Contacts cont = new Contacts(); cont.FirstName = row.GetCell(0).RichStringCellValue.String; cont.LastName = row.GetCell(1).RichStringCellValue.String; cont.Email = row.GetCell(2).RichStringCellValue.String; cont.Phone = row.GetCell(3).NumericCellValue.ToString(); cont.GroupsID = (groupsId == "valueUndefined") ? row.GetCell(4).RichStringCellValue.String : groupsId; cont.Id = Guid.NewGuid().ToString(); cont.Deleted = false; cont.Visible = true; var chk = db.Set <Contacts>().Where(s => s.Email == cont.Email && s.LastName == cont.LastName && s.Groups.Companies.Id == shardKey).FirstOrDefault(); if (chk != null) { continue; } db.Set <Contacts>().Add(cont); } try { db.SaveChanges(); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Propably the Foreign Key GroupId is wrong on some of your Contacts!!! Make sure the groupId exists!")); } } return(Request.CreateResponse(HttpStatusCode.OK)); }