예제 #1
0
        /// <summary>
        /// Adds File to Database
        /// </summary>
        /// <param name="info"></param>
        /// <param name="hierarchyId">Folder Directory of user uploading file</param>
        /// <returns></returns>
        public Guid AddResource(ResourceInfo info, string hierarchyId, EnumPostType postType)
        {
            try
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("[resource].[AddFileByParentId]", AppConfigManager.ConnectionString))
                {
                    info.ResourceId = Guid.NewGuid();
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                    adapter.SelectCommand.Parameters.AddWithValue("@ResourceId", info.ResourceId);
                    var nodeParam = adapter.SelectCommand.Parameters.Add("@UserHierarchyId", SqlDbType.Udt);
                    nodeParam.Value = SqlHierarchyId.Parse(hierarchyId);
                    nodeParam.UdtTypeName = "HierarchyId";
                    string extn = CodeHelper.CustomPicFileExtension;

                    switch (postType)
                    {
                        case EnumPostType.None:
                            break;
                        case EnumPostType.Picture:
                            extn = CodeHelper.CustomPicFileExtension;
                            break;
                        case EnumPostType.Video:
                            extn = CodeHelper.CustomVideoFileExtension;
                            break;
                        case EnumPostType.Audio:
                            extn = CodeHelper.CustomAudioFileExtension;
                            break;
                        default:
                            break;
                    }

                    adapter.SelectCommand.Parameters.AddWithValue("@FileExtension", extn);
                    adapter.SelectCommand.Parameters.AddWithValue("@FileData", info.Data);

                    adapter.SelectCommand.Connection.Open();
                    adapter.SelectCommand.ExecuteNonQuery();
                    adapter.SelectCommand.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                throw new Exception(CodeHelper.UnableToAddFile);
            }

            return info.ResourceId;
        }
