Esempio n. 1
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. 2
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. 3
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. 4
0
        protected override void Seed(mpbdmContext <Guid> context)
        {
            /*
             * COMPANIES
             */
            string[]         companiesArray  = new string[2];
            int              count_companies = 0;
            List <Companies> companies       = new List <Companies>
            {
                new Companies {
                    Id = "2c8c7462-d6ca-429c-9021-21203bea780d", Name = "Sieben", Address = "Αθήνα", Email = "*****@*****.**"
                },
                new Companies {
                    Id = "48344df7-4837-4144-b1c8-6470aeb9dae4", Name = "Coca-Cola", Address = "Αθήνα", Email = "*****@*****.**"
                },
            };

            foreach (Companies company in companies)
            {
                companiesArray[count_companies] = company.Id;
                context.Set <Companies>().Add(company);
                count_companies++;
            }


            /*
             * USERS
             */
            Users[]      usersArray  = new Users[5];
            int          count_users = 0;
            List <Users> users       = new List <Users>
            {
                new Users {
                    Id = "Google:105535740556221909032", FirstName = "Στέφανος", LastName = "Λιγνός", Email = "*****@*****.**", CompaniesID = companiesArray[0]
                },
                new Users {
                    Id = "Google:108551266495594343585", FirstName = "Μάνος", LastName = "Ψαράκης", Email = "*****@*****.**", CompaniesID = companiesArray[1]
                },
                new Users {
                    Id = "Facebook:762253580534078", FirstName = "Nikos", LastName = "Atlas", Email = "*****@*****.**", CompaniesID = companiesArray[0]
                },
                new Users {
                    Id = "custom:nikatlas", FirstName = "Nikatlas", LastName = "Atlas", Email = "*****@*****.**", CompaniesID = companiesArray[0]
                },
                new Users {
                    Id = "custom:steflignos", FirstName = "Stefanos", LastName = "Lignos", Email = "*****@*****.**", CompaniesID = companiesArray[0]
                },
            };

            foreach (Users user in users)
            {
                usersArray[count_users] = user;
                context.Set <Users>().Add(user);
                count_users++;
            }

            byte[]  salt = CustomLoginProviderUtils.generateSalt();
            Account nik  = new Account
            {
                Id       = Guid.NewGuid().ToString(),
                Username = "******",
                Salt     = salt,
                SaltedAndHashedPassword = CustomLoginProviderUtils.hash("123321qwe", salt),
                User = usersArray[3]
            };
            Account stef = new Account
            {
                Id       = Guid.NewGuid().ToString(),
                Username = "******",
                Salt     = salt,
                SaltedAndHashedPassword = CustomLoginProviderUtils.hash("123321qwe", salt),
                User = usersArray[4]
            };

            List <Account> accs = new List <Account>
            {
                nik, stef
            };

            foreach (Account acc in accs)
            {
                context.Set <Account>().Add(acc);
            }

            /*
             * GROUPS
             */
            string[]      groupsArray  = new string[8];
            int           count_groups = 0;
            List <Groups> groups       = new List <Groups>
            {
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Research & development", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[0]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Digital Marketing", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[0]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Human Resources", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[0]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Sales & Marketing", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[0]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Other", Address = "Στο Πουθενά", Visible = true, CompaniesID = companiesArray[0]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Human Resources", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[1]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "Sales & Marketing", Address = "Αθήνα", Visible = true, CompaniesID = companiesArray[1]
                },
                new Groups {
                    Id = Guid.NewGuid().ToString(), Name = "None", Address = "Στο Πουθενά", Visible = true, CompaniesID = companiesArray[1]
                },
            };

            foreach (Groups group in groups)
            {
                groupsArray[count_groups] = group.Id;
                context.Set <Groups>().Add(group);
                count_groups++;
            }


            /*
             * CONTACTS
             */
            string[]        contactsArray  = new string[22];
            int             count_contacts = 0;
            List <Contacts> contacts       = new List <Contacts>
            {
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Μαρία", LastName = "Κουνάκη", Phone = "6974767832", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[1]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Φίλιππος", LastName = "Κολέτσης", Phone = "6973245684", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[1]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Γιώργος", LastName = "Αργυράκης", Phone = "6974532123", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[1]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Μάρα", LastName = "Κυμπιζη", Phone = "6932456789", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[1]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Κωνσταντίνος", LastName = "Τζαβάρας", Phone = "697356745", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[3]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Αντωνία", LastName = "Ρεμούνδου", Phone = "6975634251", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[3]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Ολγα", LastName = "Ζούμπου", Phone = "6934567324", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[3]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Νικόλ", LastName = "Μπουζούκου", Phone = "6943245622", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[2]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Στέφανος", LastName = "Λιγνός", Phone = "6974053682", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Νίκος", LastName = "Καλαβρουζιώτης", Phone = "6982108999", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Φοίβος", LastName = "Σταμόπουλος", Phone = "6945849202", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Μάνος", LastName = "Ψαράκης", Phone = "6942466270", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[7]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Βασιλική", LastName = "Τραχάνη", Phone = "6938627519", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Ελένη", LastName = "Παπανικολάου", Phone = "6943567743", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Γιώργος", LastName = "Σαχπατζίδης", Phone = "6945823948", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[0]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Κωνσταντίνα", LastName = "Παπαδοπούλου", Phone = "6981222331", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[5]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Γιάννης", LastName = "Παντζόπουλος", Phone = "6974567342", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[5]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Γιάννης", LastName = "Ρέγκας", Phone = "6946578423", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[5]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Μαρία", LastName = "Σκαλκούτα", Phone = "6933745862", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[5]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Κωνσταντίνος", LastName = "Τζάνης", Phone = "6975678421", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[6]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Αντωνία", LastName = "Ρέβη", Phone = "6981234767", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[6]
                },
                new Contacts {
                    Id = Guid.NewGuid().ToString(), FirstName = "Όλγα", LastName = "Ζούνη", Phone = "6943567422", Email = "*****@*****.**", Visible = true, GroupsID = groupsArray[6]
                },
            };

            foreach (Contacts contact in contacts)
            {
                contactsArray[count_contacts] = contact.Id;
                context.Set <Contacts>().Add(contact);
                count_contacts++;
            }


            /*
             * FAVORITES
             */
            List <Favorites> favorites = new List <Favorites>
            {
                new Favorites {
                    Id = Guid.NewGuid().ToString(), Visible = true, UsersID = usersArray[0].Id, ContactsID = contactsArray[0]
                },
                new Favorites {
                    Id = Guid.NewGuid().ToString(), Visible = true, UsersID = usersArray[3].Id, ContactsID = contactsArray[1]
                },
                new Favorites {
                    Id = Guid.NewGuid().ToString(), Visible = true, UsersID = usersArray[4].Id, ContactsID = contactsArray[7]
                },
                new Favorites {
                    Id = Guid.NewGuid().ToString(), Visible = true, UsersID = usersArray[0].Id, ContactsID = contactsArray[8]
                },
                new Favorites {
                    Id = Guid.NewGuid().ToString(), Visible = true, UsersID = usersArray[1].Id, ContactsID = contactsArray[16]
                },
            };

            foreach (Favorites favorite in favorites)
            {
                context.Set <Favorites>().Add(favorite);
            }



            base.Seed(context);
        }