Esempio n. 1
0
        // GET api/goodpoem/?tenant={tenant}&poemKey={poemKey}
        public string Get(int tenant = 0, string poemKey = null)
        {
            string    goodPoem      = "";
            CacheItem goodPoemCache = _cache.GetCacheItem(poemKey);

            if (goodPoemCache != null)
            {
                goodPoem = goodPoemCache.Value.ToString();
            }
            else
            {
                int triesCount = 0;
                while (triesCount < (Config.SERVICE_TIMEOUT / 1000))
                {
                    var value = Sharding.GetInstance().Read(tenant, poemKey);
                    if (!String.IsNullOrEmpty(value))
                    {
                        goodPoem      = value;
                        goodPoemCache = new CacheItem(poemKey, value);
                        _cache.Set(goodPoemCache, new CacheItemPolicy());
                        break;
                    }

                    System.Threading.Thread.Sleep(1000);
                    triesCount++;
                }
            }

            Console.WriteLine("Get: " + goodPoem);
            return(goodPoem);
        }
Esempio n. 2
0
        public void ShardingTest()
        {
            var sharding = new Sharding(_catalogConfig, _databaseConfig, _mockTenantsRepo.Object, _mockHelper.Object);

            Assert.IsNotNull(sharding);
            Assert.IsNotNull(sharding.ShardMapManager);
        }
        // POST api/CustomLogin
        public HttpResponseMessage Post(ChangePassRequest changeRequest)
        {
            string shardKey = Sharding.FindShard(User);
            // NEED TO RECHECK CONTEXT MUST DETERMINE COMPANY -> MUST FIND CORRECT DataBase
            mpbdmContext <Guid> context = new mpbdmContext <Guid>(WebApiConfig.ShardingObj.ShardMap, new Guid(shardKey), WebApiConfig.ShardingObj.connstring);
            Account             account = context.Accounts.Include("User").Where(a => a.User.Email == changeRequest.email).SingleOrDefault();

            if (account != null)
            {
                byte[] incoming = CustomLoginProviderUtils.hash(changeRequest.oldpass, account.Salt);

                if (CustomLoginProviderUtils.slowEquals(incoming, account.SaltedAndHashedPassword))
                {
                    if (changeRequest.password == changeRequest.repass)
                    {
                        byte[] newpass = CustomLoginProviderUtils.hash(changeRequest.password, account.Salt);
                        account.SaltedAndHashedPassword = newpass;
                        context.SaveChanges();
                        return(this.Request.CreateResponse(HttpStatusCode.Created));
                    }
                    return(this.Request.CreateResponse(HttpStatusCode.BadRequest, "Passes don't match"));
                }
            }
            return(this.Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid email or password"));
        }
Esempio n. 4
0
        /// <summary>
        /// Register tenant shard
        /// </summary>
        /// <param name="tenantServerConfig">The tenant server configuration.</param>
        /// <param name="databaseConfig">The database configuration.</param>
        /// <param name="catalogConfig">The catalog configuration.</param>
        /// <param name="resetEventDate">If set to true, the events dates for all tenants will be reset </param>
        public void RegisterTenantShard(TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig, bool resetEventDate)
        {
            //get all database in devtenantserver
            var tenants          = GetAllTenantNames(tenantServerConfig, databaseConfig);
            var connectionString = GetBasicSqlConnectionString(databaseConfig);

            foreach (var tenant in tenants)
            {
                var tenantId = GetTenantKey(tenant);
                if (Sharding.RegisterNewShard(tenant, tenantId, tenantServerConfig, databaseConfig, catalogConfig))
                {
                    // resets all tenants' event dates
                    if (resetEventDate)
                    {
                        #region EF6
                        //use EF6 since execution of Stored Procedure in EF Core for anonymous return type is not supported yet
                        using (var context = new TenantContext(Sharding.ShardMap, tenantId, connectionString))
                        {
                            context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                        }
                        #endregion

                        #region EF core
                        //https://github.com/aspnet/EntityFramework/issues/7032
                        //using (var context = new TenantDbContext(Sharding.ShardMap, tenantId, connectionString))
                        //{
                        //     context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                        //}
                        #endregion
                    }
                }
            }
        }
Esempio n. 5
0
        public void ShardingTest()
        {
            var sharding = new Sharding(_catalogConfig.CatalogDatabase, _connectionString, _mockCatalogRepo, _mockTenantRepo, _mockUtilities.Object);

            Assert.IsNotNull(sharding);
            Assert.IsNotNull(sharding.ShardMapManager);
        }
        public void Register <T, TC, TE, TS>(Func <T> entityFactory)
            where T : ShardedEntity <TC, TE, TS>
            where TC : AbstractCommand
            where TE : AbstractEvent
            where TS : AbstractState
        {
            var proto           = entityFactory.Invoke();
            var entityTypeName  = proto.EntityTypeName;
            var entityClassType = proto.GetType();

            var alreadyRegistered = RegisteredTypeNames.GetOrAdd(entityTypeName, entityClassType);

            if (alreadyRegistered != null && alreadyRegistered != entityClassType)
            {
                throw new InvalidOperationException($"The entity type {nameof(T)} is already registered");
            }

            ReverseRegister.TryAdd(entityClassType, entityTypeName);

            var PassivateAfterIdleTimeout = ActorSystem.Settings.Config.GetConfig("wyvern.persistence")
                                            .GetTimeSpan("passivate-after-idle-timeout", TimeSpan.FromSeconds(100));

            const string snapshotPluginId = "";
            const string journalPluginId  = "";

            JoinCluster(ActorSystem);

            if (Role.ForAll(Cluster.Get(ActorSystem).SelfRoles.Contains))
            {
                ActorSystem.Log.Info("Cluster sharding initialized");
                var props = ShardedEntityActorProps.Create <T, TC, TE, TS>(
                    entityTypeName,
                    Option <string> .None,
                    entityFactory,
                    SnapshotAfter,
                    PassivateAfterIdleTimeout,
                    snapshotPluginId,
                    journalPluginId
                    );

                Sharding.Start(
                    entityTypeName,
                    props,
                    ShardingSettings,
                    ExtractEntityId,
                    ExtractShardId
                    );
            }
            else
            {
                ActorSystem.Log.Warning("Cluster proxy initialized");
                Sharding.StartProxy(
                    entityTypeName,
                    Role.Value,
                    ExtractEntityId,
                    ExtractShardId
                    );
            }
        }
Esempio n. 7
0
        /*
         * 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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        /// <summary>
        /// Register tenant shard
        /// </summary>
        /// <param name="tenantServerConfig">The tenant server configuration.</param>
        /// <param name="databaseConfig">The database configuration.</param>
        /// <param name="catalogConfig">The catalog configuration.</param>
        /// <param name="resetEventDate">If set to true, the events dates for all tenants will be reset </param>
        public static void RegisterAllTenantShard(string tenantServer)
        {
            var tenants = GetAllTenantNames();

            foreach (var tenant in tenants)
            {
                var tenantId = GetTenantKey(tenant.TenantName);
                var result   = Sharding.RegisterNewShard(tenantId, tenant.TenantName, tenantServer);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Initialises the shard map manager and shard map
        /// <para>Also does all tasks related to sharding</para>
        /// </summary>
        private void InitialiseShardMapManager()
        {
            var basicConnectionString = GetBasicSqlConnectionString();
            SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(basicConnectionString)
            {
                DataSource     = DatabaseConfig.SqlProtocol + ":" + CatalogConfig.CatalogServer + "," + DatabaseConfig.DatabaseServerPort,
                InitialCatalog = CatalogConfig.CatalogDatabase
            };

            var sharding = new Sharding(CatalogConfig.CatalogDatabase, connectionString.ConnectionString, _catalogRepository, _tenantRepository, _utilities);
        }
Esempio n. 11
0
        public async void RegisterShardTest()
        {
            _databaseConfig = new DatabaseConfig
            {
                SqlProtocol = SqlProtocol.Default
            };

            var sharding = new Sharding(_catalogConfig.CatalogDatabase, _connectionString, _mockCatalogRepo.Object, _mockTenantRepo.Object, _mockUtilities.Object);
            var result   = await Sharding.RegisterNewShard("TestTenant", 397858529, "localhost", _databaseConfig.DatabaseServerPort, _catalogConfig.ServicePlan);

            Assert.IsTrue(result);
        }
Esempio n. 12
0
        public async void RegisterShardTest()
        {
            _databaseConfig = new DatabaseConfig
            {
                SqlProtocol = SqlProtocol.Default
            };

            var sharding = new Sharding(_catalogConfig.CatalogDatabase, _connectionString, _mockCatalogRepo, _mockTenantRepo, _mockUtilities.Object);
            var result   = await Sharding.RegisterNewShard("TestTenant", 1, TestServer, 1433, _catalogConfig.ServicePlan);

            Assert.True(result);
        }
Esempio n. 13
0
        public async Task Consume(ConsumeContext <PoemFilteringCompleted> context)
        {
            string corrId        = context.Message.corrId;
            string poemGoodLines = context.Message.poemGoodLines;
            int    tenant        = context.Message.tenant;

            Console.WriteLine(corrId);
            Console.WriteLine(poemGoodLines);
            Console.WriteLine(tenant);

            _cache.Set(new CacheItem(corrId, poemGoodLines), new CacheItemPolicy());

            Sharding.GetInstance().Write(tenant, corrId, poemGoodLines);
        }
        /// <summary>
        /// Get a reference to the entity with the given entityId
        /// </summary>
        /// <param name="entityId"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IShardedEntityReference RefFor <T>(string entityId)
            where T : class
        {
            if (!ReverseRegister.TryGetValue(typeof(T), out var entityName))
            {
                throw new InvalidOperationException($"{typeof(T)} not registered.");
            }

            return(new ShardedEntityReference(
                       entityId,
                       Sharding.ShardRegion(PrependRegistryName(entityName)),
                       ActorSystem,
                       AskTimeout
                       ));
        }
Esempio n. 15
0
        public static void Register()
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();

            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

            // To display errors in the browser during development, uncomment the following
            // line. Comment it out again when you deploy your service for production use.
            //config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            //var formatters = config.Formatters;
            //var jsonFormatter = formatters.JsonFormatter;
            //var settings = jsonFormatter.SerializerSettings;
            //settings.Formatting = Formatting.Indented;
            //settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Elastic Scale must ensure data consistency
            //Database.SetInitializer(new mpbdmInitializer());

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Groups, MobileGroup>().ReverseMap();
                cfg.CreateMap <Favorites, MobileFavorites>().ReverseMap();
            });


            // sharding GLOBAL
            ShardingObj = new Sharding();


            //ShardingObj.RegisterNewShard(server, shard1, connectionString, company1);
            //ShardingObj.RegisterNewShard(server, shard2, connectionString, company2);

            // Register the mapping of the tenant to the shard in the shard map.
            // After this step, DDR on the shard map can be used

            /*
             * PointMapping<int> mapping;
             * if (!this.ShardMap.TryGetMappingForKey(key, out mapping))
             * {
             *  this.ShardMap.CreatePointMapping(key, shard);
             * }
             * //*/
        }
