private async Task GetFilesRecursive(string sourseDir, FileStructureDTO fileStructureRoot, int userId, int projectId) { try { foreach (string directory in Directory.GetDirectories(sourseDir)) { var dirName = directory.Substring(directory.LastIndexOf('\\') + 1); var nestedFileStructure = new FileStructureDTO() { Type = TreeNodeType.Folder, Name = dirName, Id = Guid.NewGuid().ToString() }; fileStructureRoot.NestedFiles.Add(nestedFileStructure); await GetFilesRecursive(directory, nestedFileStructure, userId, projectId); } foreach (var file in Directory.GetFiles(sourseDir)) { var fileName = file.Substring(file.LastIndexOf('\\') + 1); var dirName = sourseDir.Substring(sourseDir.LastIndexOf('\\') + 1); Debug.WriteLine(dirName); var fileCreateDto = new FileCreateDTO(); fileCreateDto.Folder = dirName; fileCreateDto.Name = fileName; fileCreateDto.ProjectId = projectId; fileCreateDto.Content = await GetFileContent(file); var fileCreated = await _fileService.CreateAsync(fileCreateDto, userId); var nestedFileStructure = new FileStructureDTO() { Type = TreeNodeType.File, Name = fileName, Id = fileCreated.Id, Size = Encoding.Unicode.GetByteCount(fileCreated.Content) }; fileStructureRoot.NestedFiles.Add(nestedFileStructure); } } catch (Exception e) { if (e is TooHeavyFileException || e is TooManyFilesInProjectException) { throw e; } Debug.WriteLine(e.Message); } }
public async Task <int> GetFileStructureSize(FileStructureDTO projectStructureDTO, string fileStructureId) { foreach (var item in projectStructureDTO.NestedFiles) { if (item.Id == fileStructureId) { return(item.Size); } int size = await GetFileStructureSize(item, fileStructureId); if (size != 0) { return(size); } } return(0); }
private async Task <FileStructureDTO> CalculateSize(FileStructureDTO projectStructureDTO) { foreach (var item in projectStructureDTO.NestedFiles) { if (item.Type == 0) { item.Size = (await CalculateSize(item)).Size; } else { item.Size = await _fileService.GetFileSize(item.Id); } projectStructureDTO.Size += item.Size; } return(projectStructureDTO); }
public async Task <ProjectStructureDTO> CreateEmptyAsync(int projectId, string projectName) { var emptyStructureDTO = new ProjectStructureDTO() { Id = projectId.ToString() }; var initialFileStructure = new FileStructureDTO() { Type = TreeNodeType.Folder, Id = Guid.NewGuid().ToString(), Details = $"Super important details of file {projectName}", Name = projectName }; emptyStructureDTO.NestedFiles.Add(initialFileStructure); var emptyStructure = _mapper.Map <ProjectStructure>(emptyStructureDTO); var createdProjectStructure = await _projectStructureRepository.CreateAsync(emptyStructure); return(await GetByIdAsync(createdProjectStructure.Id)); }
private ICollection <string> GetListOfFilesId(FileStructureDTO fileStructure) { var filesId = new List <string>(); var queue = new Queue <FileStructureDTO>(fileStructure.NestedFiles); while (queue.Count > 0) { var node = queue.Dequeue(); if (node.Type == TreeNodeType.File) { filesId.Add(node.Id); continue; } foreach (var subFolder in node.NestedFiles) { queue.Enqueue(subFolder); } } return(filesId); }
private async Task SaveFilesOnDisk(FileStructureDTO fileStructure, IDictionary <string, FileDTO> allFileInFileStructure, string path) { foreach (var node in fileStructure.NestedFiles) { if (node.Type == TreeNodeType.File) { var hasFile = allFileInFileStructure.TryGetValue(node.Id, out var file); if (!hasFile) { continue; } Directory.CreateDirectory(path); using (StreamWriter streamWriter = File.CreateText(Path.Combine(path, file.Name))) { await streamWriter.WriteAsync(file.Content).ConfigureAwait(false); } } else { await SaveFilesOnDisk(node, allFileInFileStructure, Path.Combine(path, node.Name)).ConfigureAwait(false); } } }
private async Task GetFilesRecursiveForGit(string sourseDir, FileStructureDTO fileStructureRoot, int userId, int projectId, bool isClone) { try { foreach (string directory in Directory.GetDirectories(sourseDir)) { var dirName = directory.Substring(directory.LastIndexOf('\\') + 1); var tempDir = fileStructureRoot.NestedFiles.SingleOrDefault(n => n.Name == dirName); //get temp libgit2sharp`s folder string ff = "nothing"; if (isClone) { ff = Directory.GetDirectories(sourseDir) .SingleOrDefault(d => d.Contains("_git2_")) .Substring(directory.LastIndexOf('\\') + 1); } //Add directory to projectStructure if (tempDir == null && dirName != ".git" && dirName != ff) { var nestedFileStructure = new FileStructureDTO() { Type = TreeNodeType.Folder, Name = dirName, Id = Guid.NewGuid().ToString() }; fileStructureRoot.NestedFiles.Add(nestedFileStructure); await GetFilesRecursiveForGit(directory, nestedFileStructure, userId, projectId, isClone); } } foreach (var file in Directory.GetFiles(sourseDir)) { var fileName = file.Substring(file.LastIndexOf('\\') + 1); var dirName = sourseDir.Substring(sourseDir.LastIndexOf('\\') + 1); Debug.WriteLine(dirName); var tempFile = fileStructureRoot.NestedFiles.SingleOrDefault(n => n.Name == fileName); if (tempFile == null && dirName != ".git") { var fileCreateDto = new FileCreateDTO(); fileCreateDto.Folder = dirName; fileCreateDto.Name = fileName; fileCreateDto.ProjectId = projectId; fileCreateDto.Content = await GetFileContent(file); var fileCreated = await _fileService.CreateAsync(fileCreateDto, userId); var nestedFileStructure = new FileStructureDTO() { Type = TreeNodeType.File, Name = fileName, Id = fileCreated.Id, Size = Encoding.Unicode.GetByteCount(fileCreated.Content) }; fileStructureRoot.NestedFiles.Add(nestedFileStructure); } else if (tempFile != null && dirName != ".git") { var targetFile = await _fileService.GetByIdAsync(tempFile.Id); targetFile.Content = await GetFileContent(file); targetFile.UpdatedAt = DateTime.Now; targetFile.UpdaterId = userId; await _fileService.UpdateAsync(_mapper.Map <FileUpdateDTO>(targetFile), userId); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }