private static void DeleteFile(Guid? resourceId) { ResourceManager resMgr = new ResourceManager(); resMgr.DeleteResource(resourceId); }
/// <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); } }
/// <summary> /// Change profile picture /// </summary> /// <param name="userIdf"></param> /// <param name="name"></param> /// <returns></returns> public string ChangeProfilePicture(Guid userIdf, string folderPath, byte[] data) { Guid? oldImage = null; Guid resourceId = Guid.Empty; try { using (SqlDataAdapter adapter = new SqlDataAdapter("[user].[GetPicture]", AppConfigManager.ConnectionString)) { adapter.SelectCommand.CommandType = CommandType.StoredProcedure; adapter.SelectCommand.Parameters.AddWithValue("@UserIdf", userIdf); DataTable dt = new DataTable(); adapter.Fill(dt); if (dt != null && dt.Rows.Count > 0) { object obj = dt.Rows[0][0]; if (obj != null && !String.IsNullOrWhiteSpace(obj.ToString())) oldImage = Guid.Parse(Convert.ToString(obj)); ResourceManager mgr = new ResourceManager(); resourceId = mgr.AddResource(new ResourceInfo() { Data = data, FileExtension = CodeHelper.CustomPicFileExtension }, folderPath, EnumPostType.Picture); if (resourceId != Guid.Empty) { adapter.SelectCommand.CommandText = "[user].[ChangePicture]"; adapter.SelectCommand.Parameters.AddWithValue("@ResoureId", resourceId); adapter.SelectCommand.Connection.Open(); int results = adapter.SelectCommand.ExecuteNonQuery(); adapter.SelectCommand.Connection.Close(); if (results > 0) { // if file was added and ImageIdf was updated, delete old file if (oldImage != null) DeleteFile(oldImage); return true.ToString(); } else { // if image was added but ImageIdf was not updated remove newly added file if (resourceId != Guid.Empty) DeleteFile(resourceId); } } } throw new Exception(CodeHelper.UnableToUpdateProfilePicture); } } catch (Exception ex) { if (resourceId != Guid.Empty) DeleteFile(resourceId); string msg = ex.Message; throw new Exception(CodeHelper.UnableToUpdateProfilePicture); } }
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); } }