Exemple #1
0
        public int Like(int postId, int accountId)
        {
            Entities.Like iLikie = new Like();
            iLikie.AccountId  = accountId;
            iLikie.PostId     = postId;
            iLikie.InsertDate = DateTime.UtcNow;
            _data.Likes.AddObject(iLikie);

            return(_data.SaveChanges());
        }
Exemple #2
0
        public int follow(string followerUserName, string followedUserId)
        {
            try
            {
                Guid follower          = new Guid(followedUserId);
                int  followerAccountId = _data.Accounts.Where(x => x.aspnet_Users.UserName == followerUserName).Select(a => a.AccountId).SingleOrDefault();
                int  followedAccountId = _data.Accounts.Where(x => x.UserId == follower).Select(a => a.AccountId).SingleOrDefault();

                Follow newFollow = new Follow();
                newFollow.FollowedAccountId = followedAccountId;
                newFollow.FollowerAccountId = followerAccountId;
                newFollow.InsertDate        = DateTime.UtcNow;

                _data.Follows.AddObject(newFollow);
                return(_data.SaveChanges());
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
        private bool SaveTwitterData(object userId, string consumerKey, string consumerSecret, string token, string secret, string userName, string twitterUserId)
        {
            try
            {
                Account newAccount = new Account()
                {
                    UserId = (Guid)userId, TwitterAccessToken = token, TwitterAccessSecret = secret, UserName = userName, TwitterUserId = twitterUserId
                };
                _data.Accounts.AddObject(newAccount);
                _data.Profiles.AddObject(new Profile()
                {
                    AccountId = newAccount.AccountId
                });
                _data.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        /// <summary>
        /// Save images uploaded into Azure Blob storage
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="fileStream"></param>
        /// <param name="fileName"></param>
        /// <param name="fileLocation"></param>
        public string SaveProfileImage(string userName, System.IO.Stream fileStream, string fileName, string fileLocation)
        {
            var account = _data.Accounts.Where(x => x.UserName == userName).FirstOrDefault();

            // Setup the connection to Windows Azure Storage
            var             storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=" + ConfigurationManager.AppSettings["AzureStorageAccount"] + ";AccountKey=" + ConfigurationManager.AppSettings["AzureStorageKey"]);
            CloudBlobClient blobClient     = storageAccount.CreateCloudBlobClient();

            // For large file copies you need to set up a custom timeout period
            // and using parallel settings appears to spread the copy across multiple threads
            blobClient.Timeout = new System.TimeSpan(1, 0, 0);
            blobClient.ParallelOperationThreadCount = 2;

            // Get and create the container
            CloudBlobContainer blobContainer = null;

            blobContainer = blobClient.GetContainerReference(userName); //("publicfiles");
            blobContainer.CreateIfNotExist();

            // Setup the permissions on the container to be public
            var permissions = new BlobContainerPermissions();

            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            blobContainer.SetPermissions(permissions);

            //ImageResizer
            //The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
            ResizeSettings resizeCropSettings      = new ResizeSettings("width=150&height=150&format=jpg&crop=auto");
            ResizeSettings resizeCropSettingsThumb = new ResizeSettings("width=55&height=55&format=jpg&crop=auto");

            //Let the image builder add the correct extension based on the output file type (which may differ).
            MemoryStream ms      = new MemoryStream();
            MemoryStream msThumb = new MemoryStream();

            ImageBuilder.Current.Build(fileStream, ms, resizeCropSettings, false);
            fileStream.Position = 0;
            ImageBuilder.Current.Build(fileStream, msThumb, resizeCropSettingsThumb, false);


            //Generate a file path
            string fileNameProfile = "profile/" + fileName.Replace(" ", "");
            string fileNameThumb   = "thumb/profile-pic"; //+ fileName.Replace(" ", "");

            //if there are existing profile and thumb images delete them
            if (account.ProfileImage != null)
            {
                var oldProfile = blobContainer.GetBlobReference("profile/" + account.ProfileImage);
                oldProfile.Delete();
                var oldThumb = blobContainer.GetBlobReference("thumb/profile-pic");
                oldThumb.Delete();
            }

            // Create the Blob and upload the file
            var blob      = blobContainer.GetBlobReference(fileNameProfile);
            var blobThumb = blobContainer.GetBlobReference(fileNameThumb);

            ms.Position      = 0;
            msThumb.Position = 0;
            blob.UploadFromStream(ms);
            blobThumb.UploadFromStream(msThumb);
            ms.Close();
            msThumb.Close();

            //update account table with profile image filename
            try
            {
                account.ProfileImage = fileName.Replace(" ", "");
                _data.SaveChanges();
            }
            catch
            {
                //todo: log exceptions with ELMAH
            }

            return(fileNameProfile);
        }