From 92444ccf759ceb766bf42619ad1a1d3b2fc23927 Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Sat, 7 Mar 2020 01:36:25 +0100 Subject: [PATCH 1/4] GetFiles and GetFolder by folder path --- Oqtane.Client/Services/FileService.cs | 23 +++++++++++-- Oqtane.Client/Services/FolderService.cs | 24 ++++++++++++-- .../Services/Interfaces/IFileService.cs | 1 + .../Services/Interfaces/IFolderService.cs | 2 ++ Oqtane.Server/Controllers/FileController.cs | 32 ++++++++++++++++++- Oqtane.Server/Controllers/FolderController.cs | 27 ++++++++++++++++ 6 files changed, 104 insertions(+), 5 deletions(-) diff --git a/Oqtane.Client/Services/FileService.cs b/Oqtane.Client/Services/FileService.cs index 3a3bc8a8..d377d62e 100644 --- a/Oqtane.Client/Services/FileService.cs +++ b/Oqtane.Client/Services/FileService.cs @@ -1,9 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Logging; using Microsoft.JSInterop; using Oqtane.Models; using Oqtane.Shared; @@ -12,12 +14,14 @@ namespace Oqtane.Services { public class FileService : ServiceBase, IFileService { + + private readonly ILogger _logger; private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; private readonly IJSRuntime _jsRuntime; - public FileService(HttpClient http, SiteState siteState, NavigationManager navigationManager, IJSRuntime jsRuntime) + public FileService(HttpClient http, SiteState siteState, NavigationManager navigationManager, IJSRuntime jsRuntime, ILogger Logger) { _http = http; _siteState = siteState; @@ -40,6 +44,21 @@ namespace Oqtane.Services return await _http.GetJsonAsync>(apiurl + "?folder=" + Folder); } + public async Task> GetFilesAsync(int siteId, string folderPath) + { + try + { + if (!folderPath.EndsWith("\\")) folderPath += "\\"; + var path = WebUtility.UrlEncode(folderPath); + return await _http.GetJsonAsync>($"{apiurl}/{siteId}/{path}"); + } + catch (Exception e) + { + _logger.LogDebug(e,"Folder not found: {path}"); + } + return null; + } + public async Task GetFileAsync(int FileId) { return await _http.GetJsonAsync(apiurl + "/" + FileId.ToString()); diff --git a/Oqtane.Client/Services/FolderService.cs b/Oqtane.Client/Services/FolderService.cs index 762ff7d8..630231fe 100644 --- a/Oqtane.Client/Services/FolderService.cs +++ b/Oqtane.Client/Services/FolderService.cs @@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Components; using System.Collections.Generic; using Oqtane.Shared; using System; +using System.Diagnostics.CodeAnalysis; +using System.Net; +using Microsoft.Extensions.Logging; namespace Oqtane.Services { @@ -14,12 +17,14 @@ namespace Oqtane.Services private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - - public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + private readonly ILogger _logger; + + public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ILogger logger) { _http = http; _siteState = siteState; _navigationManager = navigationManager; + _logger = logger; } private string apiurl @@ -38,6 +43,21 @@ namespace Oqtane.Services { return await _http.GetJsonAsync(apiurl + "/" + FolderId.ToString()); } + + public async Task GetFolderAsync(int siteId, [NotNull]string folderPath) + { + try + { + if (!folderPath.EndsWith("\\")) folderPath += "\\"; + var path = WebUtility.UrlEncode(folderPath); + return await _http.GetJsonAsync($"{apiurl}/{siteId}/{path}"); + } + catch (Exception e) + { + _logger.LogDebug(e,"Folder not found: {path}"); + } + return null; + } public async Task AddFolderAsync(Folder Folder) { diff --git a/Oqtane.Client/Services/Interfaces/IFileService.cs b/Oqtane.Client/Services/Interfaces/IFileService.cs index 2a6234b6..c3107e6a 100644 --- a/Oqtane.Client/Services/Interfaces/IFileService.cs +++ b/Oqtane.Client/Services/Interfaces/IFileService.cs @@ -17,5 +17,6 @@ namespace Oqtane.Services Task UploadFilesAsync(string Folder, string[] Files, string FileUploadName); Task DownloadFileAsync(int FileId); + Task> GetFilesAsync(int siteId, string folderPath); } } diff --git a/Oqtane.Client/Services/Interfaces/IFolderService.cs b/Oqtane.Client/Services/Interfaces/IFolderService.cs index d91e253e..bbad463c 100644 --- a/Oqtane.Client/Services/Interfaces/IFolderService.cs +++ b/Oqtane.Client/Services/Interfaces/IFolderService.cs @@ -1,5 +1,6 @@ using Oqtane.Models; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; namespace Oqtane.Services @@ -12,5 +13,6 @@ namespace Oqtane.Services Task UpdateFolderAsync(Folder Folder); Task UpdateFolderOrderAsync(int SiteId, int FolderId, int? ParentId); Task DeleteFolderAsync(int FolderId); + Task GetFolderAsync(int siteId, [NotNull]string folderPath); } } diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 418c0829..35da2162 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -14,6 +14,7 @@ using System.Threading.Tasks; using Oqtane.Security; using System.Linq; using System.Drawing; +using System.Net; namespace Oqtane.Controllers { @@ -67,6 +68,35 @@ namespace Oqtane.Controllers } return files; } + + // GET: api//siteId/folderPath + [HttpGet("{siteId}/{path}")] + public IEnumerable Get(int siteId, string path) + { + var folderPath = WebUtility.UrlDecode(path); + Folder folder = _folders.GetFolder(siteId, folderPath); + List files; + if (folder != null) + if (_userPermissions.IsAuthorized(User, "Browse", folder.Permissions)) + { + files = _files.GetFiles(folder.FolderId).ToList(); + } + else + { + _logger.Log(LogLevel.Error, this, LogFunction.Read, "User Not Authorized To Access Folder {folder}", + folder); + HttpContext.Response.StatusCode = 401; + return null; + } + else + { + _logger.Log(LogLevel.Error, this, LogFunction.Read, "Folder not found {path}", + path); + HttpContext.Response.StatusCode = 401; + return null; + } + return files; + } // GET api//5 [HttpGet("{id}")] diff --git a/Oqtane.Server/Controllers/FolderController.cs b/Oqtane.Server/Controllers/FolderController.cs index cbb48fb8..4b076e7f 100644 --- a/Oqtane.Server/Controllers/FolderController.cs +++ b/Oqtane.Server/Controllers/FolderController.cs @@ -5,6 +5,7 @@ using Oqtane.Repository; using Oqtane.Models; using Oqtane.Shared; using System.Linq; +using System.Net; using Oqtane.Infrastructure; using Oqtane.Security; @@ -56,6 +57,32 @@ namespace Oqtane.Controllers } } + [HttpGet("{siteId}/{path}")] + public Folder GetByPath(int siteId, string path) + { + var folderPath = WebUtility.UrlDecode(path); + Folder folder = _folders.GetFolder(siteId, folderPath); + if (folder != null) + if (_userPermissions.IsAuthorized(User, "Browse", folder.Permissions)) + { + return folder; + } + else + { + _logger.Log(LogLevel.Error, this, LogFunction.Read, "User Not Authorized To Access Folder {Folder}", + folder); + HttpContext.Response.StatusCode = 401; + return null; + } + else + { + _logger.Log(LogLevel.Error, this, LogFunction.Read, "Folder not found {path}", + path); + HttpContext.Response.StatusCode = 401; + return null; + } + } + // POST api/ [HttpPost] [Authorize(Roles = Constants.RegisteredRole)] From 563580881311569627a7193de1fecb2744586703 Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Sat, 7 Mar 2020 01:37:52 +0100 Subject: [PATCH 2/4] GetFiles and GetFolder by folder path --- Oqtane.Server/Controllers/FileController.cs | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 35da2162..bf19bd64 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -40,14 +40,14 @@ namespace Oqtane.Controllers // GET: api/?folder=x [HttpGet] - public IEnumerable Get(string folder) + public IEnumerable Get(string path) { List files = new List(); int folderid; - if (int.TryParse(folder, out folderid)) + if (int.TryParse(path, out folderid)) { - Folder Folder = _folders.GetFolder(folderid); - if (Folder != null && _userPermissions.IsAuthorized(User, "Browse", Folder.Permissions)) + Folder folder = _folders.GetFolder(folderid); + if (folder != null && _userPermissions.IsAuthorized(User, "Browse", folder.Permissions)) { files = _files.GetFiles(folderid).ToList(); } @@ -56,10 +56,10 @@ namespace Oqtane.Controllers { if (User.IsInRole(Constants.HostRole)) { - folder = GetFolderPath(folder); - if (Directory.Exists(folder)) + path = GetFolderPath(path); + if (Directory.Exists(path)) { - foreach (string file in Directory.GetFiles(folder)) + foreach (string file in Directory.GetFiles(path)) { files.Add(new Models.File { Name = Path.GetFileName(file), Extension = Path.GetExtension(file).Replace(".","") }); } @@ -139,17 +139,17 @@ namespace Oqtane.Controllers [Authorize(Roles = Constants.RegisteredRole)] public void Delete(int id) { - Models.File File = _files.GetFile(id); - if (_userPermissions.IsAuthorized(User, "Folder", File.Folder.FolderId, "Edit")) + Models.File file = _files.GetFile(id); + if (_userPermissions.IsAuthorized(User, "Folder", file.Folder.FolderId, "Edit")) { _files.DeleteFile(id); - string filepath = Path.Combine(GetFolderPath(File.Folder) + File.Name); + string filepath = Path.Combine(GetFolderPath(file.Folder) + file.Name); if (System.IO.File.Exists(filepath)) { System.IO.File.Delete(filepath); } - _logger.Log(LogLevel.Information, this, LogFunction.Delete, "File Deleted {File}", File); + _logger.Log(LogLevel.Information, this, LogFunction.Delete, "File Deleted {File}", file); } else { @@ -440,4 +440,4 @@ namespace Oqtane.Controllers return file; } } -} \ No newline at end of file +} From 979463b3651fca657fd5ca2244ac3770523535bd Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Sun, 8 Mar 2020 22:55:42 +0100 Subject: [PATCH 3/4] FileController parameter fix --- Oqtane.Server/Controllers/FileController.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index bf19bd64..d1606eaa 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -40,14 +40,14 @@ namespace Oqtane.Controllers // GET: api/?folder=x [HttpGet] - public IEnumerable Get(string path) + public IEnumerable Get(string folder) { List files = new List(); int folderid; - if (int.TryParse(path, out folderid)) + if (int.TryParse(folder, out folderid)) { - Folder folder = _folders.GetFolder(folderid); - if (folder != null && _userPermissions.IsAuthorized(User, "Browse", folder.Permissions)) + Folder f = _folders.GetFolder(folderid); + if (f != null && _userPermissions.IsAuthorized(User, "Browse", f.Permissions)) { files = _files.GetFiles(folderid).ToList(); } @@ -56,10 +56,10 @@ namespace Oqtane.Controllers { if (User.IsInRole(Constants.HostRole)) { - path = GetFolderPath(path); - if (Directory.Exists(path)) + folder = GetFolderPath(folder); + if (Directory.Exists(folder)) { - foreach (string file in Directory.GetFiles(path)) + foreach (string file in Directory.GetFiles(folder)) { files.Add(new Models.File { Name = Path.GetFileName(file), Extension = Path.GetExtension(file).Replace(".","") }); } From 5d575c95cab910641fc1e7a10cf064d2c72ca185 Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Mon, 9 Mar 2020 19:04:34 +0100 Subject: [PATCH 4/4] ILogger reference removed --- Oqtane.Client/Services/FileService.cs | 27 +++++-------- Oqtane.Client/Services/FolderService.cs | 50 ++++++++++--------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/Oqtane.Client/Services/FileService.cs b/Oqtane.Client/Services/FileService.cs index d377d62e..265ccb67 100644 --- a/Oqtane.Client/Services/FileService.cs +++ b/Oqtane.Client/Services/FileService.cs @@ -5,7 +5,6 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; -using Microsoft.Extensions.Logging; using Microsoft.JSInterop; using Oqtane.Models; using Oqtane.Shared; @@ -14,14 +13,13 @@ namespace Oqtane.Services { public class FileService : ServiceBase, IFileService { - - private readonly ILogger _logger; private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; private readonly IJSRuntime _jsRuntime; - public FileService(HttpClient http, SiteState siteState, NavigationManager navigationManager, IJSRuntime jsRuntime, ILogger Logger) + public FileService(HttpClient http, SiteState siteState, NavigationManager navigationManager, + IJSRuntime jsRuntime) { _http = http; _siteState = siteState; @@ -46,19 +44,11 @@ namespace Oqtane.Services public async Task> GetFilesAsync(int siteId, string folderPath) { - try - { - if (!folderPath.EndsWith("\\")) folderPath += "\\"; - var path = WebUtility.UrlEncode(folderPath); - return await _http.GetJsonAsync>($"{apiurl}/{siteId}/{path}"); - } - catch (Exception e) - { - _logger.LogDebug(e,"Folder not found: {path}"); - } - return null; + if (!folderPath.EndsWith("\\")) folderPath += "\\"; + var path = WebUtility.UrlEncode(folderPath); + return await _http.GetJsonAsync>($"{apiurl}/{siteId}/{path}"); } - + public async Task GetFileAsync(int FileId) { return await _http.GetJsonAsync(apiurl + "/" + FileId.ToString()); @@ -81,7 +71,8 @@ namespace Oqtane.Services public async Task UploadFileAsync(string Url, int FolderId) { - return await _http.GetJsonAsync(apiurl + "/upload?url=" + WebUtility.UrlEncode(Url) + "&folderid=" + FolderId.ToString()); + return await _http.GetJsonAsync(apiurl + "/upload?url=" + WebUtility.UrlEncode(Url) + "&folderid=" + + FolderId.ToString()); } public async Task UploadFilesAsync(int FolderId, string[] Files, string Id) @@ -117,8 +108,10 @@ namespace Oqtane.Services } } } + attempts += 1; } + if (!success) { result = result.Substring(0, result.Length - 1); diff --git a/Oqtane.Client/Services/FolderService.cs b/Oqtane.Client/Services/FolderService.cs index 630231fe..a457b021 100644 --- a/Oqtane.Client/Services/FolderService.cs +++ b/Oqtane.Client/Services/FolderService.cs @@ -8,7 +8,6 @@ using Oqtane.Shared; using System; using System.Diagnostics.CodeAnalysis; using System.Net; -using Microsoft.Extensions.Logging; namespace Oqtane.Services { @@ -17,66 +16,55 @@ namespace Oqtane.Services private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - private readonly ILogger _logger; - - public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ILogger logger) + + public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager) { _http = http; _siteState = siteState; _navigationManager = navigationManager; - _logger = logger; } - private string apiurl - { - get { return CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "Folder"); } - } + private string ApiUrl => CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "Folder"); public async Task> GetFoldersAsync(int SiteId) { - List folders = await _http.GetJsonAsync>(apiurl + "?siteid=" + SiteId.ToString()); + List folders = await _http.GetJsonAsync>(ApiUrl + "?siteid=" + SiteId.ToString()); folders = GetFoldersHierarchy(folders); return folders; } public async Task GetFolderAsync(int FolderId) { - return await _http.GetJsonAsync(apiurl + "/" + FolderId.ToString()); + return await _http.GetJsonAsync(ApiUrl + "/" + FolderId.ToString()); } - - public async Task GetFolderAsync(int siteId, [NotNull]string folderPath) + + public async Task GetFolderAsync(int siteId, [NotNull] string folderPath) { - try - { - if (!folderPath.EndsWith("\\")) folderPath += "\\"; - var path = WebUtility.UrlEncode(folderPath); - return await _http.GetJsonAsync($"{apiurl}/{siteId}/{path}"); - } - catch (Exception e) - { - _logger.LogDebug(e,"Folder not found: {path}"); - } - return null; + if (!folderPath.EndsWith("\\")) folderPath += "\\"; + var path = WebUtility.UrlEncode(folderPath); + return await _http.GetJsonAsync($"{ApiUrl}/{siteId}/{path}"); } public async Task AddFolderAsync(Folder Folder) { - return await _http.PostJsonAsync(apiurl, Folder); + return await _http.PostJsonAsync(ApiUrl, Folder); } public async Task UpdateFolderAsync(Folder Folder) { - return await _http.PutJsonAsync(apiurl + "/" + Folder.FolderId.ToString(), Folder); + return await _http.PutJsonAsync(ApiUrl + "/" + Folder.FolderId.ToString(), Folder); } public async Task UpdateFolderOrderAsync(int SiteId, int FolderId, int? ParentId) { - await _http.PutJsonAsync(apiurl + "/?siteid=" + SiteId.ToString() + "&folderid=" + FolderId.ToString() + "&parentid=" + ((ParentId == null) ? "" : ParentId.ToString()), null); + await _http.PutJsonAsync( + ApiUrl + "/?siteid=" + SiteId.ToString() + "&folderid=" + FolderId.ToString() + "&parentid=" + + ((ParentId == null) ? "" : ParentId.ToString()), null); } public async Task DeleteFolderAsync(int FolderId) { - await _http.DeleteAsync(apiurl + "/" + FolderId.ToString()); + await _http.DeleteAsync(ApiUrl + "/" + FolderId.ToString()); } private static List GetFoldersHierarchy(List Folders) @@ -97,10 +85,11 @@ namespace Oqtane.Services level = folder.Level; children = Folders.Where(item => item.ParentId == folder.FolderId); } + foreach (Folder child in children) { child.Level = level + 1; - child.HasChildren = Folders.Where(item => item.ParentId == child.FolderId).Any(); + child.HasChildren = Folders.Any(item => item.ParentId == child.FolderId); hierarchy.Add(child); GetPath(folders, child); } @@ -109,13 +98,14 @@ namespace Oqtane.Services GetPath(Folders, null); // add any non-hierarchical items to the end of the list - foreach(Folder folder in Folders) + foreach (Folder folder in Folders) { if (hierarchy.Find(item => item.FolderId == folder.FolderId) == null) { hierarchy.Add(folder); } } + return hierarchy; } }