public object Head()
        {
            Site   site;
            FileId parentId;
            string nameFilter, pathFilter, physicalPath;

            Parse(out parentId, out site, out nameFilter, out pathFilter);

            if (pathFilter != null)
            {
                return(GetByPath(pathFilter == string.Empty ? "/" : pathFilter));
            }

            if (parentId == null || site == null)
            {
                return(NotFound());
            }

            physicalPath = FilesHelper.GetPhysicalPath(site, parentId.Path);

            if (!_fileProvider.DirectoryExists(physicalPath))
            {
                return(NotFound());
            }

            var dirInfo  = _fileProvider.GetDirectory(physicalPath);
            var children = GetChildren(site, parentId.Path, dirInfo, nameFilter, false);

            // Set HTTP header for total count
            this.Context.Response.SetItemsCount(children.Count());
            this.Context.Response.Headers[HeaderNames.AcceptRanges] = _units;

            return(Ok());
        }
        public object Head()
        {
            Fields fields;
            FileId parentId;
            string nameFilter, physicalPath;

            PreGet(out parentId, out nameFilter, out physicalPath, out fields);

            if (physicalPath != null)
            {
                return(GetByPhysicalPath(physicalPath, fields));
            }

            if (parentId != null && !_provider.DirectoryExists(parentId.PhysicalPath))
            {
                return(NotFound());
            }

            IEnumerable <IFileInfo> children = parentId != null?GetChildren(parentId.PhysicalPath, nameFilter) : GetFromLocations(nameFilter);

            // Set HTTP header for total count
            this.Context.Response.SetItemsCount(children.Count());
            this.Context.Response.Headers[HeaderNames.AcceptRanges] = _units;

            return(Ok());
        }
        internal string UpdateFile(dynamic model, string physicalPath)
        {
            DateTime?created      = null;
            DateTime?lastAccess   = null;
            DateTime?lastModified = null;

            var file = _fileProvider.GetFile(physicalPath);

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

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

            if (model.name != null)
            {
                string name = DynamicHelper.Value(model.name);

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

                var newPath = Path.Combine(file.Parent.Path, name);

                if (!newPath.Equals(physicalPath, StringComparison.OrdinalIgnoreCase))
                {
                    if (_fileProvider.FileExists(newPath) || _fileProvider.DirectoryExists(newPath))
                    {
                        throw new AlreadyExistsException("name");
                    }

                    _fileProvider.Move(physicalPath, newPath);

                    physicalPath = newPath;
                }
            }

            _fileProvider.SetFileTime(physicalPath, lastAccess, lastModified, created);

            return(physicalPath);
        }
Пример #4
0
        public object Post([FromBody] dynamic model)
        {
            string   name;
            FileType fileType;
            FileId   fileId, parentId;

            _helper.EnsurePostModelValid(model, out name, out fileId, out parentId);

            try {
                fileType = FilesHelper.GetFileType(fileId.PhysicalPath);
            }
            catch (FileNotFoundException) {
                throw new NotFoundException("file");
            }

            if (!_fileService.DirectoryExists(parentId.PhysicalPath))
            {
                throw new NotFoundException("parent");
            }

            var src = fileType == FileType.File ? _fileService.GetFile(fileId.PhysicalPath) : _fileService.GetDirectory(fileId.PhysicalPath);

            string destPath = Path.Combine(parentId.PhysicalPath, name == null ? src.Name : name);

            if (PathUtil.IsAncestor(src.Path, destPath) || src.Path.Equals(destPath, StringComparison.OrdinalIgnoreCase))
            {
                throw new ApiArgumentException("parent", "The destination folder is a subfolder of the source");
            }

            if (fileType == FileType.File && _fileService.DirectoryExists(destPath) || fileType == FileType.Directory && _fileService.FileExists(destPath))
            {
                throw new AlreadyExistsException("name");
            }

            MoveOperation copy = InitiateCopy(src, destPath);

            Context.Response.StatusCode = (int)HttpStatusCode.Accepted;
            Context.Response.Headers.Add("Location", MoveHelper.GetLocation(copy.Id, true));

            return(_helper.ToJsonModel(copy));
        }
Пример #5
0
        public async Task CopyDirectory(string sourcePath, string destPath, Func <string, string, Task> copyFile)
        {
            var source     = _fileService.GetDirectory(sourcePath);
            var dest       = _fileService.GetDirectory(destPath);
            var destParent = dest.Parent;

            if (PathUtil.IsAncestor(sourcePath, destPath) || 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.Path);
            }

            var tasks    = 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);

                tasks.Add(Task.Run(() => {
                    if (!_fileService.DirectoryExists(destDirPath))
                    {
                        _fileService.CreateDirectory(destDirPath);
                    }

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

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

            await Task.WhenAll(tasks);
        }
Пример #6
0
 private void PrepareOutputFolder(string folderPath)
 {
     if (_fileProvider.DirectoryExists(folderPath))
     {
         foreach (var path in _fileProvider.EnumerateFiles(folderPath))
         {
             _fileProvider.Delete(path);
         }
     }
     else
     {
         _fileProvider.CreateDirectory(folderPath);
     }
 }