Merge pull request #21 from oqtane/master

sync with upstream
This commit is contained in:
Shaun Walker
2020-03-09 15:41:01 -04:00
committed by GitHub
6 changed files with 104 additions and 22 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
@ -17,7 +18,8 @@ namespace Oqtane.Services
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)
{
_http = http;
_siteState = siteState;
@ -40,6 +42,13 @@ namespace Oqtane.Services
return await _http.GetJsonAsync<List<File>>(apiurl + "?folder=" + Folder);
}
public async Task<List<File>> GetFilesAsync(int siteId, string folderPath)
{
if (!folderPath.EndsWith("\\")) folderPath += "\\";
var path = WebUtility.UrlEncode(folderPath);
return await _http.GetJsonAsync<List<File>>($"{apiurl}/{siteId}/{path}");
}
public async Task<File> GetFileAsync(int FileId)
{
return await _http.GetJsonAsync<File>(apiurl + "/" + FileId.ToString());
@ -62,7 +71,8 @@ namespace Oqtane.Services
public async Task<File> UploadFileAsync(string Url, int FolderId)
{
return await _http.GetJsonAsync<File>(apiurl + "/upload?url=" + WebUtility.UrlEncode(Url) + "&folderid=" + FolderId.ToString());
return await _http.GetJsonAsync<File>(apiurl + "/upload?url=" + WebUtility.UrlEncode(Url) + "&folderid=" +
FolderId.ToString());
}
public async Task<string> UploadFilesAsync(int FolderId, string[] Files, string Id)
@ -98,8 +108,10 @@ namespace Oqtane.Services
}
}
}
attempts += 1;
}
if (!success)
{
result = result.Substring(0, result.Length - 1);

View File

@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using Oqtane.Shared;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
namespace Oqtane.Services
{
@ -22,41 +24,47 @@ namespace Oqtane.Services
_navigationManager = navigationManager;
}
private string apiurl
{
get { return CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "Folder"); }
}
private string ApiUrl => CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "Folder");
public async Task<List<Folder>> GetFoldersAsync(int SiteId)
{
List<Folder> folders = await _http.GetJsonAsync<List<Folder>>(apiurl + "?siteid=" + SiteId.ToString());
List<Folder> folders = await _http.GetJsonAsync<List<Folder>>(ApiUrl + "?siteid=" + SiteId.ToString());
folders = GetFoldersHierarchy(folders);
return folders;
}
public async Task<Folder> GetFolderAsync(int FolderId)
{
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)
{
if (!folderPath.EndsWith("\\")) folderPath += "\\";
var path = WebUtility.UrlEncode(folderPath);
return await _http.GetJsonAsync<Folder>($"{ApiUrl}/{siteId}/{path}");
}
public async Task<Folder> AddFolderAsync(Folder Folder)
{
return await _http.PostJsonAsync<Folder>(apiurl, Folder);
return await _http.PostJsonAsync<Folder>(ApiUrl, Folder);
}
public async Task<Folder> UpdateFolderAsync(Folder Folder)
{
return await _http.PutJsonAsync<Folder>(apiurl + "/" + Folder.FolderId.ToString(), Folder);
return await _http.PutJsonAsync<Folder>(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<Folder> GetFoldersHierarchy(List<Folder> Folders)
@ -77,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);
}
@ -89,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;
}
}

View File

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

View File

@ -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<Folder> UpdateFolderAsync(Folder Folder);
Task UpdateFolderOrderAsync(int SiteId, int FolderId, int? ParentId);
Task DeleteFolderAsync(int FolderId);
Task<Folder> GetFolderAsync(int siteId, [NotNull]string folderPath);
}
}

View File

@ -14,6 +14,7 @@ using System.Threading.Tasks;
using Oqtane.Security;
using System.Linq;
using System.Drawing;
using System.Net;
namespace Oqtane.Controllers
{
@ -45,8 +46,8 @@ namespace Oqtane.Controllers
int 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();
}
@ -67,6 +68,35 @@ namespace Oqtane.Controllers
}
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
[HttpGet("{id}")]
@ -109,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
{
@ -410,4 +440,4 @@ namespace Oqtane.Controllers
return file;
}
}
}
}

View File

@ -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/<controller>
[HttpPost]
[Authorize(Roles = Constants.RegisteredRole)]