Merge pull request #2597 from sbwalker/dev

fix #2596 - fix EF Core tracking error when updating a file in a folder which has a Capacity specified
This commit is contained in:
Shaun Walker 2023-02-15 12:42:00 -05:00 committed by GitHub
commit ebe03e9310
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View File

@ -20,7 +20,6 @@ using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Png;
using System.Net.Http; using System.Net.Http;
using Oqtane.Migrations.Tenant;
// ReSharper disable StringIndexOfIsCultureSpecific.1 // ReSharper disable StringIndexOfIsCultureSpecific.1
@ -142,7 +141,7 @@ namespace Oqtane.Controllers
{ {
if (File.Name != file.Name || File.FolderId != file.FolderId) if (File.Name != file.Name || File.FolderId != file.FolderId)
{ {
file.Folder = _folders.GetFolder(file.FolderId); file.Folder = _folders.GetFolder(file.FolderId, false);
string folderpath = _folders.GetFolderPath(file.Folder); string folderpath = _folders.GetFolderPath(file.Folder);
if (!Directory.Exists(folderpath)) if (!Directory.Exists(folderpath))
{ {
@ -151,7 +150,7 @@ namespace Oqtane.Controllers
System.IO.File.Move(_files.GetFilePath(File), Path.Combine(folderpath, file.Name)); System.IO.File.Move(_files.GetFilePath(File), Path.Combine(folderpath, file.Name));
} }
var newfile = CreateFile(file.Name, file.Folder.FolderId, _files.GetFilePath(file)); var newfile = CreateFile(File.Name, file.Folder.FolderId, _files.GetFilePath(file));
if (newfile != null) if (newfile != null)
{ {
file.Extension = newfile.Extension; file.Extension = newfile.Extension;
@ -659,10 +658,10 @@ namespace Oqtane.Controllers
var file = _files.GetFile(folderid, filename); var file = _files.GetFile(folderid, filename);
int size = 0; int size = 0;
var folder = _folders.GetFolder(folderid); var folder = _folders.GetFolder(folderid, false);
if (folder.Capacity != 0) if (folder.Capacity != 0)
{ {
foreach (var f in _files.GetFiles(folderid)) foreach (var f in _files.GetFiles(folderid, false))
{ {
size += f.Size; size += f.Size;
} }

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -27,10 +26,23 @@ namespace Oqtane.Repository
} }
public IEnumerable<File> GetFiles(int folderId) public IEnumerable<File> GetFiles(int folderId)
{
return GetFiles(folderId, true);
}
public IEnumerable<File> GetFiles(int folderId, bool tracking)
{ {
var alias = _tenants.GetAlias(); var alias = _tenants.GetAlias();
IEnumerable<Permission> permissions = _permissions.GetPermissions(alias.SiteId, EntityNames.Folder, folderId).ToList(); IEnumerable<Permission> permissions = _permissions.GetPermissions(alias.SiteId, EntityNames.Folder, folderId).ToList();
IEnumerable<File> files = _db.File.Where(item => item.FolderId == folderId).Include(item => item.Folder); IEnumerable<File> files;
if (tracking)
{
files = _db.File.Where(item => item.FolderId == folderId).Include(item => item.Folder);
}
else
{
files = _db.File.AsNoTracking().Where(item => item.FolderId == folderId).Include(item => item.Folder);
}
foreach (File file in files) foreach (File file in files)
{ {
file.Folder.Permissions = permissions.EncodePermissions(); file.Folder.Permissions = permissions.EncodePermissions();
@ -135,7 +147,7 @@ namespace Oqtane.Repository
public string GetFilePath(File file) public string GetFilePath(File file)
{ {
if (file == null) return null; if (file == null) return null;
var folder = file.Folder ?? _db.Folder.Find(file.FolderId); var folder = file.Folder ?? _db.Folder.AsNoTracking().FirstOrDefault(item => item.FolderId == file.FolderId);
var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name); var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name);
return filepath; return filepath;
} }

View File

@ -6,6 +6,7 @@ namespace Oqtane.Repository
public interface IFileRepository public interface IFileRepository
{ {
IEnumerable<File> GetFiles(int folderId); IEnumerable<File> GetFiles(int folderId);
IEnumerable<File> GetFiles(int folderId, bool tracking);
File AddFile(File file); File AddFile(File file);
File UpdateFile(File file); File UpdateFile(File file);
File GetFile(int fileId); File GetFile(int fileId);