59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using Fake.Application;
|
|
using Fake.ObjectStorage;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Oms.Application.Dtos.Xhs;
|
|
using Oms.Application.Shared;
|
|
using Oms.Application.Storage;
|
|
using Oms.Domain.CustomersAggregate.Repositories;
|
|
using Oms.Domain.XhsAggregate.Entities;
|
|
using Oms.Domain.XhsAggregate.Repositories;
|
|
|
|
namespace Oms.Application.Services.Xhs;
|
|
|
|
[ApiExplorerSettings(GroupName = "OMS-Xhs")]
|
|
public class XhsMediaService : ApplicationService
|
|
{
|
|
private IXhsMediaRepository MediaRepository => LazyServiceProvider.GetRequiredService<IXhsMediaRepository>();
|
|
private ICustomerRepository CustomerRepository => LazyServiceProvider.GetRequiredService<ICustomerRepository>();
|
|
private IObjectStorage FileStorage => LazyServiceProvider.GetRequiredService<IObjectStorage>();
|
|
|
|
/// <summary>
|
|
/// 上传媒体文件
|
|
/// </summary>
|
|
[HttpPost("/xhs/media/upload")]
|
|
public async Task<XhsMediaUploadOutput> UploadAsync(IFormFile file)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
throw new BusinessException("请选择文件");
|
|
}
|
|
|
|
var customerId = await OmsSessionHelper.ResolveXhsCustomerIdAsync(CustomerRepository, CurrentUser);
|
|
var ext = Path.GetExtension(file.FileName);
|
|
var storedName = $"{Guid.NewGuid():N}{ext}";
|
|
var relativeKey = $"uploads/xhs/{customerId}/{storedName}";
|
|
|
|
await using var stream = file.OpenReadStream();
|
|
var storageKey = await FileStorage.SaveObjectAsync(stream, relativeKey, file.ContentType);
|
|
var publicUrl = FileStorage.GetPublicUrl(storageKey);
|
|
|
|
var media = new XhsMedia
|
|
{
|
|
CustomerId = customerId,
|
|
FileName = file.FileName,
|
|
StoragePath = storageKey,
|
|
PublicUrl = publicUrl,
|
|
MimeType = file.ContentType,
|
|
FileSizeBytes = file.Length
|
|
};
|
|
await MediaRepository.InsertAsync(media);
|
|
|
|
return new XhsMediaUploadOutput
|
|
{
|
|
MediaId = media.Id,
|
|
PublicUrl = publicUrl
|
|
};
|
|
}
|
|
}
|