Esempio n. 16
0
        public void RegisterShardTest()
        {
            TenantServerConfig tenantServerConfig = new TenantServerConfig
            {
                TenantServer = "localhost"
            };

            _databaseConfig = new DatabaseConfig
            {
                SqlProtocol = SqlProtocol.Default
            };
            _mockHelper.Setup(helper => helper.GetSqlConnectionString(_databaseConfig, _catalogConfig)).Returns(ShardMapManagerConnString);

            var sharding = new Sharding(_catalogConfig, _databaseConfig, _mockTenantsRepo.Object, _mockHelper.Object);
            var result   = Sharding.RegisterNewShard("TestTenant", 397858529, tenantServerConfig, _databaseConfig, _catalogConfig);

            Assert.IsTrue(result);
        }
Esempio n. 17
0
        public async Task RegisterShardTest()
        {
            _databaseConfig = new DatabaseConfig
            {
                SqlProtocol = SqlProtocol.Default
            };

            var sharding = new Sharding(_catalogConfig.CatalogDatabase, _connectionString, _mockCatalogRepo, _mockTenantRepo, _mockUtilities.Object);

            Assert.IsNotNull(sharding);

            var shard = Sharding.CreateNewShard("Test Tenant 1", TestServer, _databaseConfig.DatabaseServerPort, _catalogConfig.ServicePlan);

            Assert.IsNotNull(shard);

            var result = await Sharding.RegisterNewShard(1032943028, _catalogConfig.ServicePlan, shard);

            Assert.IsTrue(result);
        }
