public static IEnumerable <FileEntry> GetEntries(IFolderDao folderDao, Folder parent, FilterType filter, Guid subjectId, OrderBy orderBy, String searchText, int from, int count, out int total) { total = 0; if (parent == null) { throw new ArgumentNullException("parent", FilesCommonResource.ErrorMassage_FolderNotFound); } var fileSecurity = Global.GetFilesSecurity(); var entries = Enumerable.Empty <FileEntry>(); if (parent.FolderType == FolderType.Projects && parent.ID.Equals(Global.FolderProjects)) { var apiServer = new ASC.Api.ApiServer(); var apiUrl = String.Format("{0}project/maxlastmodified.json", SetupInfo.WebApiBaseUrl); var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")))); var projectLastModified = responseApi["response"].Value <String>(); const string projectLastModifiedCacheKey = "documents/projectFolders/projectLastModified"; if (HttpRuntime.Cache.Get(projectLastModifiedCacheKey) == null || !HttpRuntime.Cache.Get(projectLastModifiedCacheKey).Equals(projectLastModified)) { HttpRuntime.Cache.Remove(projectLastModifiedCacheKey); HttpRuntime.Cache.Insert(projectLastModifiedCacheKey, projectLastModified); } var projectListCacheKey = String.Format("documents/projectFolders/{0}", SecurityContext.CurrentAccount.ID); var fromCache = HttpRuntime.Cache.Get(projectListCacheKey); if (fromCache == null) { apiUrl = String.Format("{0}project/filter.json?sortBy=title&sortOrder=ascending", SetupInfo.WebApiBaseUrl); responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")))); var responseData = responseApi["response"]; if (!(responseData is JArray)) { return(entries.ToList()); } var folderIDProjectTitle = new Dictionary <object, String>(); foreach (JObject projectInfo in responseData.Children()) { var projectID = projectInfo["id"].Value <String>(); var projectTitle = Global.ReplaceInvalidCharsAndTruncate(projectInfo["title"].Value <String>()); int projectFolderID; JToken projectSecurityJToken; if (projectInfo.TryGetValue("security", out projectSecurityJToken)) { var projectSecurity = projectInfo["security"].Value <JObject>(); JToken projectCanFileReadJToken; if (projectSecurity.TryGetValue("canReadFiles", out projectCanFileReadJToken)) { if (!projectSecurity["canReadFiles"].Value <bool>()) { continue; } } } JToken projectFolderIDJToken; if (projectInfo.TryGetValue("projectFolder", out projectFolderIDJToken)) { projectFolderID = projectInfo["projectFolder"].Value <int>(); } else { projectFolderID = (int)FilesIntegration.RegisterBunch("projects", "project", projectID); } if (!folderIDProjectTitle.ContainsKey(projectFolderID)) { folderIDProjectTitle.Add(projectFolderID, projectTitle); } HttpRuntime.Cache.Remove("documents/folders/" + projectFolderID.ToString()); HttpRuntime.Cache.Insert("documents/folders/" + projectFolderID.ToString(), projectTitle, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30)); } var folders = folderDao.GetFolders(folderIDProjectTitle.Keys.ToArray()); folders.ForEach(x => { x.Title = folderIDProjectTitle[x.ID]; x.Access = FileShare.ReadWrite; x.FolderUrl = PathProvider.GetFolderUrl(x); }); entries = entries.Concat(folders); if (entries.Any()) { HttpRuntime.Cache.Remove(projectListCacheKey); HttpRuntime.Cache.Insert(projectListCacheKey, entries, new CacheDependency(null, new[] { projectLastModifiedCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15)); } } else { entries = entries.Concat((IEnumerable <FileEntry>)fromCache); } entries = FilterEntries(entries, filter, subjectId, searchText); parent.TotalFiles = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalFiles : 1)); parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalSubFolders + 1 : 0)); } else if (parent.FolderType == FolderType.SHARE) { //share var shared = (IEnumerable <FileEntry>)fileSecurity.GetSharesForMe(); if (CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor()) { shared = shared.Where(r => !r.ProviderEntry); } shared = FilterEntries(shared, filter, subjectId, searchText) .Where(f => f.CreateBy != SecurityContext.CurrentAccount.ID && // don't show my files f.RootFolderType == FolderType.USER); // don't show common files (common files can read) entries = entries.Concat(shared); parent.TotalFiles = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalFiles : 1)); parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalSubFolders + 1 : 0)); } else { var folders = folderDao.GetFolders(parent.ID, orderBy, filter, subjectId, searchText).Cast <FileEntry>(); folders = fileSecurity.FilterRead(folders); entries = entries.Concat(folders); //TODO:Optimize var files = folderDao.GetFiles(parent.ID, orderBy, filter, subjectId, searchText).Cast <FileEntry>(); files = fileSecurity.FilterRead(files); entries = entries.Concat(files); if ((parent.ID.Equals(Global.FolderMy) || parent.ID.Equals(Global.FolderCommon)) && ImportConfiguration.SupportInclusion && (Global.IsAdministrator || CoreContext.Configuration.Personal || FilesSettings.EnableThirdParty)) { using (var securityDao = Global.DaoFactory.GetSecurityDao()) using (var providerDao = Global.DaoFactory.GetProviderDao()) { var providers = providerDao.GetProvidersInfo(parent.RootFolderType); var folderList = providers .Select(providerInfo => //Fake folder. Don't send request to third party new Folder { ID = providerInfo.RootFolderId, ParentFolderID = parent.ID, CreateBy = providerInfo.Owner, CreateOn = providerInfo.CreateOn, FolderType = FolderType.DEFAULT, ModifiedBy = providerInfo.Owner, ModifiedOn = providerInfo.CreateOn, ProviderId = providerInfo.ID, ProviderKey = providerInfo.ProviderKey, RootFolderCreator = providerInfo.Owner, RootFolderId = providerInfo.RootFolderId, RootFolderType = providerInfo.RootFolderType, Shareable = false, Title = providerInfo.CustomerTitle, TotalFiles = 0, TotalSubFolders = 0 } ) .Where(fileSecurity.CanRead).ToList(); if (folderList.Any()) { securityDao.GetPureShareRecords(folderList.Cast <FileEntry>().ToArray()) .Where(x => x.Owner == SecurityContext.CurrentAccount.ID) .Select(x => x.EntryId).Distinct().ToList() .ForEach(id => { folderList.First(y => y.ID.Equals(id)).SharedByMe = true; }); } var thirdPartyFolder = FilterEntries(folderList, filter, subjectId, searchText); entries = entries.Concat(thirdPartyFolder); } } } if (orderBy.SortedBy != SortedByType.New) { entries = SortEntries(entries, orderBy); total = entries.Count(); if (0 < from) { entries = entries.Skip(from); } if (0 < count) { entries = entries.Take(count); } } entries = FileMarker.SetTagsNew(folderDao, parent, entries); //sorting after marking if (orderBy.SortedBy == SortedByType.New) { entries = SortEntries(entries, orderBy); total = entries.Count(); if (0 < from) { entries = entries.Skip(from); } if (0 < count) { entries = entries.Take(count); } } SetFileStatus(entries.Select(r => r as File).Where(r => r != null && r.ID != null)); return(entries); }
public static IEnumerable<FileEntry> GetEntries(IFolderDao folderDao, Folder parent, FilterType filter, Guid subjectId, OrderBy orderBy, String searchText, int from, int count, out int total) { total = 0; if (parent == null) throw new ArgumentNullException(FilesCommonResource.ErrorMassage_FolderNotFound); var fileSecurity = Global.GetFilesSecurity(); var entries = Enumerable.Empty<FileEntry>(); if (parent.FolderType == FolderType.Projects && parent.ID.Equals(Global.FolderProjects)) { var apiServer = new ASC.Api.ApiServer(); var apiUrl = String.Format("{0}project/maxlastmodified.json", SetupInfo.WebApiBaseUrl); var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")))); var projectLastModified = responseApi["response"].Value<String>(); const string projectLastModifiedCacheKey = "documents/projectFolders/projectLastModified"; if (HttpRuntime.Cache.Get(projectLastModifiedCacheKey) == null || !HttpRuntime.Cache.Get(projectLastModifiedCacheKey).Equals(projectLastModified)) HttpRuntime.Cache.Insert(projectLastModifiedCacheKey, projectLastModified); var projectListCacheKey = String.Format("documents/projectFolders/{0}", SecurityContext.CurrentAccount.ID); var fromCache = HttpRuntime.Cache.Get(projectListCacheKey); if (fromCache == null) { apiUrl = String.Format("{0}project/filter.json?sortBy=title&sortOrder=ascending", SetupInfo.WebApiBaseUrl); responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")))); var responseData = responseApi["response"]; if (!(responseData is JArray)) return entries.ToList(); var folderIDProjectTitle = new Dictionary<object, String>(); foreach (JObject projectInfo in responseData.Children()) { var projectID = projectInfo["id"].Value<String>(); var projectTitle = Global.ReplaceInvalidCharsAndTruncate(projectInfo["title"].Value<String>()); int projectFolderID; JToken projectSecurityJToken; if (projectInfo.TryGetValue("security", out projectSecurityJToken)) { var projectSecurity = projectInfo["security"].Value<JObject>(); JToken projectCanFileReadJToken; if (projectSecurity.TryGetValue("canReadFiles", out projectCanFileReadJToken)) { if (!projectSecurity["canReadFiles"].Value<bool>()) { continue; } } } JToken projectFolderIDJToken; if (projectInfo.TryGetValue("projectFolder", out projectFolderIDJToken)) projectFolderID = projectInfo["projectFolder"].Value<int>(); else projectFolderID = (int)FilesIntegration.RegisterBunch("projects", "project", projectID); if (!folderIDProjectTitle.ContainsKey(projectFolderID)) folderIDProjectTitle.Add(projectFolderID, projectTitle); HttpRuntime.Cache.Insert("documents/folders/" + projectFolderID.ToString(), projectTitle, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30)); } var folders = folderDao.GetFolders(folderIDProjectTitle.Keys.ToArray()); folders.ForEach(x => { x.Title = folderIDProjectTitle[x.ID]; x.Access = FileShare.Read; x.FolderUrl = PathProvider.GetFolderUrl(x); }); entries = entries.Concat(folders); if (entries.Any()) HttpRuntime.Cache.Insert(projectListCacheKey, entries, new CacheDependency(null, new[] { projectLastModifiedCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15)); } else { entries = entries.Concat((IEnumerable<FileEntry>)fromCache); } entries = FilterEntries(entries, filter, subjectId, searchText); parent.TotalFiles = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalFiles : 1)); parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalSubFolders + 1 : 0)); } else if (parent.FolderType == FolderType.SHARE) { //share var shared = (IEnumerable<FileEntry>)fileSecurity.GetSharesForMe(); shared = FilterEntries(shared, filter, subjectId, searchText) .Where(f => f.CreateBy != SecurityContext.CurrentAccount.ID && // don't show my files f.RootFolderType == FolderType.USER); // don't show common files (common files can read) entries = entries.Concat(shared); parent.TotalFiles = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalFiles : 1)); parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f is Folder ? ((Folder)f).TotalSubFolders + 1 : 0)); } else { var folders = folderDao.GetFolders(parent.ID, orderBy, filter, subjectId, searchText).Cast<FileEntry>(); folders = fileSecurity.FilterRead(folders); entries = entries.Concat(folders); var files = folderDao.GetFiles(parent.ID, orderBy, filter, subjectId, searchText).Cast<FileEntry>(); files = fileSecurity.FilterRead(files); entries = entries.Concat(files); if (ImportConfiguration.SupportInclusion && (parent.ID.Equals(Global.FolderMy) || parent.ID.Equals(Global.FolderCommon))) { using (var securityDao = Global.DaoFactory.GetSecurityDao()) using (var providerDao = Global.DaoFactory.GetProviderDao()) { var providers = providerDao.GetProvidersInfo(parent.RootFolderType); var folderList = providers .Select(providerInfo => //Fake folder. Don't send request to third party new Folder { ID = providerInfo.RootFolderId, ParentFolderID = parent.ID, CreateBy = providerInfo.Owner, CreateOn = providerInfo.CreateOn, FolderType = FolderType.DEFAULT, ModifiedBy = providerInfo.Owner, ModifiedOn = providerInfo.CreateOn, ProviderId = providerInfo.ID, ProviderKey = providerInfo.ProviderKey, RootFolderCreator = providerInfo.Owner, RootFolderId = providerInfo.RootFolderId, RootFolderType = providerInfo.RootFolderType, Shareable = false, Title = providerInfo.CustomerTitle, TotalFiles = 0, TotalSubFolders = 0 } ) .Where(fileSecurity.CanRead).ToList(); if (folderList.Any()) securityDao.GetPureShareRecords(folderList.Cast<FileEntry>().ToArray()) .Where(x => x.Owner == SecurityContext.CurrentAccount.ID) .Select(x => x.EntryId).Distinct().ToList() .ForEach(id => { folderList.First(y => y.ID.Equals(id)).SharedByMe = true; }); var thirdPartyFolder = FilterEntries(folderList, filter, subjectId, searchText); entries = entries.Concat(thirdPartyFolder); } } } if (orderBy.SortedBy != SortedByType.New) { entries = SortEntries(entries, orderBy); total = entries.Count(); if (0 < from) entries = entries.Skip(from); if (0 < count) entries = entries.Take(count); } entries = FileMarker.SetTagsNew(folderDao, parent, entries); //sorting after marking if (orderBy.SortedBy == SortedByType.New) { entries = SortEntries(entries, orderBy); total = entries.Count(); if (0 < from) entries = entries.Skip(from); if (0 < count) entries = entries.Take(count); } return entries; }