finished files page
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="WebDav.Client" Version="2.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
namespace BetterIServ.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class HelperController : Controller {
|
||||
public class HelperController : ControllerBase {
|
||||
|
||||
[HttpPost("/login")]
|
||||
public async Task<ActionResult<string>> Login([FromForm] string email, [FromForm] string password) {
|
||||
@@ -34,7 +34,7 @@ public class HelperController : Controller {
|
||||
|
||||
return Ok(part.Replace("IServAuthSession=", ""));
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (Exception) {
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
115
BetterIServ.Backend/Controllers/WebDavController.cs
Normal file
115
BetterIServ.Backend/Controllers/WebDavController.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System.Net;
|
||||
using BetterIServ.Backend.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebDav;
|
||||
|
||||
namespace BetterIServ.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("webdav")]
|
||||
public class WebDavController : ControllerBase {
|
||||
|
||||
[HttpPost("content")]
|
||||
public async Task<ActionResult<DirectoryContent[]>> GetDirContent([FromBody] Credentials credentials, [FromQuery] string dir) {
|
||||
var baseAddress = new Uri($"https://webdav.{credentials.Domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(credentials.Username, credentials.Password)
|
||||
});
|
||||
|
||||
var result = await client.Propfind(baseAddress + dir);
|
||||
if (!result.IsSuccessful) return NotFound(result.Description);
|
||||
|
||||
var contents = new List<DirectoryContent>();
|
||||
foreach (var resource in result.Resources) {
|
||||
var name = resource.Uri.Split("/")[^1];
|
||||
if (resource.Uri.EndsWith("/"))
|
||||
name = resource.Uri.Split("/")[^2];
|
||||
|
||||
var content = new DirectoryContent {
|
||||
Url = resource.Uri,
|
||||
LastModified = resource.LastModifiedDate ?? DateTime.Now,
|
||||
Size = resource.ContentLength ?? 0,
|
||||
Type = resource.IsCollection ? "dir" : "file",
|
||||
Name = name
|
||||
};
|
||||
contents.Add(content);
|
||||
}
|
||||
contents.RemoveAt(0);
|
||||
|
||||
return contents.OrderBy(item => item.Type).ToArray();
|
||||
}
|
||||
|
||||
[HttpPost("download")]
|
||||
public async Task<FileStreamResult> DonwloadFile([FromBody] Credentials credentials, [FromQuery] string url) {
|
||||
var baseAddress = new Uri($"https://webdav.{credentials.Domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(credentials.Username, credentials.Password)
|
||||
});
|
||||
|
||||
var file = await client.GetRawFile(new Uri(baseAddress + url));
|
||||
if (!file.IsSuccessful) {
|
||||
Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
return new FileStreamResult(Stream.Null, "");
|
||||
}
|
||||
|
||||
var split = url.Split("/");
|
||||
return new FileStreamResult(file.Stream, "application/octet-stream") {
|
||||
FileDownloadName = split[^1]
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost("delete")]
|
||||
public async Task<IActionResult> DeleteElement([FromBody] Credentials credentials, [FromQuery] string url) {
|
||||
var baseAddress = new Uri($"https://webdav.{credentials.Domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(credentials.Username, credentials.Password)
|
||||
});
|
||||
|
||||
var result = await client.Delete(new Uri(baseAddress + url));
|
||||
if (result.IsSuccessful) return Ok();
|
||||
return BadRequest(result.Description);
|
||||
}
|
||||
|
||||
[HttpPost("upload")]
|
||||
public async Task<IActionResult> UploadFile([FromQuery] string url, [FromForm] string domain, [FromForm] string username, [FromForm] string password) {
|
||||
var baseAddress = new Uri($"https://webdav.{domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(username, password)
|
||||
});
|
||||
|
||||
var result = await client.PutFile(new Uri(baseAddress + url), Request.Form.Files[0].OpenReadStream());
|
||||
if (result.IsSuccessful) return Ok();
|
||||
return BadRequest(result.Description);
|
||||
}
|
||||
|
||||
[HttpPost("create")]
|
||||
public async Task<IActionResult> CreateFolder([FromBody] Credentials credentials, [FromQuery] string url) {
|
||||
var baseAddress = new Uri($"https://webdav.{credentials.Domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(credentials.Username, credentials.Password)
|
||||
});
|
||||
|
||||
var result = await client.Mkcol(new Uri(baseAddress + url));
|
||||
if (result.IsSuccessful) return Ok();
|
||||
return BadRequest(result.Description);
|
||||
}
|
||||
|
||||
[HttpPost("move")]
|
||||
public async Task<IActionResult> MoveElement([FromBody] Credentials credentials, [FromQuery] string url, [FromQuery] string newUrl) {
|
||||
var baseAddress = new Uri($"https://webdav.{credentials.Domain}");
|
||||
using var client = new WebDavClient(new WebDavClientParams {
|
||||
BaseAddress = baseAddress,
|
||||
Credentials = new NetworkCredential(credentials.Username, credentials.Password)
|
||||
});
|
||||
|
||||
var result = await client.Move(new Uri(baseAddress + url), new Uri(baseAddress + newUrl));
|
||||
if (result.IsSuccessful) return Ok();
|
||||
return BadRequest(result.Description);
|
||||
}
|
||||
|
||||
}
|
||||
7
BetterIServ.Backend/Entities/Credentials.cs
Normal file
7
BetterIServ.Backend/Entities/Credentials.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace BetterIServ.Backend.Entities;
|
||||
|
||||
public struct Credentials {
|
||||
public string Domain { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
9
BetterIServ.Backend/Entities/DirectoryContent.cs
Normal file
9
BetterIServ.Backend/Entities/DirectoryContent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace BetterIServ.Backend.Entities;
|
||||
|
||||
public struct DirectoryContent {
|
||||
public string Url { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime LastModified { get; set; }
|
||||
public string Type { get; set; }
|
||||
public long Size { get; set; }
|
||||
}
|
||||
@@ -20,6 +20,8 @@ if (app.Environment.IsDevelopment()) {
|
||||
app.UseCors(options => {
|
||||
options.WithOrigins("http://localhost:8100");
|
||||
options.AllowCredentials();
|
||||
options.AllowAnyHeader();
|
||||
options.AllowAnyMethod();
|
||||
});
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
Reference in New Issue
Block a user