public Core.File ToFile(File file)
        {
            if (file == null)
                return null;

            var errorFile = file as SharePointFileErrorEntry;
            if (errorFile != null)
                return new Core.File
                {
                    ID = MakeId(errorFile.ID),
                    FolderID = MakeId(GetParentFolderId(errorFile.ID)),
                    CreateBy = Owner,
                    CreateOn = DateTime.UtcNow,
                    ModifiedBy = Owner,
                    ModifiedOn = DateTime.UtcNow,
                    ProviderId = ID,
                    ProviderKey = ProviderKey,
                    RootFolderCreator = Owner,
                    RootFolderId = MakeId(RootFolder.ServerRelativeUrl),
                    RootFolderType = RootFolderType,
                    Title = MakeTitle(GetTitleById(errorFile.ID)),
                    Error = errorFile.Error
                };

            var result = new Core.File
            {
                ID = MakeId(file.ServerRelativeUrl),
                Access = Core.Security.FileShare.None,
                //ContentLength = file.Length,
                CreateBy = Owner,
                CreateOn = file.TimeCreated.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeCreated) : file.TimeCreated,
                FileStatus = FileStatus.None,
                FolderID = MakeId(GetParentFolderId(file.ServerRelativeUrl)),
                ModifiedBy = Owner,
                ModifiedOn = file.TimeLastModified.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeLastModified) : file.TimeLastModified,
                NativeAccessor = file,
                ProviderId = ID,
                ProviderKey = ProviderKey,
                Title = MakeTitle(file.Name),
                RootFolderId = MakeId(SpRootFolderId),
                RootFolderType = RootFolderType,
                RootFolderCreator = Owner,
                SharedByMe = false,
                Version = 1
            };

            if (file.IsPropertyAvailable("Length"))
            {
                result.ContentLength = file.Length;
            }
            else if(file.IsObjectPropertyInstantiated("ListItemAllFields"))
            {
                result.ContentLength = Convert.ToInt64(file.ListItemAllFields["File_x0020_Size"]);
            }

            return result;
        }
        private void EnsurePublish(File file, string publishComment)
        {
            // Load dependent data.
            var parentList = file.ListItemAllFields.ParentList;
            if (!parentList.IsPropertyAvailable("EnableMinorVersion") || !parentList.IsPropertyAvailable("EnableModeration") || !file.IsPropertyAvailable("Level") || !file.IsPropertyAvailable("ListItemAllFields"))
            {
                _clientContext.Load(parentList);
                _clientContext.Load(file);
                _clientContext.ExecuteQuery();
            }

            var isDirty = false;
            if (file.Level == FileLevel.Checkout)
            {
                file.CheckIn(string.Empty, CheckinType.MajorCheckIn);
                isDirty = true;
            }
            if (parentList.EnableMinorVersions && file.Level != FileLevel.Published)
            {
                file.Publish(publishComment);
                isDirty = true;
            }
            if (parentList.EnableModeration && Convert.ToInt32(file.ListItemAllFields["_ModerationStatus"]) != 0)
            {
                file.Approve(string.Empty);
                isDirty = true;
            }

            if (isDirty)
            {
                file.RefreshLoad();
                _clientContext.ExecuteQuery();
            }
        }