using System; using System.IO; using AccountManagement.Domain.TaskAgg; using ApkReader; using Company.Domain.AndroidApkVersionAgg; using CompanyManagment.App.Contracts.AndroidApkVersion; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using System; using System.IO; using System.Threading.Tasks; using _0_Framework.Application; namespace CompanyManagment.Application; public class AndroidApkVersionApplication : IAndroidApkVersionApplication { private readonly IAndroidApkVersionRepository _androidApkVersionRepository; private readonly ITaskRepository _taskRepository; public AndroidApkVersionApplication(IAndroidApkVersionRepository androidApkVersionRepository, ITaskRepository taskRepository) { _androidApkVersionRepository = androidApkVersionRepository; _taskRepository = taskRepository; } public async Task CreateAndActive(IFormFile file) { OperationResult op = new OperationResult(); #region Validation if (file == null || file.Length == 0) return op.Failed("لطفا فایل خود را وارد کنید"); // Check if the uploaded file is an APK if (Path.GetExtension(file.FileName).ToLower() != ".apk") return op.Failed("لطفا فایلی با پسوند .apk وارد کنید"); #endregion var activeApks = _androidApkVersionRepository.GetActives(); await activeApks.ExecuteUpdateAsync(setter => setter.SetProperty(e => e.IsActive, IsActive.False)); _androidApkVersionRepository.SaveChanges(); var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage", "Apk", "Android", "GozreshgirWebView"); Directory.CreateDirectory(path); //var apk = new ApkReader.ApkReader().Read(file.OpenReadStream()); string uniqueFileName = $"{Path.GetFileNameWithoutExtension(file.FileName)}{Path.GetExtension(file.FileName)}"; string filepath = Path.Combine(path, uniqueFileName); await using (var stream = new FileStream(filepath, FileMode.Create)) { await file.CopyToAsync(stream); } var entity = new AndroidApkVersion("0", "0", IsActive.True, filepath); _androidApkVersionRepository.Create(entity); _androidApkVersionRepository.SaveChanges(); return op.Succcedded(); } public async Task CreateAndDeActive(IFormFile file) { OperationResult op = new OperationResult(); #region Validation if (file == null || file.Length == 0) return op.Failed("لطفا فایل خود را وارد کنید"); // Check if the uploaded file is an APK if (Path.GetExtension(file.FileName).ToLower() != ".apk") return op.Failed("لطفا فایلی با پسوند .apk وارد کنید"); #endregion var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage", "Apk", "Android", "GozreshgirWebView"); Directory.CreateDirectory(path); var apk = new ApkReader.ApkReader().Read(file.OpenReadStream()); string uniqueFileName = $"{Path.GetFileNameWithoutExtension(file.FileName)}.v{apk.VersionName}{Path.GetExtension(file.FileName)}"; string filepath = Path.Combine(path, uniqueFileName); await using (var stream = new FileStream(filepath, FileMode.Create)) { await file.CopyToAsync(stream); } var entity = new AndroidApkVersion(apk.VersionName, apk.VersionCode, IsActive.False, filepath); _androidApkVersionRepository.SaveChanges(); return op.Succcedded(); } public async Task GetLatestActiveVersionPath() { return await _androidApkVersionRepository.GetLatestActiveVersionPath(); } public OperationResult Remove(long id) { OperationResult op = new OperationResult(); var entity = _androidApkVersionRepository.Get(id); if (entity == null) return op.Failed("چنین آیتمی وجود ندارد"); var path = entity.Path; _androidApkVersionRepository.Remove(entity); _androidApkVersionRepository.SaveChanges(); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } return op.Succcedded(); } public bool HasAndroidApkToDownload() { return _androidApkVersionRepository.Exists(x => x.IsActive == IsActive.True); } }