namespace Tiobon.Core.Services;
///
/// 附件 (服务)
///
public class Ghre_AttachmentServices : BaseServices, IGhre_AttachmentServices
{
private readonly IBaseRepository _dal;
public Ghre_AttachmentServices(IBaseRepository dal)
{
this._dal = dal;
base.BaseDal = dal;
}
public async Task> UploadVideoAsync(ChunkUpload upload)
{
var file = upload.file;
var path = $"{$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}upload/{upload.masterId}/{Path.DirectorySeparatorChar}{upload.id}{Path.DirectorySeparatorChar}"}";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var stream = File.Create(path + $"{upload.chunkIndex}"))
{
await file.CopyToAsync(stream);
}
if (upload.chunkIndex == upload.totalChunks - 1)
{
var ext = string.Empty;
if (string.IsNullOrEmpty(file.FileName) == false)
{
var dotPos = upload.fileName.LastIndexOf('.');
ext = upload.fileName.Substring(dotPos + 1);
}
var id = SnowFlakeSingle.Instance.NextId();
await FileMerge(upload.id, "." + ext, id, upload.masterId);
using var _context = ContextFactory.CreateContext();
var filePath = $"/files/upload/{upload.masterId}/{id}.{ext}";
var fileAttachment = new Ghre_Attachment();
fileAttachment.Id = SnowFlakeSingle.Instance.NextId();
fileAttachment.AttachmentNo = upload.file.FileName;
fileAttachment.AttachFileName = upload.file.FileName;
fileAttachment.CreateBy = App.User.ID;
fileAttachment.CreateTime = DateTime.Now;
fileAttachment.AttachmentName = upload.fileName;
fileAttachment.AttachFileExtension = ext;
fileAttachment.AttachFileSize = upload.file.Length;
fileAttachment.PhysicsPath = filePath;
fileAttachment.RelativePath = filePath;
fileAttachment.FileURL = filePath;
fileAttachment.ThumbnailPath = filePath;
fileAttachment.AttachmentType = upload.file.ContentType;
//url = fileName + "." + ext;
_context.Add(fileAttachment);
_context.SaveChanges();
return ServiceResult.OprateSuccess("", new FileUploadResult()
{
Id = fileAttachment.Id,
AttachFileExtension = fileAttachment.AttachFileExtension,
AttachFileName = fileAttachment.AttachFileName,
AttachmentName = fileAttachment.AttachmentName,
AttachmentNo = fileAttachment.AttachmentNo,
FileURL = "/Advanced" + fileAttachment.FileURL,
PhysicsPath = "/Advanced" + fileAttachment.PhysicsPath,
RelativePath = "/Advanced" + fileAttachment.RelativePath,
ThumbnailPath = "/Advanced" + fileAttachment.ThumbnailPath,
AttachFileSize = fileAttachment.AttachFileSize,
});
}
return ServiceResult.OprateSuccess("", null);
}
public static async Task