| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using Masuit.MyBlogs.Core.Common;
- using Masuit.MyBlogs.Core.Extensions.DriveHelpers;
- using Masuit.MyBlogs.Core.Extensions.Firewall;
- using Masuit.MyBlogs.Core.Infrastructure.Drive;
- using Masuit.MyBlogs.Core.Models.Drive;
- using Masuit.MyBlogs.Core.Models.DTO;
- using Masuit.MyBlogs.Core.Models.ViewModel;
- using Masuit.Tools.Core.Net;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Filters;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- namespace Masuit.MyBlogs.Core.Controllers.Drive
- {
- [ApiController]
- [ServiceFilter(typeof(FirewallAttribute))]
- [Route("api/")]
- public class SitesController : Controller
- {
- private readonly IDriveAccountService _siteService;
- private readonly IDriveService _driveService;
- private readonly SettingService _setting;
- public UserInfoDto CurrentUser => HttpContext.Session.Get<UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto();
- public SitesController(IDriveAccountService siteService, IDriveService driveService, SettingService setting)
- {
- this._siteService = siteService;
- this._driveService = driveService;
- this._setting = setting;
- }
- /// <summary>
- /// 返回所有sites
- /// </summary>
- /// <returns></returns>
- [HttpGet("sites"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public IActionResult GetSites()
- {
- return Json(_siteService.GetSites(), new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- /// <summary>
- /// 根据路径获取文件夹内容
- /// </summary>
- /// <returns></returns>
- [HttpGet("sites/{siteName}/{**path}"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public async Task<IActionResult> GetDirectory(string siteName, string path)
- {
- if (string.IsNullOrEmpty(siteName))
- {
- return NotFound(new ErrorResponse()
- {
- message = "找不到请求的 Site Name"
- });
- }
- if (string.IsNullOrEmpty(path))
- {
- try
- {
- var result = await _driveService.GetRootItems(siteName, CurrentUser.IsAdmin);
- return Json(result, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- catch (Exception e)
- {
- return StatusCode(500, e.Message);
- }
- }
- else
- {
- try
- {
- var result = await _driveService.GetDriveItemsByPath(path, siteName, CurrentUser.IsAdmin);
- if (result == null)
- {
- return NotFound(new ErrorResponse()
- {
- message = $"路径{path}不存在"
- });
- }
- return Json(result, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- catch
- {
- return NotFound(new ErrorResponse()
- {
- message = $"路径{path}不存在"
- });
- }
- }
- }
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- [HttpGet("files/{siteName}/{**path}"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public async Task<IActionResult> Download(string siteName, string path)
- {
- try
- {
- var result = await _driveService.GetDriveItemByPath(path, siteName);
- if (result != null)
- {
- return Redirect(result.DownloadUrl);
- }
- return NotFound(new ErrorResponse()
- {
- message = $"所求的{path}不存在"
- });
- }
- catch (Exception e)
- {
- return StatusCode(500, e.Message);
- }
- }
- /// <summary>
- /// 获取基本信息
- /// </summary>
- /// <returns></returns>
- [HttpGet("info"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public IActionResult GetInfo()
- {
- bool isAollowAnonymous = !string.IsNullOrEmpty(_setting.Get("AllowAnonymouslyUpload")) && Convert.ToBoolean(_setting.Get("AllowAnonymouslyUpload"));
- return Json(new
- {
- appName = _setting.Get("AppName"),
- webName = _setting.Get("WebName"),
- defaultDrive = _setting.Get("DefaultDrive"),
- readme = _setting.Get("Readme"),
- footer = _setting.Get("Footer"),
- allowUpload = isAollowAnonymous
- }, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- /// <summary>
- /// 获得readme
- /// </summary>
- /// <returns></returns>
- [HttpGet("readme"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public IActionResult GetReadme()
- {
- return Json(new
- {
- readme = _setting.Get("Readme")
- }, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- /// <summary>
- /// 获取文件分片上传路径
- /// </summary>
- /// <returns></returns>
- [HttpGet("upload/{siteName}/{**fileName}"), ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)]
- public async Task<IActionResult> GetUploadUrl(string siteName, string fileName)
- {
- bool isAollowAnonymous = !string.IsNullOrEmpty(_setting.Get("AllowAnonymouslyUpload")) && Convert.ToBoolean(_setting.Get("AllowAnonymouslyUpload"));
- if (!isAollowAnonymous)
- {
- if (Request.Headers.ContainsKey("Authorization"))
- {
- if (!CurrentUser.IsAdmin)
- {
- return Unauthorized(new ErrorResponse()
- {
- message = "未经授权的访问"
- });
- }
- }
- else
- {
- return Unauthorized(new ErrorResponse()
- {
- message = "未经授权的访问"
- });
- }
- }
- string path = Path.Combine($"upload/{Guid.NewGuid()}", fileName);
- try
- {
- var result = await _driveService.GetUploadUrl(path, siteName);
- return Json(new
- {
- requestUrl = result,
- fileUrl = $"{OneDriveConfiguration.BaseUri}/api/files/{siteName}/{path}"
- }, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- catch (Exception e)
- {
- return StatusCode(500, e.Message);
- }
- }
- /// <summary>
- /// 获取文件分片上传路径
- /// </summary>
- /// <returns></returns>
- [HttpGet("cli/upload/{siteName}/:/{**path}")]
- public async Task<IActionResult> GetUploadUrl(string siteName, string path, string uploadPassword)
- {
- if (uploadPassword != _setting.Get("UploadPassword"))
- {
- return Unauthorized(new ErrorResponse()
- {
- message = "上传密码错误"
- });
- }
- if (string.IsNullOrEmpty(path))
- {
- return BadRequest(new ErrorResponse()
- {
- message = "必须存在上传路径"
- });
- }
- try
- {
- var result = await _driveService.GetUploadUrl(path, siteName);
- return Json(new
- {
- requestUrl = result
- }, new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- catch (Exception e)
- {
- return StatusCode(500, e.Message);
- }
- }
- public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
- {
- if (CommonHelper.SystemSettings.GetOrAdd("CloseSite", "false") == "true")
- {
- context.Result = new BadRequestObjectResult(new { code = 403 });
- return Task.CompletedTask;
- }
- return next();
- }
- }
- }
|