public async Task <OperationResult> AddNewImage([FromForm] int fieldId, [FromForm] int caseId) { try { var newFile = HttpContext.Request.Form.Files.Last(); var core = await _coreHelper.GetCore(); var sdkDbContext = core.DbContextHelper.GetDbContext(); var caseDb = await sdkDbContext.Cases .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.Id == caseId) .FirstOrDefaultAsync(); var field = await sdkDbContext.Fields .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.Id == fieldId) .FirstOrDefaultAsync(); if (caseDb == null || field == null) { return(new OperationResult(false, _localizationService.GetString("CaseNotFound"))); } var folder = Path.Combine(Path.GetTempPath(), "cases-temp-files"); Directory.CreateDirectory(folder); var filePath = Path.Combine(folder, $"{DateTime.Now.Ticks}.{newFile.FileName.Split(".").Last()}"); string hash; using (var md5 = MD5.Create()) { await using (var stream = new FileStream(filePath, FileMode.Create)) { await newFile.CopyToAsync(stream); var grr = await md5.ComputeHashAsync(stream); hash = BitConverter.ToString(grr).Replace("-", "").ToLower(); } } var newUploadData = new Microting.eForm.Infrastructure.Data.Entities.UploadedData { Checksum = hash, FileName = $"{hash}.{newFile.FileName.Split(".").Last()}", FileLocation = filePath, Extension = newFile.FileName.Split(".").Last() }; await newUploadData.Create(sdkDbContext); newUploadData.FileName = $"{newUploadData.Id}_{newUploadData.FileName}"; await newUploadData.Update(sdkDbContext); await core.PutFileToStorageSystem(filePath, newUploadData.FileName); var fieldValue = new Microting.eForm.Infrastructure.Data.Entities.FieldValue { FieldId = field.Id, CaseId = caseDb.Id, CheckListId = field.CheckListId, WorkerId = caseDb.WorkerId, DoneAt = DateTime.UtcNow, UploadedDataId = newUploadData.Id }; await fieldValue.Create(sdkDbContext); string smallFilename = $"{newUploadData.Id}_300_{newUploadData.Checksum}{newUploadData.Extension}"; //uploadedDataObj.Id + "_300_" + uploadedDataObj.Checksum; string bigFilename = $"{newUploadData.Id}_700_{newUploadData.Checksum}{newUploadData.Extension}"; //uploadedDataObj.Id + "_700_" + urlStr.Remove(0, index); System.IO.File.Copy(filePath, Path.Combine(Path.GetTempPath(), smallFilename)); System.IO.File.Copy(filePath, Path.Combine(Path.GetTempPath(), bigFilename)); string filePathResized = Path.Combine(Path.GetTempPath(), smallFilename); using (var image = new MagickImage(filePathResized)) { decimal currentRation = image.Height / (decimal)image.Width; int newWidth = 300; int newHeight = (int)Math.Round((currentRation * newWidth)); image.Resize(newWidth, newHeight); image.Crop(newWidth, newHeight); await image.WriteAsync(filePathResized); image.Dispose(); await core.PutFileToStorageSystem(Path.Combine(Path.GetTempPath(), filePathResized), smallFilename); } System.IO.File.Delete(filePathResized); filePathResized = Path.Combine(Path.GetTempPath(), bigFilename); using (var image = new MagickImage(filePathResized)) { decimal currentRation = image.Height / (decimal)image.Width; int newWidth = 700; int newHeight = (int)Math.Round((currentRation * newWidth)); image.Resize(newWidth, newHeight); image.Crop(newWidth, newHeight); await image.WriteAsync(filePathResized); image.Dispose(); await core.PutFileToStorageSystem(Path.Combine(Path.GetTempPath(), filePathResized), bigFilename); } System.IO.File.Delete(filePathResized); return(new OperationResult(true, _localizationService.GetString("ImageUpdatedSuccessfully"))); } catch (Exception e) { Console.WriteLine(e); return(new OperationResult(false, _localizationService.GetString("ErrorWhileUpdateImage"))); } }
public async Task <OperationResult> UpdateImage([FromForm] int uploadedObjId) { try { var newFile = HttpContext.Request.Form.Files.Last(); var core = await _coreHelper.GetCore(); var sdkDbContext = core.DbContextHelper.GetDbContext(); var uploadData = await sdkDbContext.UploadedDatas .Where(x => x.Id == uploadedObjId) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .FirstOrDefaultAsync(); var fieldValue = await sdkDbContext.FieldValues .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.UploadedDataId == uploadedObjId) .FirstOrDefaultAsync(); if (uploadData == null || fieldValue == null) { return(new OperationResult(false, _localizationService.GetString("ImageNotFound"))); } // delete old image await core.Advanced_DeleteUploadedData((int)fieldValue.FieldId, uploadedObjId); var folder = Path.Combine(Path.GetTempPath(), "cases-temp-files"); Directory.CreateDirectory(folder); var filePath = Path.Combine(folder, $"{DateTime.Now.Ticks}.{newFile.FileName.Split(".").Last()}"); var hash = ""; using (var md5 = MD5.Create()) { await using (var stream = new FileStream(filePath, FileMode.Create)) { await newFile.CopyToAsync(stream); var grr = await md5.ComputeHashAsync(stream); hash = BitConverter.ToString(grr).Replace("-", "").ToLower(); } } await core.PutFileToStorageSystem(filePath, newFile.FileName); var newUploadData = new Microting.eForm.Infrastructure.Data.Entities.UploadedData { Checksum = hash, FileName = newFile.FileName, FileLocation = filePath, }; await newUploadData.Create(sdkDbContext); fieldValue.UploadedDataId = newUploadData.Id; await fieldValue.Update(sdkDbContext); return(new OperationResult(true, _localizationService.GetString("ImageUpdatedSuccessfully"))); } catch (Exception e) { Console.WriteLine(e); return(new OperationResult(false, _localizationService.GetString("ErrorWhileUpdateImage"))); } }