Esempio n. 18
0
 /// <summary>
 /// Resolves any mapping differences between the global shard map in the catalog and the local shard map located a tenant database
 /// </summary>
 /// <param name="tenantId">The tenant identifier.</param>
 /// <param name="UseGlobalShardMap">Specifies if the global shard map or the local shard map should be used as the source of truth for resolution.</param>
 public static void ResolveMappingDifferences(int TenantId, bool UseGlobalShardMap = false)
 {
     Sharding.ResolveMappingDifferences(TenantId, UseGlobalShardMap);
 }
Esempio n. 19
0
        public async Task <IActionResult> Post([FromBody] CreateTeamModel model)
        {
            //TODO: Implement Detailed Error Checking
            if (ModelState.IsValid)
            {
                try
                {
                    //TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig
                    var databaseConfig = new DatabaseConfig
                    {
                        DatabasePassword   = _configuration["DatabaseOptions:DatabasePassword"],
                        DatabaseUser       = _configuration["DatabaseOptions:DatabaseUser"],
                        DatabaseServerPort = Int32.Parse(_configuration["DatabaseOptions:DatabaseServerPort"]),
                        SqlProtocol        = SqlProtocol.Tcp,
                        ConnectionTimeOut  = Int32.Parse(_configuration["DatabaseOptions:ConnectionTimeOut"]),
                    };

                    var catalogConfig = new CatalogConfig
                    {
                        ServicePlan     = _configuration["DatabaseOptions:ServicePlan"],
                        CatalogDatabase = _configuration["DatabaseOptions:CatalogDatabase"],
                        CatalogServer   = _configuration["DatabaseOptions:CatalogServer"], // + ".database.windows.net"
                    };

                    var tenantServerConfig = new TenantServerConfig
                    {
                        TenantServer   = _configuration["DatabaseOptions:CatalogServer"],// + ".database.windows.net",
                        TenantDatabase = _configuration["DatabaseOptions:TenantDatabase"],
                    };

                    var team = new Team
                    {
                        Id       = _utilities.GetTenantKey(model.TenantName),
                        Name     = model.TenantName,
                        LogoLink = model.TenantLogoLink,
                    };



                    //Create Shard, Add Team and Register Tenant against shard
                    var shard = Sharding.CreateNewShard(tenantServerConfig.TenantDatabase, tenantServerConfig.TenantServer, databaseConfig.DatabaseServerPort, null);
                    await _tenantRepository.AddTeam(team);

                    var x = await Sharding.RegisterNewShard(team.Id, "", shard);

                    //Add first user to team. Team Owner!
                    var applicationUser = await _userService.GetApplicationUserAsync();

                    var user = new User {
                        ApplicationUserId = applicationUser.Id, Email = applicationUser.Email, UserRole = Role.SuperAdministrator
                    };
                    await _tenantRepository.AddUserToTeam(user, team.Id);


                    return(Ok(new { team_id = team.Id, team_name = team.Name }));
                }
                catch (Exception ex)
                {
                    //TODO: Log Error
                    return(BadRequest(new { Error = ex.Message }));
                }
            }

            return(BadRequest(ModelState));
        }
