public async Task <ActionResult> EditFile(FileEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.ToDictionary()));
            }

            var editedFile = await _filesManager.Edit(model, User.Identity.GetUserId());

            return(Json(editedFile));
        }
Beispiel #2
0
 public IActionResult Put([FromForm] FileEditViewModel model)
 {
     if (ModelState.IsValid && model.File != null)
     {
         if (_img.Edit(model.File, model.OldImage, out string path))
         {
             return(Ok(new { Name = path }));
         }
         return(BadRequest(new { Message = "We could not add this resource. Please try again" }));
     }
     return(BadRequest(new { Message = "Your data is bad" }));
 }
 public IActionResult EditFile(FileEditViewModel model)
 {
     if (ModelState.IsValid)
     {
         FileDTO file = new FileDTO {
             Id       = model.Id,
             Name     = model.Name,
             IsPublic = model.IsPublic,
             FolderId = model.FolderId
         };
         try {
             fileService.EditFile(file);
         } catch (UnauthorizedAccessException) {
             return(StatusCode(StatusCodes.Status403Forbidden));
         }
         return(RedirectToAction(nameof(Drive), new { id = file.FolderId }));
     }
     return(View(model));
 }
        public IActionResult EditFile(int id)
        {
            FileDTO file;

            try {
                file = fileService.GetFile(id);
            } catch (UnauthorizedAccessException) {
                return(StatusCode(StatusCodes.Status403Forbidden));
            }

            if (file != null)
            {
                FileEditViewModel model = new FileEditViewModel {
                    Id       = file.Id,
                    Name     = file.Name,
                    IsPublic = file.IsPublic,
                    Size     = file.Data.Length,
                    FolderId = file.FolderId ?? 0
                };
                return(View(model));
            }
            return(NotFound($"File with id {id} doesn't exists"));
        }
Beispiel #5
0
        public async Task <FileListViewModel> Edit(FileEditViewModel model, string agentId)
        {
            if (string.IsNullOrEmpty(agentId))
            {
                throw new NotAuthorizedUserException("Само потребители могат да редактират файлове!");
            }

            if (!await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Administrator)) &&
                !await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Agent)) &&
                !await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Maintenance)))
            {
                throw new NotAuthorizedUserException("Нямате право да извършвате това действие!");
            }

            if (!await Exist(fileId: model.FileId))
            {
                throw new ArgumentException("Файлът, който искате да редактирате не съществува в системата!");
            }

            var fileToEdit = await _dbContext.Files.FirstOrDefaultAsync(f => f.Id == model.FileId)
                             ?? throw new ContentNotFoundException("Не е намерен файлът, който искате да редактирате!");

            if (fileToEdit.AgentId != agentId && !await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Administrator)))
            {
                throw new NotAuthorizedUserException("Трябва да сте собственик на файла или админ за да го редактирате!");
            }

            if (await Exist(fileName: model.FileName, folderId: fileToEdit.FolderId))
            {
                throw new ArgumentException("Съществува файл с това име в папката!");
            }

            //Extentions are not editable
            //Only the name without the extention
            var newFileName = Path.GetFileNameWithoutExtension(model.FileName) + Path.GetExtension(fileToEdit.Name);
            var currentDir  = Path.GetDirectoryName(fileToEdit.RelativePath);

            var newRelativePath = Path.Combine(currentDir ?? _baseMaterialsPath, newFileName);
            var oldRelativePath = fileToEdit.RelativePath;
            var systemRoot      = HttpRuntime.AppDomainAppPath;
            var newPhysicalPath = Path.Combine(systemRoot.TrimEnd('\\', '/'), newRelativePath.TrimStart('\\', '/'));
            var oldPhysicalPath = Path.Combine(systemRoot.TrimEnd('\\', '/'), oldRelativePath.TrimStart('\\', '/'));

            try
            {
                //Change the name in the file system
                File.Move(oldPhysicalPath, newPhysicalPath);
            }
            catch (Exception)
            {
                throw new Exception("Проблем в системата при преименуване на файл!");
            }

            try
            {
                // change name in DB
                _dbContext.Files.Attach(fileToEdit);
                fileToEdit.Name         = newFileName;
                fileToEdit.RelativePath = newRelativePath;
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception)
            {
                //Change the name in the file system
                File.Move(newPhysicalPath, oldPhysicalPath);

                throw new Exception("Проблем при в системата, свържете се с Администратор!");
            }


            return(await Get(model.FileId, agentId));
        }