Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #2596 - fix EF Core tracking error when updating a file in a folder which has a Capacity specified #2597

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Oqtane.Server/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Png;
using System.Net.Http;
using Oqtane.Migrations.Tenant;

// ReSharper disable StringIndexOfIsCultureSpecific.1

Expand Down Expand Up @@ -142,7 +141,7 @@ public Models.File Put(int id, [FromBody] Models.File file)
{
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);
if (!Directory.Exists(folderpath))
{
Expand All @@ -151,7 +150,7 @@ public Models.File Put(int id, [FromBody] Models.File file)
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)
{
file.Extension = newfile.Extension;
Expand Down Expand Up @@ -659,10 +658,10 @@ private Models.File CreateFile(string filename, int folderid, string filepath)
var file = _files.GetFile(folderid, filename);

int size = 0;
var folder = _folders.GetFolder(folderid);
var folder = _folders.GetFolder(folderid, false);
if (folder.Capacity != 0)
{
foreach (var f in _files.GetFiles(folderid))
foreach (var f in _files.GetFiles(folderid, false))
{
size += f.Size;
}
Expand Down
18 changes: 15 additions & 3 deletions Oqtane.Server/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -27,10 +26,23 @@ public FileRepository(TenantDBContext context, IPermissionRepository permissions
}

public IEnumerable<File> GetFiles(int folderId)
{
return GetFiles(folderId, true);
}

public IEnumerable<File> GetFiles(int folderId, bool tracking)
{
var alias = _tenants.GetAlias();
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)
{
file.Folder.Permissions = permissions.EncodePermissions();
Expand Down Expand Up @@ -135,7 +147,7 @@ public string GetFilePath(int fileId)
public string GetFilePath(File file)
{
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);
return filepath;
}
Expand Down
1 change: 1 addition & 0 deletions Oqtane.Server/Repository/Interfaces/IFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Oqtane.Repository
public interface IFileRepository
{
IEnumerable<File> GetFiles(int folderId);
IEnumerable<File> GetFiles(int folderId, bool tracking);
File AddFile(File file);
File UpdateFile(File file);
File GetFile(int fileId);
Expand Down