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(); private ICustomerRepository CustomerRepository => LazyServiceProvider.GetRequiredService(); private IObjectStorage FileStorage => LazyServiceProvider.GetRequiredService(); /// /// 上传媒体文件 /// [HttpPost("/xhs/media/upload")] public async Task 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 }; } }