Merge pull request #1047 from chlupac/GetPath

Introduce GetFolderPath and GetFilePath repository methods
This commit is contained in:
Shaun Walker 2021-01-12 09:41:09 -05:00 committed by GitHub
commit edbdfe454e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 14 deletions

View File

@ -142,13 +142,13 @@ namespace Oqtane.Controllers
Models.File _file = _files.GetFile(id, false);
if (_file.Name != file.Name || _file.FolderId != file.FolderId)
{
string folderpath = GetFolderPath(file.Folder);
string folderpath = _folders.GetFolderPath(file.Folder);
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
System.IO.File.Move(Path.Combine(GetFolderPath(_file.Folder), _file.Name), Path.Combine(folderpath, file.Name));
System.IO.File.Move(_files.GetFilePath(_file), Path.Combine(folderpath, file.Name));
}
file.Extension = Path.GetExtension(file.Name).ToLower().Replace(".", "");
@ -177,7 +177,7 @@ namespace Oqtane.Controllers
{
_files.DeleteFile(id);
string filepath = Path.Combine(GetFolderPath(file.Folder), file.Name);
string filepath = _files.GetFilePath(file);
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
@ -213,7 +213,7 @@ namespace Oqtane.Controllers
return file;
}
string folderPath = GetFolderPath(folder);
string folderPath = _folders.GetFolderPath(folder);
CreateDirectory(folderPath);
string filename = url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1);
@ -280,7 +280,7 @@ namespace Oqtane.Controllers
if (virtualFolder != null &&
_userPermissions.IsAuthorized(User, PermissionNames.Edit, virtualFolder.Permissions))
{
folderPath = GetFolderPath(virtualFolder);
folderPath = _folders.GetFolderPath(virtualFolder);
}
}
else
@ -291,7 +291,7 @@ namespace Oqtane.Controllers
}
}
if (folderPath != "")
if (!String.IsNullOrEmpty(folderPath))
{
CreateDirectory(folderPath);
using (var stream = new FileStream(Path.Combine(folderPath, file.FileName), FileMode.Create))
@ -472,7 +472,7 @@ namespace Oqtane.Controllers
{
if (_userPermissions.IsAuthorized(User, PermissionNames.View, file.Folder.Permissions))
{
var filepath = Path.Combine(GetFolderPath(file.Folder), file.Name);
var filepath = _files.GetFilePath(file);
if (System.IO.File.Exists(filepath))
{
var result = asAttachment
@ -500,11 +500,6 @@ namespace Oqtane.Controllers
return System.IO.File.Exists(errorPath) ? PhysicalFile(errorPath, MimeUtilities.GetMimeType(errorPath)) : null;
}
private string GetFolderPath(Folder folder)
{
return Utilities.PathCombine(_environment.ContentRootPath, "Content", "Tenants", _tenants.GetTenant().TenantId.ToString(), "Sites", folder.SiteId.ToString(), folder.Path);
}
private string GetFolderPath(string folder)
{
return Utilities.PathCombine(_environment.WebRootPath, folder);

View File

@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Oqtane.Extensions;
using Oqtane.Models;
using Oqtane.Shared;
using File = Oqtane.Models.File;
namespace Oqtane.Repository
{
@ -11,11 +13,13 @@ namespace Oqtane.Repository
{
private TenantDBContext _db;
private readonly IPermissionRepository _permissions;
private readonly IFolderRepository _folderRepository;
public FileRepository(TenantDBContext context, IPermissionRepository permissions)
public FileRepository(TenantDBContext context, IPermissionRepository permissions, IFolderRepository folderRepository)
{
_db = context;
_permissions = permissions;
_folderRepository = folderRepository;
}
public IEnumerable<File> GetFiles(int folderId)
@ -74,5 +78,19 @@ namespace Oqtane.Repository
_db.File.Remove(file);
_db.SaveChanges();
}
public string GetFilePath(int fileId)
{
var file = _db.File.Find(fileId);
return GetFilePath(file);
}
public string GetFilePath(File file)
{
if (file == null) return null;
var folder = file.Folder ?? _db.Folder.Find(file.FolderId);
var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name);
return filepath;
}
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Oqtane.Extensions;
using Oqtane.Models;
@ -11,11 +12,15 @@ namespace Oqtane.Repository
{
private TenantDBContext _db;
private readonly IPermissionRepository _permissions;
private readonly IWebHostEnvironment _environment;
private readonly ITenantResolver _tenants;
public FolderRepository(TenantDBContext context, IPermissionRepository permissions)
public FolderRepository(TenantDBContext context, IPermissionRepository permissions,IWebHostEnvironment environment, ITenantResolver tenants)
{
_db = context;
_permissions = permissions;
_environment = environment;
_tenants = tenants;
}
public IEnumerable<Folder> GetFolders(int siteId)
@ -85,5 +90,17 @@ namespace Oqtane.Repository
_db.Folder.Remove(folder);
_db.SaveChanges();
}
public string GetFolderPath(int folderId)
{
Folder folder = _db.Folder.Find(folderId);
return GetFolderPath(folder);
}
public string GetFolderPath(Folder folder)
{
return Utilities.PathCombine(_environment.ContentRootPath, "Content", "Tenants", _tenants.GetTenant().TenantId.ToString(), "Sites", folder.SiteId.ToString(), folder.Path);
}
}
}

View File

@ -11,5 +11,7 @@ namespace Oqtane.Repository
File GetFile(int fileId);
File GetFile(int fileId, bool tracking);
void DeleteFile(int fileId);
string GetFilePath(int fileId);
string GetFilePath(File file);
}
}

View File

@ -12,5 +12,7 @@ namespace Oqtane.Repository
Folder GetFolder(int folderId, bool tracking);
Folder GetFolder(int siteId, string path);
void DeleteFolder(int folderId);
string GetFolderPath(int folderId);
string GetFolderPath(Folder folder);
}
}