public void RemoveWu10FromFileName(string serviceName) { var dllPath = GetServiceDllPath(serviceName); if (string.IsNullOrEmpty(dllPath)) { return; } var wu10Path = GetPathWithWu10Prefix(dllPath); // If the file has returned, then we need to assume that some other process has recreated // it. It's safer to assume the new file is correct and delete the "old" file. if (_filesHelper.Exists(dllPath)) { _filesHelper.Delete(wu10Path); } else { _filesHelper.GiveOwnershipToAdministrators(wu10Path); _filesHelper.RenameFile(wu10Path, dllPath); _filesHelper.GiveOwnershipToTrustedInstaller(dllPath); } }
public IActionResult Edit(ClientEditViewModel model) { if (ModelState.IsValid) { string[] validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; // Find existed Client by Id Client client = _context.Set <Client>() .FindBy(c => c.ClientId == new Guid(model.ClientId)) .Include(cl => cl.ClientImages) .FirstOrDefault(); if (client != null) { // Update Client's Properties client.Name = model.Name; client.ShortDescription = model.ShortDescription; client.Description = model.Description; client.HttpUrl = model.HttpUrl; } else { return(NotFound($"Клиент с id: {model.ClientId} не найден")); } //// Check on null or zero length file if (model.ImageUpload != null && model.ImageUpload.Length > 0) { if (validImageTypes.Contains(model.ImageUpload.ContentType)) { // Remove existed File from Server string pathDir = _optionsAccessor.Value.ClientImages; string pathExistedFile = client.ClientImages.FirstOrDefault()?.Path.Substring(1); if (!string.IsNullOrEmpty(pathExistedFile)) { _filesHelper.Delete(pathExistedFile); } // Copy New File to Server string pathNewFileName = Guid.NewGuid().ToString(); string pathNewFileExt = Path.GetExtension(model.ImageUpload.FileName); string pathNewFile = pathDir + pathNewFileName + pathNewFileExt; _filesHelper.Copy(pathDir, pathNewFileName + pathNewFileExt, model.ImageUpload); // SYNC changes to Database ClientImage clientNewImage = new ClientImage { IsPoster = true, Path = @"\" + pathDir + pathNewFileName + pathNewFileExt }; // Find existed image ClientImage clientExistedImage = client.ClientImages.FirstOrDefault(); // Remove previous image if (clientExistedImage != null) { client.ClientImages.Remove(clientExistedImage); } client.ClientImages.Add(clientNewImage); } //// Update DB _context.Set <Client>().Update(client, new Guid(model.ClientId)); return(RedirectToAction("Index")); } else { ModelState.AddModelError("ImageUpload", "Файл пустой или не в формате JPG, GIF или PNG"); } } // If Model has errors return(View(model)); }