GetFiles and GetFolder by folder path

This commit is contained in:
Pavel Vesely
2020-03-07 01:36:25 +01:00
parent b9b89e6046
commit 92444ccf75
6 changed files with 104 additions and 5 deletions

View File

@ -1,9 +1,11 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.Shared; using Oqtane.Shared;
@ -12,12 +14,14 @@ namespace Oqtane.Services
{ {
public class FileService : ServiceBase, IFileService public class FileService : ServiceBase, IFileService
{ {
private readonly ILogger<FileService> _logger;
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly SiteState _siteState; private readonly SiteState _siteState;
private readonly NavigationManager _navigationManager; private readonly NavigationManager _navigationManager;
private readonly IJSRuntime _jsRuntime; 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<FileService> Logger)
{ {
_http = http; _http = http;
_siteState = siteState; _siteState = siteState;
@ -40,6 +44,21 @@ namespace Oqtane.Services
return await _http.GetJsonAsync<List<File>>(apiurl + "?folder=" + Folder); return await _http.GetJsonAsync<List<File>>(apiurl + "?folder=" + Folder);
} }
public async Task<List<File>> GetFilesAsync(int siteId, string folderPath)
{
try
{
if (!folderPath.EndsWith("\\")) folderPath += "\\";
var path = WebUtility.UrlEncode(folderPath);
return await _http.GetJsonAsync<List<File>>($"{apiurl}/{siteId}/{path}");
}
catch (Exception e)
{
_logger.LogDebug(e,"Folder not found: {path}");
}
return null;
}
public async Task<File> GetFileAsync(int FileId) public async Task<File> GetFileAsync(int FileId)
{ {
return await _http.GetJsonAsync<File>(apiurl + "/" + FileId.ToString()); return await _http.GetJsonAsync<File>(apiurl + "/" + FileId.ToString());

View File

@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Components;
using System.Collections.Generic; using System.Collections.Generic;
using Oqtane.Shared; using Oqtane.Shared;
using System; using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Microsoft.Extensions.Logging;
namespace Oqtane.Services namespace Oqtane.Services
{ {
@ -14,12 +17,14 @@ namespace Oqtane.Services
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly SiteState _siteState; private readonly SiteState _siteState;
private readonly NavigationManager _navigationManager; private readonly NavigationManager _navigationManager;
private readonly ILogger<FolderService> _logger;
public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager)
public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ILogger<FolderService> logger)
{ {
_http = http; _http = http;
_siteState = siteState; _siteState = siteState;
_navigationManager = navigationManager; _navigationManager = navigationManager;
_logger = logger;
} }
private string apiurl private string apiurl
@ -38,6 +43,21 @@ namespace Oqtane.Services
{ {
return await _http.GetJsonAsync<Folder>(apiurl + "/" + FolderId.ToString()); return await _http.GetJsonAsync<Folder>(apiurl + "/" + FolderId.ToString());
} }
public async Task<Folder> GetFolderAsync(int siteId, [NotNull]string folderPath)
{
try
{
if (!folderPath.EndsWith("\\")) folderPath += "\\";
var path = WebUtility.UrlEncode(folderPath);
return await _http.GetJsonAsync<Folder>($"{apiurl}/{siteId}/{path}");
}
catch (Exception e)
{
_logger.LogDebug(e,"Folder not found: {path}");
}
return null;
}
public async Task<Folder> AddFolderAsync(Folder Folder) public async Task<Folder> AddFolderAsync(Folder Folder)
{ {

View File

@ -17,5 +17,6 @@ namespace Oqtane.Services
Task<string> UploadFilesAsync(string Folder, string[] Files, string FileUploadName); Task<string> UploadFilesAsync(string Folder, string[] Files, string FileUploadName);
Task<byte[]> DownloadFileAsync(int FileId); Task<byte[]> DownloadFileAsync(int FileId);
Task<List<File>> GetFilesAsync(int siteId, string folderPath);
} }
} }

View File

@ -1,5 +1,6 @@
using Oqtane.Models; using Oqtane.Models;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Oqtane.Services namespace Oqtane.Services
@ -12,5 +13,6 @@ namespace Oqtane.Services
Task<Folder> UpdateFolderAsync(Folder Folder); Task<Folder> UpdateFolderAsync(Folder Folder);
Task UpdateFolderOrderAsync(int SiteId, int FolderId, int? ParentId); Task UpdateFolderOrderAsync(int SiteId, int FolderId, int? ParentId);
Task DeleteFolderAsync(int FolderId); Task DeleteFolderAsync(int FolderId);
Task<Folder> GetFolderAsync(int siteId, [NotNull]string folderPath);
} }
} }

View File

@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -14,6 +14,7 @@ using System.Threading.Tasks;
using Oqtane.Security; using Oqtane.Security;
using System.Linq; using System.Linq;
using System.Drawing; using System.Drawing;
using System.Net;
namespace Oqtane.Controllers namespace Oqtane.Controllers
{ {
@ -67,6 +68,35 @@ namespace Oqtane.Controllers
} }
return files; return files;
} }
// GET: api/<controller>/siteId/folderPath
[HttpGet("{siteId}/{path}")]
public IEnumerable<Models.File> Get(int siteId, string path)
{
var folderPath = WebUtility.UrlDecode(path);
Folder folder = _folders.GetFolder(siteId, folderPath);
List<Models.File> 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/<controller>/5 // GET api/<controller>/5
[HttpGet("{id}")] [HttpGet("{id}")]

View File

@ -5,6 +5,7 @@ using Oqtane.Repository;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.Shared; using Oqtane.Shared;
using System.Linq; using System.Linq;
using System.Net;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Security; 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/<controller> // POST api/<controller>
[HttpPost] [HttpPost]
[Authorize(Roles = Constants.RegisteredRole)] [Authorize(Roles = Constants.RegisteredRole)]