protected void InitScripts()
        {
            var inlineScript = new StringBuilder();

            var apiServer = new ASC.Api.ApiServer();
            var apiUrl = String.Format("{0}settings/quota.json", SetupInfo.WebApiBaseUrl);
            var responseApi = Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")));

            inlineScript.AppendFormat("ASC.Files.ChunkUploads.tenantQuota = {0}.response;ASC.Files.ChunkUploads.changeQuotaText();", responseApi);

            Page.RegisterInlineScript(inlineScript.ToString());
        }
        protected void InitScripts()
        {
            var inlineScript = new StringBuilder();

            var apiServer   = new ASC.Api.ApiServer();
            var apiUrl      = String.Format("{0}settings/quota.json", SetupInfo.WebApiBaseUrl);
            var responseApi = Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET")));

            inlineScript.AppendFormat("ASC.Files.ChunkUploads.tenantQuota = {0}.response;ASC.Files.ChunkUploads.changeQuotaText();", responseApi);

            Page.RegisterInlineScript(inlineScript.ToString());
        }
        public static IEnumerable <FileEntry> GetEntries(IFolderDao folderDao, IFileDao fileDao, Folder parent, int from, int count, FilterType filter, bool subjectGroup, Guid subjectId, String searchText, bool searchInContent, bool withSubfolders, OrderBy orderBy, out int total)
        {
            total = 0;

            if (parent == null)
            {
                throw new ArgumentNullException("parent", FilesCommonResource.ErrorMassage_FolderNotFound);
            }

            var fileSecurity = Global.GetFilesSecurity();
            var entries      = Enumerable.Empty <FileEntry>();

            searchInContent = searchInContent && filter != FilterType.ByExtension && !Equals(parent.ID, Global.FolderTrash);

            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 responseBody = apiServer.GetApiResponse(apiUrl, "GET");
                if (responseBody != null)
                {
                    var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(responseBody)));

                    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 folderIDProjectTitle = (Dictionary <object, KeyValuePair <int, string> >)HttpRuntime.Cache.Get(projectListCacheKey);

                    if (folderIDProjectTitle == null)
                    {
                        apiUrl = string.Format("{0}project/filter.json?sortBy=title&sortOrder=ascending&status=open&fields=id,title,security,projectFolder", 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());
                        }

                        folderIDProjectTitle = new Dictionary <object, KeyValuePair <int, string> >();
                        foreach (JObject projectInfo in responseData.Children())
                        {
                            var projectID    = projectInfo["id"].Value <int>();
                            var projectTitle = Global.ReplaceInvalidCharsAndTruncate(projectInfo["title"].Value <String>());

                            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;
                                    }
                                }
                            }

                            int    projectFolderID;
                            JToken projectFolderIDjToken;
                            if (projectInfo.TryGetValue("projectFolder", out projectFolderIDjToken))
                            {
                                projectFolderID = projectFolderIDjToken.Value <int>();
                            }
                            else
                            {
                                projectFolderID = (int)FilesIntegration.RegisterBunch("projects", "project", projectID.ToString());
                            }

                            if (!folderIDProjectTitle.ContainsKey(projectFolderID))
                            {
                                folderIDProjectTitle.Add(projectFolderID, new KeyValuePair <int, string>(projectID, projectTitle));
                            }

                            AscCache.Default.Remove("documents/folders/" + projectFolderID);
                            AscCache.Default.Insert("documents/folders/" + projectFolderID, projectTitle, TimeSpan.FromMinutes(30));
                        }

                        HttpRuntime.Cache.Remove(projectListCacheKey);
                        HttpRuntime.Cache.Insert(projectListCacheKey, folderIDProjectTitle, new CacheDependency(null, new[] { projectLastModifiedCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15));
                    }

                    var rootKeys = folderIDProjectTitle.Keys.ToArray();
                    if (filter == FilterType.None || filter == FilterType.FoldersOnly)
                    {
                        var folders = folderDao.GetFolders(rootKeys, filter, subjectGroup, subjectId, searchText, withSubfolders, false);

                        var emptyFilter = string.IsNullOrEmpty(searchText) && filter == FilterType.None && subjectId == Guid.Empty;
                        if (!emptyFilter)
                        {
                            var projectFolderIds =
                                folderIDProjectTitle
                                .Where(projectFolder => string.IsNullOrEmpty(searchText) ||
                                       (projectFolder.Value.Value ?? "").ToLower().Trim().Contains(searchText.ToLower().Trim()))
                                .Select(projectFolder => projectFolder.Key);

                            folders.RemoveAll(folder => rootKeys.Contains(folder.ID));

                            var projectFolders = folderDao.GetFolders(projectFolderIds.ToArray(), filter, subjectGroup, subjectId, null, false, false);
                            folders.AddRange(projectFolders);
                        }

                        folders.ForEach(x =>
                        {
                            x.Title     = folderIDProjectTitle.ContainsKey(x.ID) ? folderIDProjectTitle[x.ID].Value : x.Title;
                            x.FolderUrl = folderIDProjectTitle.ContainsKey(x.ID) ? PathProvider.GetFolderUrl(x, folderIDProjectTitle[x.ID].Key) : string.Empty;
                        });

                        if (withSubfolders)
                        {
                            folders = fileSecurity.FilterRead(folders).ToList();
                        }

                        entries = entries.Concat(folders);
                    }

                    if (filter != FilterType.FoldersOnly && withSubfolders)
                    {
                        var files = fileDao.GetFiles(rootKeys, filter, subjectGroup, subjectId, searchText, searchInContent).ToList();
                        files   = fileSecurity.FilterRead(files).ToList();
                        entries = entries.Concat(files);
                    }
                }

                parent.TotalFiles      = entries.Aggregate(0, (a, f) => a + (f.FileEntryType == FileEntryType.Folder ? ((Folder)f).TotalFiles : 1));
                parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f.FileEntryType == FileEntryType.Folder ? ((Folder)f).TotalSubFolders + 1 : 0));
            }
            else if (parent.FolderType == FolderType.SHARE)
            {
                //share
                var shared = (IEnumerable <FileEntry>)fileSecurity.GetSharesForMe(filter, subjectGroup, subjectId, searchText, searchInContent, withSubfolders);

                entries = entries.Concat(shared);

                parent.TotalFiles      = entries.Aggregate(0, (a, f) => a + (f.FileEntryType == FileEntryType.Folder ? ((Folder)f).TotalFiles : 1));
                parent.TotalSubFolders = entries.Aggregate(0, (a, f) => a + (f.FileEntryType == FileEntryType.Folder ? ((Folder)f).TotalSubFolders + 1 : 0));
            }
            else
            {
                if (parent.FolderType == FolderType.TRASH)
                {
                    withSubfolders = false;
                }

                var folders = folderDao.GetFolders(parent.ID, orderBy, filter, subjectGroup, subjectId, searchText, withSubfolders).Cast <FileEntry>();
                folders = fileSecurity.FilterRead(folders);
                entries = entries.Concat(folders);

                var files = fileDao.GetFiles(parent.ID, orderBy, filter, subjectGroup, subjectId, searchText, searchInContent, withSubfolders).Cast <FileEntry>();
                files   = fileSecurity.FilterRead(files);
                entries = entries.Concat(files);

                if (filter == FilterType.None || filter == FilterType.FoldersOnly)
                {
                    var folderList = GetThirpartyFolders(parent, searchText);

                    var thirdPartyFolder = FilterEntries(folderList, filter, subjectGroup, subjectId, searchText, searchInContent);
                    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.Where(r => r != null && r.ID != null && r.FileEntryType == FileEntryType.File).Select(r => r as File).ToList());

            return(entries);
        }
