Esempio n. 1
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));
        }
        public object Post([FromBody] dynamic model)
        {
            string    name;
            IFileInfo src;
            FileId    fileId, parentId;

            _helper.EnsurePostModelValid(model, out name, out fileId, out parentId);
            src = new FilesHelper(_fileService).GetExistingFileInfo(fileId.PhysicalPath);

            if (src == null)
            {
                throw new NotFoundException("file");
            }

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

            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 (src.Type == FileType.File && _fileService.GetDirectory(destPath).Exists || src.Type == FileType.Directory && _fileService.GetFile(destPath).Exists)
            {
                throw new AlreadyExistsException("name");
            }

            MoveOperation move = InitiateMove(src, destPath);

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

            return(_helper.ToJsonModel(move));
        }