123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using FileStorage.Repository.SqlSugar;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.StaticFiles;
- using Microsoft.Extensions.Options;
- using SqlSugar;
- using XF.Domain.Authentications;
- namespace FileStorage.Host.Controllers
- {
- [ApiController]
- [Route("file")]
- public class FileController : ControllerBase
- {
- private readonly ILogger<FileController> _logger;
- private readonly IFileStorage _fileStorage;
- private readonly ISugarUnitOfWork<FileStorageDbContext> _uow;
- public FileController(
- ILogger<FileController> logger,
- IFileStorage fileStorage,
- ISugarUnitOfWork<FileStorageDbContext> uow
- )
- {
- _logger = logger;
- _fileStorage = fileStorage;
- _uow = uow;
- }
- /// <summary>
- /// 内部调用,不限制文件格式
- /// </summary>
- /// <param name="fileData"></param>
- /// <param name="source"></param>
- /// <returns></returns>
- [HttpPost("uploadinside")]
- public async Task<IActionResult> UploadInside(IFormFile fileData, string source)
- {
- var file = await _fileStorage.UploadAsync(fileData.FileName, fileData.Length, "", fileData.OpenReadStream(), source ?? string.Empty, false);
- var Path = await _fileStorage.GetFileUrlIndefinitelyAsync(file.Id, source ?? string.Empty);
- return Ok(new { Id = file.Id, fileName = file.FileName, Path = Path });
- }
- /// <summary>
- /// 限制文件格式
- /// </summary>
- /// <param name="fileData"></param>
- /// <param name="source"></param>
- /// <returns></returns>
- [HttpPost("upload")]
- public async Task<IActionResult> Upload(IFormFile fileData, string source)
- {
- var file = await _fileStorage.UploadAsync(fileData.FileName, fileData.Length, "", fileData.OpenReadStream(), source ?? string.Empty, true);
- var Path = await _fileStorage.GetFileUrlIndefinitelyAsync(file.Id, source ?? string.Empty);
- return Ok(new { Id = file.Id, fileName = file.FileName, Path = Path });
- }
- [HttpGet("getfileurl")]
- public async Task<IActionResult> GetFileUrl([FromQuery] UploadGetDto dto)
- {
- var uri = await _fileStorage.GetFileUrlAsync(dto.Id, dto.Source ?? string.Empty);
- return Ok(new { Uri = uri });
- }
- [HttpGet("downloadfile")]
- public async Task<IActionResult> DownLoadFile([FromQuery] UploadGetDto dto)
- {
- var (stream, content, fileName) = _fileStorage.DownLoadFile(dto.Id);
- if (stream != null)
- {
- return File(stream, content, fileName);
- }
- return null;
- }
- [HttpGet("delfile")]
- public async Task<bool> DelFile([FromQuery] UploadGetDto dto)
- {
- return await _fileStorage.DelFileAsync(dto.Id, dto.Source ?? string.Empty);
- }
- [HttpGet("file_path")]
- public async Task<IActionResult> GetFilePath([FromQuery] UploadGetDto dto)
- {
- var uri = await _fileStorage.GetFilePathAsync(dto.Id, dto.Source ?? string.Empty);
- return Ok(new { Path = uri });
- }
- [HttpGet("createdb")]
- public Task CreateDb()
- {
- var db = _uow.Db;
- db.DbMaintenance.CreateDatabase();
- db.CodeFirst.InitTables<FileMetadata>();
- return Task.CompletedTask;
- }
- /// <summary>
- /// 访问文件
- /// </summary>
- /// <param name="id"></param>
- /// <param name="expires"></param>
- /// <param name="clientid"></param>
- /// <param name="signature"></param>
- /// <returns></returns>
- [AllowAnonymous]
- [HttpGet("files")]
- public async Task Files(string id, string expires, string clientid, string signature)
- {
- var fullPath = await _fileStorage.GetFilePath(id, expires, clientid, signature);
- HttpContext.Response.ContentType = "application/octet-stream";
- await HttpContext.Response.SendFileAsync(fullPath);
- }
- /// <summary>
- /// 访问文件无限期
- /// </summary>
- /// <param name="id"></param>
- /// <param name="expires"></param>
- /// <param name="clientid"></param>
- /// <param name="signature"></param>
- /// <returns></returns>
- [AllowAnonymous]
- [HttpGet("files_indefinitely")]
- public async Task FilesIndefinitely(string id, string expires, string clientid, string signature)
- {
- var fullPath = await _fileStorage.GetFilePathIndefinitely(id, expires, clientid, signature);
- HttpContext.Response.ContentType = "application/octet-stream";
- await HttpContext.Response.SendFileAsync(fullPath);
- }
- [HttpGet("test")]
- [AllowAnonymous]
- public string Test()
- {
- return DateTime.Now.ToString();
- }
- }
- }
|