Exemplo n.º 4
0
        public static IEnumerable <FileEntry> GetEntries(IFolderDao folderDao, IFileDao fileDao, 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 responseBody = apiServer.GetApiResponse(apiUrl, "GET");
                if (responseBody != null)
                {
                    var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(responseBody)));

                    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 || !string.IsNullOrEmpty(searchText))
                    {
                        apiUrl = String.Format("{0}project/filter.json?sortBy=title&sortOrder=ascending&status=open", 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(), searchText, !string.IsNullOrEmpty(searchText));
                        folders.ForEach(x =>
                        {
                            x.Title     = folderIDProjectTitle.ContainsKey(x.ID) ? folderIDProjectTitle[x.ID] : x.Title;
                            x.FolderUrl = PathProvider.GetFolderUrl(x);
                        });

                        folders = fileSecurity.FilterRead(folders).ToList();

                        entries = entries.Concat(folders);

                        if (!string.IsNullOrEmpty(searchText))
                        {
                            var files = fileDao.GetFiles(folderIDProjectTitle.Keys.ToArray(), searchText, !string.IsNullOrEmpty(searchText)).ToList();
                            files   = fileSecurity.FilterRead(files).ToList();
                            entries = entries.Concat(files);
                        }

                        if (entries.Any() && string.IsNullOrEmpty(searchText))
                        {
                            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(searchText, !string.IsNullOrEmpty(searchText));
                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, !string.IsNullOrEmpty(searchText) && parent.FolderType != FolderType.TRASH).Cast <FileEntry>();
                folders = fileSecurity.FilterRead(folders);
                entries = entries.Concat(folders);

                //TODO:Optimize
                var files = fileDao.GetFiles(parent.ID, orderBy, filter, subjectId, searchText, !string.IsNullOrEmpty(searchText) && parent.FolderType != FolderType.TRASH).Cast <FileEntry>();
                files   = fileSecurity.FilterRead(files);
                entries = entries.Concat(files);

                if (filter == FilterType.None || filter == FilterType.FoldersOnly)
                {
                    var folderList = GetThirpartyFolders(parent, searchText);

                    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);
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
        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.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.ReadWrite;
                        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 ((parent.ID.Equals(Global.FolderMy) || parent.ID.Equals(Global.FolderCommon)) &&
                    ImportConfiguration.SupportInclusion &&
                    !CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor() &&
                    (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);

            SetFileStatus(entries.Select(r => r as File).Where(r => r != null && r.ID != null));

            //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);
        }