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)));
        }
        private object InfoToJsonModel(IFileInfo info, Fields fields = null, bool full = true)
        {
            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj    = new ExpandoObject();
            var     fileId = FileId.FromPhysicalPath(info.Path);
            bool?   exists = null;

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = info.Name;
            }

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = fileId.Uuid;
            }

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = null;
            }

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                obj.physical_path = info.Path;
            }

            //
            // exists
            if (fields.Exists("exists"))
            {
                exists     = exists ?? info.Exists;
                obj.exists = exists.Value;
            }

            //
            // created
            if (fields.Exists("created"))
            {
                exists      = exists ?? info.Exists;
                obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null;
            }

            //
            // last_modified
            if (fields.Exists("last_modified"))
            {
                exists            = exists ?? info.Exists;
                obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null;
            }

            //
            // last_access
            if (fields.Exists("last_access"))
            {
                exists          = exists ?? info.Exists;
                obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null;
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = GetParentJsonModelRef(info.Path, fields.Filter("parent"));
            }

            //
            // claims
            if (fields.Exists("claims"))
            {
                obj.claims = _fileProvider.GetClaims(info.Path);
            }


            return(Core.Environment.Hal.Apply(Defines.FilesResource.Guid, obj, full));
        }
        private object DirectoryToJsonModel(IFileInfo info, Fields fields = null, bool full = true)
        {
            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj    = new ExpandoObject();
            var     fileId = FileId.FromPhysicalPath(info.Path);
            bool?   exists = null;

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = info.Name;
            }

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = fileId.Uuid;
            }

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = Enum.GetName(typeof(FileType), FileType.Directory).ToLower();
            }

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                obj.physical_path = info.Path;
            }

            //
            // exists
            if (fields.Exists("exists"))
            {
                exists     = exists ?? info.Exists;
                obj.exists = exists.Value;
            }

            //
            // created
            if (fields.Exists("created"))
            {
                exists      = exists ?? info.Exists;
                obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null;
            }

            //
            // last_modified
            if (fields.Exists("last_modified"))
            {
                exists            = exists ?? info.Exists;
                obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null;
            }

            //
            // last_access
            if (fields.Exists("last_access"))
            {
                exists          = exists ?? info.Exists;
                obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null;
            }

            //
            // total_files
            // We check for the 'full' flag to avoid unauthorized exception when referencing directories
            // Listing a directories content requires extra permissions
            if (fields.Exists("total_files") && full)
            {
                exists = exists ?? info.Exists;
                if (_fileProvider.IsAccessAllowed(info.Path, FileAccess.Read))
                {
                    obj.total_files = exists.Value ? _fileProvider.GetFiles(info.Path, "*").Count() + _fileProvider.GetDirectories(info.Path, "*").Count() : 0;
                }
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = GetParentJsonModelRef(info.Path, fields.Filter("parent"));
            }

            //
            // claims
            if (fields.Exists("claims"))
            {
                obj.claims = _fileProvider.GetClaims(info.Path);
            }

            return(Core.Environment.Hal.Apply(Defines.DirectoriesResource.Guid, obj, full));
        }
Esempio n. 4
0
 private void AddHttpLinkHeader(FileId fileId)
 {
     Context.Response.Headers.Add("Link", $"</{Defines.FILES_PATH}/{fileId.Uuid}>; rel=\"meta\"; title=\"file metadata\", </{Defines.CONTENT_PATH}/{fileId.Uuid}>; rel=\"self\"; title=\"self\"");
 }