Beispiel #1
0
        public static CheckFileInfo GetCheckFileInfo(this IWopiFile file, ClaimsPrincipal principal, HostCapabilities capabilities)
        {
            CheckFileInfo CheckFileInfo = new CheckFileInfo();

            if (principal != null)
            {
                CheckFileInfo.UserId           = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                CheckFileInfo.UserFriendlyName = principal.FindFirst(ClaimTypes.Name)?.Value;

                WopiUserPermissions permissions = (WopiUserPermissions)Enum.Parse(typeof(WopiUserPermissions), principal.FindFirst(WopiClaimTypes.UserPermissions).Value);

                CheckFileInfo.ReadOnly = permissions.HasFlag(WopiUserPermissions.ReadOnly);
                CheckFileInfo.RestrictedWebViewOnly   = permissions.HasFlag(WopiUserPermissions.RestrictedWebViewOnly);
                CheckFileInfo.UserCanAttend           = permissions.HasFlag(WopiUserPermissions.UserCanAttend);
                CheckFileInfo.UserCanNotWriteRelative = permissions.HasFlag(WopiUserPermissions.UserCanNotWriteRelative);
                CheckFileInfo.UserCanPresent          = permissions.HasFlag(WopiUserPermissions.UserCanPresent);
                CheckFileInfo.UserCanRename           = permissions.HasFlag(WopiUserPermissions.UserCanRename);
                CheckFileInfo.UserCanWrite            = permissions.HasFlag(WopiUserPermissions.UserCanWrite);
                CheckFileInfo.WebEditingDisabled      = permissions.HasFlag(WopiUserPermissions.WebEditingDisabled);
            }
            else
            {
                CheckFileInfo.IsAnonymousUser = true;
            }

            CheckFileInfo.OwnerId = file.Owner;

            // Set host capabilities
            CheckFileInfo.SupportsCoauth             = capabilities.SupportsCoauth;
            CheckFileInfo.SupportsFolders            = capabilities.SupportsFolders;
            CheckFileInfo.SupportsLocks              = capabilities.SupportsLocks;
            CheckFileInfo.SupportsGetLock            = capabilities.SupportsGetLock;
            CheckFileInfo.SupportsExtendedLockLength = capabilities.SupportsExtendedLockLength;
            CheckFileInfo.SupportsEcosystem          = capabilities.SupportsEcosystem;
            CheckFileInfo.SupportsGetFileWopiSrc     = capabilities.SupportsGetFileWopiSrc;
            CheckFileInfo.SupportedShareUrlTypes     = capabilities.SupportedShareUrlTypes;
            CheckFileInfo.SupportsScenarioLinks      = capabilities.SupportsScenarioLinks;
            CheckFileInfo.SupportsSecureStore        = capabilities.SupportsSecureStore;
            CheckFileInfo.SupportsUpdate             = capabilities.SupportsUpdate;
            CheckFileInfo.SupportsCobalt             = capabilities.SupportsCobalt;
            CheckFileInfo.SupportsRename             = capabilities.SupportsRename;
            CheckFileInfo.SupportsDeleteFile         = capabilities.SupportsDeleteFile;
            CheckFileInfo.SupportsUserInfo           = capabilities.SupportsUserInfo;
            CheckFileInfo.SupportsFileCreation       = capabilities.SupportsFileCreation;

            using (var stream = file.GetReadStream())
            {
                byte[] checksum = SHA.ComputeHash(stream);
                CheckFileInfo.SHA256 = Convert.ToBase64String(checksum);
            }
            CheckFileInfo.BaseFileName     = file.Name;
            CheckFileInfo.FileExtension    = "." + file.Extension.TrimStart('.');
            CheckFileInfo.Version          = file.LastWriteTimeUtc.ToString("s");
            CheckFileInfo.LastModifiedTime = file.LastWriteTimeUtc.ToString("o");
            CheckFileInfo.Size             = file.Exists ? file.Length : 0;
            return(CheckFileInfo);
        }
Beispiel #2
0
        private CobaltFile GetCobaltFile(IWopiFile file, ClaimsPrincipal principal)
        {
            var disposal = new DisposalEscrow(file.Owner);
            CobaltFilePartitionConfig content = new CobaltFilePartitionConfig
            {
                IsNewFile              = true,
                HostBlobStore          = new TemporaryHostBlobStore(new TemporaryHostBlobStore.Config(), disposal, file.Identifier + @".Content"),
                cellSchemaIsGenericFda = true,
                CellStorageConfig      = new CellStorageConfig(),
                Schema      = CobaltFilePartition.Schema.ShreddedCobalt,
                PartitionId = FilePartitionId.Content
            };

            CobaltFilePartitionConfig coauth = new CobaltFilePartitionConfig
            {
                IsNewFile              = true,
                HostBlobStore          = new TemporaryHostBlobStore(new TemporaryHostBlobStore.Config(), disposal, file.Identifier + @".CoauthMetadata"),
                cellSchemaIsGenericFda = false,
                CellStorageConfig      = new CellStorageConfig(),
                Schema      = CobaltFilePartition.Schema.ShreddedCobalt,
                PartitionId = FilePartitionId.CoauthMetadata
            };

            CobaltFilePartitionConfig wacupdate = new CobaltFilePartitionConfig
            {
                IsNewFile              = true,
                HostBlobStore          = new TemporaryHostBlobStore(new TemporaryHostBlobStore.Config(), disposal, file.Identifier + @".WordWacUpdate"),
                cellSchemaIsGenericFda = false,
                CellStorageConfig      = new CellStorageConfig(),
                Schema      = CobaltFilePartition.Schema.ShreddedCobalt,
                PartitionId = FilePartitionId.WordWacUpdate
            };

            Dictionary <FilePartitionId, CobaltFilePartitionConfig> partitionConfigs = new Dictionary <FilePartitionId, CobaltFilePartitionConfig> {
                { FilePartitionId.Content, content }, { FilePartitionId.WordWacUpdate, wacupdate }, { FilePartitionId.CoauthMetadata, coauth }
            };


            var tempCobaltFile = new CobaltFile(disposal, partitionConfigs, new CobaltHostLockingStore(principal), null);

            if (file.Exists)
            {
                using (var stream = file.GetReadStream())
                {
                    var     srcAtom = new AtomFromStream(stream);
                    Metrics o1;
                    tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).SetStream(RootId.Default.Value, srcAtom, out o1);
                    tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).GetStream(RootId.Default.Value).Flush();
                }
            }
            return(tempCobaltFile);
        }
