136 lines
4.5 KiB
C#
136 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using AccountManagement.Application.Contracts.Media;
|
|
using AccountManagement.Domain.AdminResponseMediaAgg;
|
|
using AccountManagement.Domain.ClientResponseMediaAgg;
|
|
using AccountManagement.Domain.MediaAgg;
|
|
using AccountManagement.Domain.TaskMediaAgg;
|
|
using AccountManagement.Domain.TicketMediasAgg;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AccountMangement.Infrastructure.EFCore.Repository;
|
|
|
|
public class MediaRepository : RepositoryBase<long, Media>, IMediaRepository
|
|
{
|
|
private const string _basePath = "Storage/Medias";
|
|
public string BasePath { get; set; }
|
|
|
|
|
|
private readonly AccountContext _accountContext;
|
|
public MediaRepository(AccountContext taskManagerContext, IWebHostEnvironment webHostEnvironment) : base(taskManagerContext)
|
|
{
|
|
_accountContext = taskManagerContext;
|
|
BasePath = webHostEnvironment.ContentRootPath + "/Storage";
|
|
}
|
|
//ساخت جدول واسط بین مدیا و نسک
|
|
//نکته: این متد ذخیره انجام نمیدهد
|
|
|
|
public void CreateMediaWithTaskMedia(long taskId, long mediaId)
|
|
{
|
|
var Taskmedias = new TaskMedia(taskId, mediaId);
|
|
_accountContext.Add(Taskmedias);
|
|
}
|
|
public void Remove(long id)
|
|
{
|
|
var media = Get(id);
|
|
Remove(media);
|
|
}
|
|
|
|
public void CreateTicketMedia(long ticketId, long mediaId)
|
|
{
|
|
var ticketMedias = new TicketMedia(ticketId, mediaId);
|
|
_accountContext.Add(ticketMedias);
|
|
}
|
|
|
|
public void CreateAdminResponseMedia(long adminResponseId, long mediaId)
|
|
{
|
|
var adminResMedia = new AdminResponseMedia(adminResponseId, mediaId);
|
|
_accountContext.Add(adminResMedia);
|
|
}
|
|
|
|
public void CreateClientResponseMedia(long clientResponseId, long mediaId)
|
|
{
|
|
var clientResMedia = new ClientResponseMedia(clientResponseId, mediaId);
|
|
_accountContext.Add(clientResMedia);
|
|
}
|
|
|
|
public List<MediaViewModel> GetMediaByTaskId(long taskId)
|
|
{
|
|
return _accountContext.TaskMedias.Include(x => x.Media).Where(x => x.TaskId == taskId).Select(x =>
|
|
new MediaViewModel()
|
|
{
|
|
Id = x.Media.id,
|
|
Path = x.Media.Path,
|
|
Type = x.Media.Type,
|
|
}).ToList();
|
|
}
|
|
|
|
#region Pooya
|
|
|
|
public List<MediaViewModel> GetMedias(List<long> ids)
|
|
{
|
|
return _accountContext.Medias.Where(x => ids.Contains(x.id)).Select(x => new MediaViewModel()
|
|
{
|
|
Id = x.id,
|
|
Path = x.Path,
|
|
Category = x.Category,
|
|
Type = x.Type
|
|
}).ToList();
|
|
}
|
|
|
|
public List<Media> GetRange(IEnumerable<long> mediaIds)
|
|
{
|
|
return _accountContext.Medias.Where(x => mediaIds.Contains(x.id)).ToList();
|
|
}
|
|
|
|
public OperationResult UploadFile(IFormFile file, string fileLabel, string relativePath, int maximumFileLength,
|
|
List<string> allowedExtensions, string category)
|
|
{
|
|
OperationResult op = new();
|
|
var path = Path.Combine(_basePath, relativePath);
|
|
var fileExtension = Path.GetExtension(file.FileName);
|
|
|
|
if (file == null || file.Length == 0)
|
|
return op.Failed("خطای سیستمی");
|
|
|
|
if (file.Length > (maximumFileLength * 1024 * 1024))
|
|
return op.Failed($"حجم فایل نمی تواند بیشتر از " +
|
|
$"{maximumFileLength}" +
|
|
$"مگابایت باشد");
|
|
|
|
if (!allowedExtensions.Contains(fileExtension.ToLower()))
|
|
{
|
|
var operationMessage = ":فرمت فایل باید یکی از موارد زیر باشد";
|
|
operationMessage += "\n";
|
|
operationMessage += string.Join(" ", allowedExtensions);
|
|
return op.Failed(operationMessage);
|
|
}
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
var extension = Path.GetExtension(file.FileName);
|
|
|
|
var uniqueFileName = $"{fileLabel}-{DateTime.Now.Ticks}{extension}";
|
|
var filePath = Path.Combine(path, uniqueFileName);
|
|
using (var fileStream = new FileStream(filePath, FileMode.CreateNew))
|
|
{
|
|
file.CopyTo(fileStream);
|
|
}
|
|
var mediaEntity = new Media(filePath, extension, "فایل", category);
|
|
Create(mediaEntity);
|
|
SaveChanges();
|
|
|
|
return op.Succcedded(mediaEntity.id);
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|