예제 #2
0
        public string AddUser(UserInfo info, ResourceInfo resourceInfo)
        {
            try
            {
                if (!String.IsNullOrWhiteSpace(resourceInfo.DataUrl))
                    resourceInfo.Data = JsonWebToken.Base64UrlDecode(resourceInfo.DataUrl);

                string reqHeader = HttpContext.Current.Request.Headers[CodeHelper.HeaderAccessKey];
                if (!String.IsNullOrEmpty(reqHeader))
                {
                    // If token is valid then add user
                    if (TokenAuthorization.CheckBasicAuthorization(reqHeader))
                    {
                        UserManager mgr = new UserManager();
                        // gets territory id by country code
                        TerritoryManager trMgr = new TerritoryManager();
                        info.TerritoryId = trMgr.GetTerritoryIdByCountryCode(info.CountryCode, trMgr.GetTerritoryList()).Id;
                        if (!String.IsNullOrWhiteSpace(info.UserName) && !String.IsNullOrWhiteSpace(info.DisplayName) && !String.IsNullOrWhiteSpace(info.OwnNumber)
                            && !String.IsNullOrWhiteSpace(info.Status) && !String.IsNullOrWhiteSpace(info.SerialNum) && !String.IsNullOrWhiteSpace(info.ComId))
                            return mgr.AddUser(info, resourceInfo);
                        throw new Exception(CodeHelper.UnableToAddUser);
                    }

                    // throw exception stating token is invalid
                    throw new Exception(CodeHelper.InvalidToken);
                }

                throw new Exception(CodeHelper.InvalidHeader);
            }
            catch (Exception ex)
            {
                //HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                //return ex.Message;
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
예제 #3
0
        public string AddPost(PostInfo info, ResourceInfo resource1Info, ResourceInfo resource2Info, List<ContactInfo> lstContacts, bool isPicture, bool isVideo, bool isAudio, bool isWeb)
        {
            try
            {
                if (!String.IsNullOrWhiteSpace(info.Title) && !String.IsNullOrWhiteSpace(info.Caption1) && !String.IsNullOrWhiteSpace(info.Caption2)
                     && !String.IsNullOrWhiteSpace(resource1Info.DataUrl) && info.Duration >= 5 && info.Duration <= 60
                     && (info.IsPublic == true || info.ToContacts == true || (info.ToSelectedContacts == true && lstContacts.Count > 0))
                     && (info.TerritoryId != null || info.IsGlobal == true) && (isPicture || isVideo || isAudio) && (info.CategoryId == 1 || info.CategoryId == 2 || info.CategoryId == 3))
                {
                    PostManager mgr = new PostManager();

                    TokenInfo tokenInfo = new HelperMethods().GetUserToken<TokenInfo>(isWeb, HttpContext.Current);

                    resource1Info.Data = JsonWebToken.Base64UrlDecode(resource1Info.DataUrl);

                    if (resource2Info != null && !String.IsNullOrWhiteSpace(resource2Info.DataUrl))
                        resource2Info.Data = JsonWebToken.Base64UrlDecode(resource2Info.DataUrl);

                    // Sets user token from cookie
                    info.UserIdf = tokenInfo.Idf;
                    info.EndDate = DateTime.UtcNow.AddMinutes(info.Duration);

                    if (info.IsPublic)
                    {
                        info.ToContacts = false;
                        info.ToSelectedContacts = false;
                    }
                    else if (info.ToContacts)
                    {
                        info.ToSelectedContacts = false;
                        info.IsPublic = false;
                    }
                    else if (info.ToSelectedContacts)
                    {
                        info.IsPublic = false;
                        info.ToContacts = false;
                    }

                    if (isPicture)
                    {
                        isVideo = false;
                        isAudio = false;
                    }
                    else if (isVideo)
                    {
                        isPicture = false;
                        isAudio = false;
                    }
                    else if (isAudio)
                    {
                        isVideo = false;
                        isPicture = false;
                    }

                    EnumPostType postType = isPicture ? EnumPostType.Picture : (isVideo ? EnumPostType.Video : (isAudio ? EnumPostType.Audio : EnumPostType.Picture));

                    var guids = mgr.AddPostImages(resource1Info, resource2Info, tokenInfo.FolderPath, postType);
                    long postId = mgr.AddPost(info, guids[0], guids[1], postType, tokenInfo.FolderPath);
                    if (info.ToSelectedContacts)
                    {
                        List<ContactInfo> lstFilteredContacts = new List<ContactInfo>();
                        ContactManager conMgr = new ContactManager();
                        lstContacts.ForEach(x =>
                        {
                            if (!String.IsNullOrWhiteSpace(x.ContactToken))
                            {
                                try
                                {
                                    TokenInfo contactToken = JsonWebToken.DecodeToken<TokenInfo>(x.ContactToken, CodeHelper.SecretAccessKey, true, false);
                                    x.ContactIdf = contactToken.Idf;
                                    lstFilteredContacts.Add(x);
                                }
                                catch (Exception)
                                {
                                    // if contact does not have all details, ignore it
                                }
                            }

                        });
                        conMgr.SavePostContacts(info.UserIdf, info.TerritoryId, info.IsGlobal, postId, lstFilteredContacts);
                    }

                    // schedule job
                    mgr.SchedulePost(postId, info.EndDate);

                    // send notification to all users either GCM or SignalR stating your contact has posted something
                    var obj = GlobalHost.ConnectionManager.GetHubContext<HeyVoteHub>();
                    new PostManager().GetNotificationUserList(postId.ToString()).ForEach(x =>
                    {
                        var signalR = HeyVoteHub.MyUsers.Where(y => y.Value.Equals(x.NotifyUserIdf)).FirstOrDefault();
                        if (!signalR.Equals(default(KeyValuePair<string, string>)))
                            obj.Clients.Client(signalR.Key).notifyUsers(new
                            {
                                Id = x.Id,
                                Title = x.Title,
                                UserIdf = x.UserIdf,
                                ImageIdf = x.ImageIdf,
                                Image1Idf = x.Image1Idf,
                                FolderPath = x.FolderPath,
                                CreatedOn = x.CreatedOn,
                                Caption1 = x.Caption1,
                                Caption2 = x.Caption2,
                                DisplayName = x.DisplayName,
                                PostId = x.PostId,
                                EndDate = x.EndDate,
                                isPost = x.isPost,
                                isFollow = x.isFollow,
                                isContact = x.isContact,
                                hasRead = false,
                                isViewed = false
                            });
                    });

                    return true.ToString();
                }
                return false.ToString();
            }
            catch (Exception ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
예제 #4
0
        /// <summary>
        /// Adds as new user  
        /// </summary>
        /// <param name="info"></param>
        /// <param name="resourceInfo"></param>
        /// <returns></returns>
        public string AddUser(UserInfo info, ResourceInfo resourceInfo)
        {
            string directory = String.Empty;
            Guid? resourceId = null;
            bool hasImage = false;
            bool hasDirectory = false;
            try
            {
                // Create Directory
                DirectoryManager dirMgr = new DirectoryManager();
                directory = dirMgr.CreateDirectory();

                if (!String.IsNullOrWhiteSpace(directory))
                    hasDirectory = true;

                hasImage = resourceInfo.Data != null;

                if (hasImage)
                {
                    // Add Resource to that directory
                    ResourceManager mgr = new ResourceManager();
                    resourceId = mgr.AddResource(resourceInfo, directory, EnumPostType.Picture);
                }

                using (SqlDataAdapter adapter = new SqlDataAdapter("[user].[AddUserInfo]", AppConfigManager.ConnectionString))
                {
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                    adapter.SelectCommand.Parameters.AddWithValue("@UserName", info.UserName);
                    adapter.SelectCommand.Parameters.AddWithValue("@DisplayName", info.DisplayName);
                    adapter.SelectCommand.Parameters.AddWithValue("@OwnNumber", info.OwnNumber);
                    adapter.SelectCommand.Parameters.AddWithValue("@GenderId", info.GenderId);
                    adapter.SelectCommand.Parameters.AddWithValue("@AgeRangeId", info.AgeRangeId);
                    adapter.SelectCommand.Parameters.AddWithValue("@Status", info.Status);
                    adapter.SelectCommand.Parameters.AddWithValue("@ImageIdf", resourceId);

                    var nodeParam = adapter.SelectCommand.Parameters.Add("@FolderPath", SqlDbType.Udt);
                    nodeParam.Value = SqlHierarchyId.Parse(directory);
                    nodeParam.UdtTypeName = "HierarchyId";

                    adapter.SelectCommand.Parameters.AddWithValue("@TerritoryId", info.TerritoryId);

                    DataTable dt = new DataTable();

                    // Gets UserId of Inserted User
                    adapter.Fill(dt);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        var userIdf = dt.Rows[0][CodeHelper.Idf];
                        var userFolderPath = dt.Rows[0][CodeHelper.FolderPath];
                        if (userIdf != null && userFolderPath != null)
                            return JsonWebToken.Encode(new TokenInfo() { Idf = Guid.Parse(userIdf.ToString()), FolderPath = userFolderPath.ToString(), TerritoryId = info.TerritoryId }, CodeHelper.SecretAccessKey, HvHashAlgorithm.RS256);

                    }

                    if (resourceId != null)
                        DeleteFile(resourceId);
                    // Delete directory and file
                    if (hasDirectory)
                        DeleteDirectory(directory);

                    throw new Exception(CodeHelper.UnableToAddUser);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;

                if (resourceId != null)
                    DeleteFile(resourceId);
                // Delete directory and file
                if (hasDirectory)
                    DeleteDirectory(directory);
                throw new Exception(CodeHelper.UnableToAddUser);
            }
        }
예제 #5
0
        public Guid?[] AddPostImages(ResourceInfo resourceInfo1, ResourceInfo resourceInfo2, string hierarchyId, EnumPostType postType)
        {
            try
            {
                ResourceManager mgr = new ResourceManager();
                Guid resourceId1 = mgr.AddResource(resourceInfo1, hierarchyId, postType);
                Guid? resourceId2 = null;
                if (resourceInfo2 != null && !String.IsNullOrWhiteSpace(resourceInfo2.DataUrl))
                    resourceId2 = mgr.AddResource(resourceInfo2, hierarchyId, postType);

                return new Guid?[] { resourceId1, resourceId2 };
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                throw new Exception(CodeHelper.UnableToAddFile);
            }
        }