Beispiel #3
0
        /// <summary>
        /// Returns a CheckFileInfo model according to https://wopi.readthedocs.io/projects/wopirest/en/latest/files/CheckFileInfo.html
        /// </summary>
        /// <param name="file">File properties of which should be returned.</param>
        /// <param name="principal">A user object which the CheckFileInfo should be correlated with.</param>
        /// <param name="capabilities">WOPI host capabilities</param>
        /// <returns>CheckFileInfo model</returns>
        public static CheckFileInfo GetCheckFileInfo(this IWopiFile file, ClaimsPrincipal principal, HostCapabilities capabilities)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (capabilities is null)
            {
                throw new ArgumentNullException(nameof(capabilities));
            }

            var checkFileInfo = new CheckFileInfo();

            if (principal is not null)
            {
                checkFileInfo.UserId           = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value.ToSafeIdentity();
                checkFileInfo.UserFriendlyName = principal.FindFirst(ClaimTypes.Name)?.Value;

                var permissions = (WopiUserPermissions)Enum.Parse(typeof(WopiUserPermissions), principal.FindFirst(WopiClaimTypes.USER_PERMISSIONS).Value);

                checkFileInfo.ReadOnly = permissions.HasFlag(WopiUserPermissions.ReadOnly);
                checkFileInfo.RestrictedWebViewOnly   = permissions.HasFlag(WopiUserPermissions.RestrictedWebViewOnly);
                checkFileInfo.UserCanAttend           = permissions.HasFlag(WopiUserPermissions.UserCanAttend);
                checkFileInfo.UserCanNotWriteRelative = permissions.HasFlag(WopiUserPermissions.UserCanNotWriteRelative);
                checkFileInfo.UserCanPresent          = permissions.HasFlag(WopiUserPermissions.UserCanPresent);
                checkFileInfo.UserCanRename           = permissions.HasFlag(WopiUserPermissions.UserCanRename);
                checkFileInfo.UserCanWrite            = permissions.HasFlag(WopiUserPermissions.UserCanWrite);
                checkFileInfo.WebEditingDisabled      = permissions.HasFlag(WopiUserPermissions.WebEditingDisabled);
            }
            else
            {
                checkFileInfo.IsAnonymousUser = true;
            }

            checkFileInfo.OwnerId = file.Owner.ToSafeIdentity();

            // Set host capabilities
            checkFileInfo.SupportsCoauth             = capabilities.SupportsCoauth;
            checkFileInfo.SupportsFolders            = capabilities.SupportsFolders;
            checkFileInfo.SupportsLocks              = capabilities.SupportsLocks;
            checkFileInfo.SupportsGetLock            = capabilities.SupportsGetLock;
            checkFileInfo.SupportsExtendedLockLength = capabilities.SupportsExtendedLockLength;
            checkFileInfo.SupportsEcosystem          = capabilities.SupportsEcosystem;
            checkFileInfo.SupportsGetFileWopiSrc     = capabilities.SupportsGetFileWopiSrc;
            checkFileInfo.SupportedShareUrlTypes     = capabilities.SupportedShareUrlTypes;
            checkFileInfo.SupportsScenarioLinks      = capabilities.SupportsScenarioLinks;
            checkFileInfo.SupportsSecureStore        = capabilities.SupportsSecureStore;
            checkFileInfo.SupportsUpdate             = capabilities.SupportsUpdate;
            checkFileInfo.SupportsCobalt             = capabilities.SupportsCobalt;
            checkFileInfo.SupportsRename             = capabilities.SupportsRename;
            checkFileInfo.SupportsDeleteFile         = capabilities.SupportsDeleteFile;
            checkFileInfo.SupportsUserInfo           = capabilities.SupportsUserInfo;
            checkFileInfo.SupportsFileCreation       = capabilities.SupportsFileCreation;

            using (var stream = file.GetReadStream())
            {
                var checksum = Sha.ComputeHash(stream);
                checkFileInfo.Sha256 = Convert.ToBase64String(checksum);
            }
            checkFileInfo.BaseFileName     = file.Name;
            checkFileInfo.FileExtension    = "." + file.Extension.TrimStart('.');
            checkFileInfo.Version          = file.LastWriteTimeUtc.ToString("s", CultureInfo.InvariantCulture);
            checkFileInfo.LastModifiedTime = file.LastWriteTimeUtc.ToString("o", CultureInfo.InvariantCulture);
            checkFileInfo.Size             = file.Exists ? file.Length : 0;
            return(checkFileInfo);
        }