diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs index c1fbd4f2fbc..4886977d457 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs @@ -377,10 +377,9 @@ public async Task> SaveFileAsync(File file, Stream fileStream, bo var (roomId, _) = await folderDao.GetParentRoomInfoFromFileEntryAsync(currentFolder); - Folder currentRoom = null; if (roomId != -1) { - currentRoom = await folderDao.GetFolderAsync(roomId); + var currentRoom = await folderDao.GetFolderAsync(roomId); var quotaRoomSettings = await _settingsManager.LoadAsync(); if (quotaRoomSettings.EnableQuota) { @@ -509,7 +508,7 @@ await strategy.ExecuteAsync(async () => { try { - await SaveFileStreamAsync(file, fileStream); + await SaveFileStreamAsync(file, fileStream, currentFolder); } catch (Exception saveException) { @@ -582,66 +581,66 @@ public async Task> ReplaceFileVersionAsync(File file, Stream file await using (await distributedLockProvider.TryAcquireFairLockAsync(LockKey)) { - await using var filesDbContext = await _dbContextFactory.CreateDbContextAsync(); - var strategy = filesDbContext.Database.CreateExecutionStrategy(); + await using var filesDbContext = await _dbContextFactory.CreateDbContextAsync(); + var strategy = filesDbContext.Database.CreateExecutionStrategy(); - await strategy.ExecuteAsync(async () => - { - await using var context = await _dbContextFactory.CreateDbContextAsync(); - await using var tx = await context.Database.BeginTransactionAsync(); + await strategy.ExecuteAsync(async () => + { + await using var context = await _dbContextFactory.CreateDbContextAsync(); + await using var tx = await context.Database.BeginTransactionAsync(); - file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title); - //make lowerCase - file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title)); + file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title); + //make lowerCase + file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title)); - file.ModifiedBy = _authContext.CurrentAccount.ID; - file.ModifiedOn = _tenantUtil.DateTimeNow(); - if (file.CreateBy == default) - { - file.CreateBy = _authContext.CurrentAccount.ID; - } + file.ModifiedBy = _authContext.CurrentAccount.ID; + file.ModifiedOn = _tenantUtil.DateTimeNow(); + if (file.CreateBy == default) + { + file.CreateBy = _authContext.CurrentAccount.ID; + } - if (file.CreateOn == default) - { - file.CreateOn = _tenantUtil.DateTimeNow(); - } + if (file.CreateOn == default) + { + file.CreateOn = _tenantUtil.DateTimeNow(); + } - toUpdate = await Queries.DbFileByVersionAsync(context, tenantId, file.Id, file.Version); - - toUpdate.Version = file.Version; - toUpdate.VersionGroup = file.VersionGroup; - toUpdate.ParentId = file.ParentId; - toUpdate.Title = file.Title; - toUpdate.ContentLength = file.ContentLength; - toUpdate.Category = (int)file.FilterType; - toUpdate.CreateBy = file.CreateBy; - toUpdate.CreateOn = _tenantUtil.DateTimeToUtc(file.CreateOn); - toUpdate.ModifiedBy = file.ModifiedBy; - toUpdate.ModifiedOn = _tenantUtil.DateTimeToUtc(file.ModifiedOn); - toUpdate.ConvertedType = file.ConvertedType; - toUpdate.Comment = file.Comment; - toUpdate.Encrypted = file.Encrypted; - toUpdate.Forcesave = file.Forcesave; - toUpdate.ThumbnailStatus = file.ThumbnailStatus; - - context.Update(toUpdate); - await context.SaveChangesAsync(); + toUpdate = await Queries.DbFileByVersionAsync(context, tenantId, file.Id, file.Version); + + toUpdate.Version = file.Version; + toUpdate.VersionGroup = file.VersionGroup; + toUpdate.ParentId = file.ParentId; + toUpdate.Title = file.Title; + toUpdate.ContentLength = file.ContentLength; + toUpdate.Category = (int)file.FilterType; + toUpdate.CreateBy = file.CreateBy; + toUpdate.CreateOn = _tenantUtil.DateTimeToUtc(file.CreateOn); + toUpdate.ModifiedBy = file.ModifiedBy; + toUpdate.ModifiedOn = _tenantUtil.DateTimeToUtc(file.ModifiedOn); + toUpdate.ConvertedType = file.ConvertedType; + toUpdate.Comment = file.Comment; + toUpdate.Encrypted = file.Encrypted; + toUpdate.Forcesave = file.Forcesave; + toUpdate.ThumbnailStatus = file.ThumbnailStatus; + + context.Update(toUpdate); + await context.SaveChangesAsync(); - await tx.CommitAsync(); - }); + await tx.CommitAsync(); + }); - file.PureTitle = file.Title; + file.PureTitle = file.Title; - var parentFolders = await Queries.DbFolderTeesAsync(filesDbContext, file.ParentId).ToListAsync(); + var parentFolders = await Queries.DbFolderTeesAsync(filesDbContext, file.ParentId).ToListAsync(); - var parentFoldersIds = parentFolders.Select(r => r.ParentId).ToList(); + var parentFoldersIds = parentFolders.Select(r => r.ParentId).ToList(); - if (parentFoldersIds.Count > 0) - { - await Queries.UpdateFoldersAsync(filesDbContext, parentFoldersIds, _tenantUtil.DateTimeToUtc(file.ModifiedOn), file.ModifiedBy, tenantId); - } + if (parentFoldersIds.Count > 0) + { + await Queries.UpdateFoldersAsync(filesDbContext, parentFoldersIds, _tenantUtil.DateTimeToUtc(file.ModifiedOn), file.ModifiedBy, tenantId); + } - toUpdate.Folders = parentFolders; + toUpdate.Folders = parentFolders; } if (fileStream != null) @@ -700,13 +699,13 @@ private async Task DeleteVersionStreamAsync(File file) await store.DeleteDirectoryAsync(GetUniqFileVersionPath(file.Id, file.Version)); } - private async Task SaveFileStreamAsync(File file, Stream stream) + private async Task SaveFileStreamAsync(File file, Stream stream, Folder currentFolder = null) { var folderDao = daoFactory.GetFolderDao(); await (await globalStore.GetStoreAsync()).SaveAsync(string.Empty, GetUniqFilePath(file), file.GetFileQuotaOwner(), stream, file.Title); - var currentFolder = await folderDao.GetFolderAsync(file.FolderIdDisplay); + currentFolder ??= await folderDao.GetFolderAsync(file.FolderIdDisplay); await folderDao.ChangeTreeFolderSizeAsync(currentFolder.Id, file.ContentLength); } diff --git a/products/ASC.Files/Core/Services/DocumentService/Configuration.cs b/products/ASC.Files/Core/Services/DocumentService/Configuration.cs index f06f8a368e3..8ba92008e61 100644 --- a/products/ASC.Files/Core/Services/DocumentService/Configuration.cs +++ b/products/ASC.Files/Core/Services/DocumentService/Configuration.cs @@ -331,12 +331,19 @@ public async IAsyncEnumerable GetRecent(FileType fileType, T fileI }; var folderDao = daoFactory.GetFolderDao(); - var files = (await entryManager.GetRecentAsync(filter, false, Guid.Empty, string.Empty, null, false)).Cast>(); - foreach (var file in files.Where(file => !Equals(fileId, file.Id))) + var files = (await entryManager.GetRecentAsync(filter, false, Guid.Empty, string.Empty, null, false)) + .Cast>() + .Where(file => !Equals(fileId, file.Id)) + .ToList(); + + var parentIds = files.Select(r => r.ParentId).Distinct().ToList(); + var parentFolders = await folderDao.GetFoldersAsync(parentIds).ToListAsync(); + + foreach (var file in files) { yield return new RecentConfig { - Folder = (await folderDao.GetFolderAsync(file.ParentId)).Title, + Folder = parentFolders.Find(r => file.ParentId == r.Id).Title, Title = file.Title, Url = baseCommonLinkUtility.GetFullAbsolutePath(filesLinkUtility.GetFileWebEditorUrl(file.Id)) };