Esempio n. 20
0
        /// <summary>
        /// Delete tenant shard
        /// </summary>
        /// <param name="tenantName">The Tenant Name.</param>
        public static void DeleteTenantShard(string tenantServer, string tenantName)
        {
            var tenantId = GetTenantKey(tenantName);

            Sharding.DeleteShard(tenantId, tenantName, tenantServer);
        }
Esempio n. 21
0
 /// <summary>
 /// Register tenant shard
 /// </summary>
 /// <param name="tenantServerConfig">The tenant server configuration.</param>
 /// <param name="databaseConfig">The database configuration.</param>
 /// <param name="catalogConfig">The catalog configuration.</param>
 /// <param name="resetEventDate">If set to true, the events dates for all tenants will be reset </param>
 public static void RegisterTenantShard(string tenantServer, string tenantName)
 {
     var tenantId = GetTenantKey(tenantName);
     var result   = Sharding.RegisterNewShard(tenantId, tenantName, tenantServer, true);
 }
Esempio n. 22
0
        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));
        }
Esempio n. 23
0
        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));
        }
Esempio n. 24
0
 public ShardingService(IConfiguration configuration, Sharding shardingProvider)
 {
     _configuration    = configuration;
     _shardingProvider = shardingProvider;
 }
Esempio n. 25
0
        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));
        }
Esempio n. 26
0
 /// <summary>
 /// Initialises the shard map manager and shard map
 /// <para>Also does all tasks related to sharding</para>
 /// </summary>
 private void InitialiseShardMapManager()
 {
     var sharding = new Sharding(CatalogConfig, DatabaseConfig, _tenantsRepository, _helper);
 }