public static void MoveSharedItems(object parentId, object toId, IFolderDao folderDao, IFileDao fileDao) { var fileSecurity = Global.GetFilesSecurity(); var folders = folderDao.GetFolders(parentId); foreach (var folder in folders) { var shared = folder.Shared && fileSecurity.GetShares(folder).Any(record => record.Share != FileShare.Restrict); if (shared) { Global.Logger.InfoFormat("Move shared folder {0} from {1} to {2}", folder.ID, parentId, toId); folderDao.MoveFolder(folder.ID, toId, null); } else { MoveSharedItems(folder.ID, toId, folderDao, fileDao); } } var files = fileDao.GetFiles(parentId, null, FilterType.None, false, Guid.Empty, string.Empty, true); foreach (var file in files.Where(file => file.Shared && fileSecurity.GetShares(file) .Any(record => record.Subject != FileConstant.ShareLinkId && record.Share != FileShare.Restrict))) { Global.Logger.InfoFormat("Move shared file {0} from {1} to {2}", file.ID, parentId, toId); fileDao.MoveFile(file.ID, toId); } }
public static void ReassignItems(object parentId, Guid fromUserId, Guid toUserId, IFolderDao folderDao, IFileDao fileDao) { var fileIds = fileDao.GetFiles(parentId, new OrderBy(SortedByType.AZ, true), FilterType.ByUser, false, fromUserId, null, true, true) .Where(file => file.CreateBy == fromUserId).Select(file => file.ID); fileDao.ReassignFiles(fileIds.ToArray(), toUserId); var folderIds = folderDao.GetFolders(parentId, new OrderBy(SortedByType.AZ, true), FilterType.ByUser, false, fromUserId, null, true) .Where(folder => folder.CreateBy == fromUserId).Select(folder => folder.ID); folderDao.ReassignFolders(folderIds.ToArray(), toUserId); }
//Long operation public static void DeleteSubitems(object parentId, IFolderDao folderDao, IFileDao fileDao) { var folders = folderDao.GetFolders(parentId); foreach (var folder in folders) { DeleteSubitems(folder.ID, folderDao, fileDao); folderDao.DeleteFolder(folder.ID); } var files = fileDao.GetFiles(parentId, null, FilterType.None, Guid.Empty, string.Empty); foreach (var file in files) { fileDao.DeleteFile(file.ID); } }
//Long operation public static void DeleteSubitems(object parentId, IFolderDao folderDao, IFileDao fileDao) { var folders = folderDao.GetFolders(parentId); foreach (var folder in folders) { DeleteSubitems(folder.ID, folderDao, fileDao); Global.Logger.InfoFormat("Delete folder {0} in {1}", folder.ID, parentId); folderDao.DeleteFolder(folder.ID); } var files = fileDao.GetFiles(parentId, null, FilterType.None, false, Guid.Empty, string.Empty, true); foreach (var file in files) { Global.Logger.InfoFormat("Delete file {0} in {1}", file.ID, parentId); fileDao.DeleteFile(file.ID); } }
public static void ReassignItems(object parentId, Guid fromUserId, Guid toUserId, IFolderDao folderDao, IFileDao fileDao) { var files = fileDao.GetFiles(parentId, new OrderBy(SortedByType.AZ, true), FilterType.ByUser, fromUserId, null, withSubfolders: true) .Where(file => file.CreateBy == fromUserId); foreach (var file in files) { file.CreateBy = toUserId; fileDao.SaveFile(file, null); } var folders = folderDao.GetFolders(parentId, new OrderBy(SortedByType.AZ, true), FilterType.ByUser, fromUserId, null, true) .Where(folder => folder.CreateBy == fromUserId); foreach (var folder in folders) { folder.CreateBy = toUserId; folderDao.SaveFolder(folder); } }
public Folder <TTo> PerformCrossDaoFolderCopy <TFrom, TTo> (TFrom fromFolderId, IFolderDao <TFrom> fromFolderDao, IFileDao <TFrom> fromFileDao, Func <TFrom, TFrom> fromConverter, TTo toRootFolderId, IFolderDao <TTo> toFolderDao, IFileDao <TTo> toFileDao, Func <TTo, TTo> toConverter, bool deleteSourceFolder, CancellationToken?cancellationToken) { var fromFolder = fromFolderDao.GetFolder(fromConverter(fromFolderId)); var toFolder1 = ServiceProvider.GetService <Folder <TTo> >(); toFolder1.Title = fromFolder.Title; toFolder1.ParentFolderID = toConverter(toRootFolderId); var toFolder = toFolderDao.GetFolder(fromFolder.Title, toConverter(toRootFolderId)); var toFolderId = toFolder != null ? toFolder.ID : toFolderDao.SaveFolder(toFolder1); var foldersToCopy = fromFolderDao.GetFolders(fromConverter(fromFolderId)); var fileIdsToCopy = fromFileDao.GetFiles(fromConverter(fromFolderId)); Exception copyException = null; //Copy files first foreach (var fileId in fileIdsToCopy) { if (cancellationToken.HasValue) { cancellationToken.Value.ThrowIfCancellationRequested(); } try { PerformCrossDaoFileCopy(fileId, fromFileDao, fromConverter, toFolderId, toFileDao, toConverter, deleteSourceFolder); } catch (Exception ex) { copyException = ex; } } foreach (var folder in foldersToCopy) { if (cancellationToken.HasValue) { cancellationToken.Value.ThrowIfCancellationRequested(); } try { PerformCrossDaoFolderCopy(folder.ID, fromFolderDao, fromFileDao, fromConverter, toFolderId, toFolderDao, toFileDao, toConverter, deleteSourceFolder, cancellationToken); } catch (Exception ex) { copyException = ex; } } if (deleteSourceFolder) { var securityDao = ServiceProvider.GetService <SecurityDao <TFrom> >(); var fromFileShareRecords = securityDao.GetPureShareRecords(fromFolder) .Where(x => x.EntryType == FileEntryType.Folder); if (fromFileShareRecords.Any()) { fromFileShareRecords.ToList().ForEach(x => { x.EntryId = toFolderId; securityDao.SetShare(x); }); } var tagDao = ServiceProvider.GetService <TagDao <TFrom> >(); var fromFileNewTags = tagDao.GetNewTags(Guid.Empty, fromFolder).ToList(); if (fromFileNewTags.Any()) { fromFileNewTags.ForEach(x => x.EntryId = toFolderId); tagDao.SaveTags(fromFileNewTags); } if (copyException == null) { fromFolderDao.DeleteFolder(fromConverter(fromFolderId)); } } if (copyException != null) { throw copyException; } return(toFolderDao.GetFolder(toConverter(toFolderId))); }
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); }
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); }