public object Patch([FromBody] dynamic model, string id)
        {
            FileId fileId = FileId.FromUuid(id);

            if (!_provider.FileExists(fileId.PhysicalPath) && !_provider.DirectoryExists(fileId.PhysicalPath))
            {
                return(NotFound());
            }

            dynamic result = null;

            switch (FilesHelper.GetFileType(fileId.PhysicalPath))
            {
            case FileType.File:
                result = PatchFile(model, fileId);
                break;

            case FileType.Directory:
                result = PatchDirectory(model, fileId);
                break;

            default:
                return(new StatusCodeResult((int)HttpStatusCode.MethodNotAllowed));
            }

            if (result.id != id)
            {
                return(LocationChanged(FilesHelper.GetLocation(result.id), result));
            }

            return(result);
        }
Exemplo n.º 2
0
        public object Patch([FromBody] dynamic model, string id)
        {
            FileId fileId = FileId.FromUuid(id);

            IFileInfo info = _helper.GetExistingFileInfo(fileId.PhysicalPath);

            if (info == null)
            {
                return(NotFound());
            }

            dynamic result = null;

            switch (info.Type)
            {
            case FileType.File:
                result = _helper.ToJsonModel(_helper.UpdateFile(model, info));
                break;

            case FileType.Directory:
                result = _helper.ToJsonModel(_helper.UpdateDirectory(model, info));
                break;

            default:
                return(new StatusCodeResult((int)HttpStatusCode.MethodNotAllowed));
            }

            if (result.id != id)
            {
                return(LocationChanged(FilesHelper.GetLocation(result.id), result));
            }

            return(result);
        }
        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)));
        }