Ejemplo n.º 1
0
        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")));
            }
        }
Ejemplo n.º 2
0
        private async Task <OperationResult> RotateImageS3(string fileName)
        {
            Console.WriteLine(@"Memory used before collection:       {0:N0}",
                              GC.GetTotalMemory(false));
            Console.WriteLine(@"Environment.ProcessorCount:          {0}",
                              Environment.ProcessorCount);
            var fileId = int.Parse(fileName.Split('_').First());
            var core   = await _coreHelper.GetCore();

            var dbContext = core.DbContextHelper.GetDbContext();

            Microting.eForm.Infrastructure.Data.Entities.UploadedData uploadedDataObj = await dbContext.UploadedDatas.SingleAsync(x => x.Id == fileId);

            var result = await core.GetFileFromS3Storage(fileName);

            var filePath   = Path.Combine(Path.GetTempPath(), fileName);
            var fileStream = System.IO.File.Create(filePath);
            await result.ResponseStream.CopyToAsync(fileStream);

            fileStream.Close();
            await fileStream.DisposeAsync();

            result.ResponseStream.Close();
            await result.ResponseStream.DisposeAsync();

            string smallFilename = $"{uploadedDataObj.Id}_300_{uploadedDataObj.Checksum}{uploadedDataObj.Extension}"; //uploadedDataObj.Id + "_300_" + uploadedDataObj.Checksum;
            string bigFilename   = $"{uploadedDataObj.Id}_700_{uploadedDataObj.Checksum}{uploadedDataObj.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 RotateFileAndPutToStorage(Path.Combine(Path.GetTempPath(), smallFilename), core, 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 RotateFileAndPutToStorage(Path.Combine(Path.GetTempPath(), bigFilename), core, bigFilename);
            }
            System.IO.File.Delete(filePathResized);
            // await RotateFileAndPutToStorage(Path.Combine(Path.GetTempPath(), smallFilename), core, smallFilename);
            // await RotateFileAndPutToStorage(Path.Combine(Path.GetTempPath(), bigFilename), core, bigFilename);
            Console.WriteLine(@"Memory used before collection:       {0:N0}",
                              GC.GetTotalMemory(false));
            GC.Collect();
            Console.WriteLine(@"Memory used after full collection:   {0:N0}",
                              GC.GetTotalMemory(true));
            return(await RotateFileAndPutToStorage(filePath, core, fileName));
        }
Ejemplo n.º 3
0
        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")));
            }
        }