public async Task CopyDirectory(IFileInfo source, IFileInfo dest, Func <IFileInfo, IFileInfo, Task> copyFile)
        {
            var destParent = dest.Parent;

            if (PathUtil.IsAncestor(source.Path, dest.Path) || source.Path.Equals(dest.Path, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException();
            }

            if (!source.Exists)
            {
                throw new DirectoryNotFoundException(source.Path);
            }
            if (destParent == null)
            {
                throw new IOException(dest.Path);
            }

            if (!dest.Exists)
            {
                _fileService.CreateDirectory(dest);
            }

            var dirTasks  = new List <Task>();
            var fileTasks = new List <Task>();
            var children  = Directory.EnumerateDirectories(source.Path, "*", SearchOption.AllDirectories);

            foreach (string dirPath in children)
            {
                var       relative    = dirPath.Substring(source.Path.Length).TrimStart(PathUtil.SEPARATORS);
                var       destDirPath = Path.Combine(dest.Path, relative);
                IFileInfo destDir     = _fileService.GetDirectory(destDirPath);

                dirTasks.Add(Task.Run(() => {
                    if (!destDir.Exists)
                    {
                        _fileService.CreateDirectory(destDir);
                    }

                    foreach (string filePath in Directory.EnumerateFiles(dirPath))
                    {
                        fileTasks.Add(copyFile(_fileService.GetFile(filePath),
                                               _fileService.GetFile(Path.Combine(destDirPath, PathUtil.GetName(filePath)))));
                    }
                }));
            }

            foreach (var filePath in Directory.EnumerateFiles(source.Path))
            {
                fileTasks.Add(copyFile(_fileService.GetFile(filePath),
                                       _fileService.GetFile(Path.Combine(dest.Path, PathUtil.GetName(filePath)))));
            }

            await Task.WhenAll(dirTasks);

            await Task.WhenAll(fileTasks);
        }
        public void CanCreateDeleteDirectory()
        {
            var tempDir   = Path.GetTempPath();
            var randomDir = Path.GetRandomFileName();

            var fullName = Path.Combine(tempDir, randomDir);

            var newDirectoryInfo = FileProvider.CreateDirectory(fullName);

            Assert.AreEqual(fullName, newDirectoryInfo.Path);

            var newDirectoryInfo2 = FileProvider.CreateDirectory(fullName);

            Assert.AreEqual(fullName, newDirectoryInfo2.Path);
        }
Exemple #3
0
 private void PrepareOutputFolder(string folderPath)
 {
     if (_fileProvider.DirectoryExists(folderPath))
     {
         foreach (var path in _fileProvider.EnumerateFiles(folderPath))
         {
             _fileProvider.Delete(path);
         }
     }
     else
     {
         _fileProvider.CreateDirectory(folderPath);
     }
 }
 public IFileInfo CreateDirectory(IFileInfo directory)
 {
     return(_next.CreateDirectory(directory));
 }
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.parent == null)
            {
                throw new ApiArgumentException("parent");
            }
            if (!(model.parent is JObject))
            {
                throw new ApiArgumentException("parent", ApiArgumentException.EXPECTED_OBJECT);
            }

            //
            // Check Id
            string parentUuid = DynamicHelper.Value(model.parent.id);

            if (parentUuid == null)
            {
                throw new ApiArgumentException("parent.id");
            }

            FileId fileId = FileId.FromUuid(parentUuid);

            if (!_provider.DirectoryExists(fileId.PhysicalPath))
            {
                throw new NotFoundException("parent");
            }

            //
            // Check Name
            string name = DynamicHelper.Value(model.name);

            if (!PathUtil.IsValidFileName(name))
            {
                throw new ApiArgumentException("model.name");
            }

            //
            // Check Type
            string type = DynamicHelper.Value(model.type);

            FileType fileType;

            if (type == null || !Enum.TryParse(type, true, out fileType))
            {
                throw new ApiArgumentException("model.type");
            }

            DateTime?created      = DynamicHelper.To <DateTime>(model.created);
            DateTime?lastAccess   = DynamicHelper.To <DateTime>(model.last_access);
            DateTime?lastModified = DynamicHelper.To <DateTime>(model.last_modified);

            var creationPath = Path.Combine(fileId.PhysicalPath, name);

            if (_provider.DirectoryExists(creationPath) || _provider.FileExists(creationPath))
            {
                throw new AlreadyExistsException("name");
            }

            IFileInfo info = fileType == FileType.File ? _provider.CreateFile(creationPath) : _provider.CreateDirectory(creationPath);

            _provider.SetFileTime(info.Path, lastAccess, lastModified, created);

            dynamic file = _helper.ToJsonModel(info);

            return(Created(FilesHelper.GetLocation(file.id), _helper.ToJsonModel(info)));
        }