public IActionResult RemoveFiles([FromForm] SelectFilesModel model) { try { string rootPath = Path.Combine(_environment.ContentRootPath, "Uploads"); foreach (Guid fileId in model.FileGuids) { var file = _db.CajFile.FirstOrDefault(x => x.Id == fileId); if (file != null) { string filePath = Path.Combine(rootPath, file.Id.ToString()); if (IOManager.RemoveFile(filePath)) { _db.CajFileInFolder.RemoveRange(_db.CajFileInFolder.Where(x => x.FkFile == file.Id)); _db.CajFile.Remove(file); } } } _db.SaveChanges(); return(StatusCode(204)); } catch (Exception ex) { _logger.LogInformation(ex.ToString()); return(StatusCode(404)); } }
public string PrepareFilesDownload([FromForm] SelectFilesModel model) { string rootPath = Path.Combine(_environment.ContentRootPath, "Uploads"); var decompressedFiles = new List <string>(); //1. Create new folder string folderName = Guid.NewGuid().ToString(); string destinationFolder = Path.Combine(rootPath, folderName); Directory.CreateDirectory(destinationFolder); //2. Decompress selected files into the new folder foreach (var fileGuid in model.FileGuids) { var file = _db.CajFile.FirstOrDefault(x => x.Id == fileGuid); string serverSourceFilePath = Path.Combine(rootPath, file.Id.ToString()); string serverDestinationFilePath = Path.Combine(destinationFolder, file.Name); //3. Rename them to the original name if (IOManager.Decompress(serverSourceFilePath, serverDestinationFilePath)) { decompressedFiles.Add(serverDestinationFilePath); } } //4. Compress them string serverCompressedFilePath = Path.Combine(destinationFolder, $"{folderName}.zip"); if (IOManager.CompressFiles(decompressedFiles.ToArray(), serverCompressedFilePath)) { foreach (var decompressedFile in decompressedFiles) { IOManager.RemoveFile(decompressedFile); } //5. Return destination folder name return(folderName); } return(null); }