Ejemplo n.º 1
0
        public Action <Stream> ProcessCobalt(IWopiFile file, ClaimsPrincipal principal, byte[] newContent)
        {
            // Refactoring tip: there are more ways of initializing Atom
            AtomFromByteArray atomRequest  = new AtomFromByteArray(newContent);
            RequestBatch      requestBatch = new RequestBatch();

            object          ctx;
            ProtocolVersion protocolVersion;

            requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion);
            var cobaltFile = GetCobaltFile(file, principal);

            cobaltFile.CobaltEndpoint.ExecuteRequestBatch(requestBatch);

            if (requestBatch.Requests.Any(request => request is PutChangesRequest && request.PartitionId == FilePartitionId.Content))
            {
                using (var stream = file.GetWriteStream())
                {
                    new GenericFda(cobaltFile.CobaltEndpoint).GetContentStream().CopyTo(stream);
                }
            }
            var             response     = requestBatch.SerializeOutputToProtocol(protocolVersion);
            Action <Stream> copyToAction = s => { response.CopyTo(s); };

            return(copyToAction);
        }
Ejemplo n.º 2
0
 public Stream GetFileStream(IWopiFile file, ClaimsPrincipal principal)
 {
     //TODO: use in filescontroller
     using var ms = new MemoryStream();
     new GenericFda(GetCobaltFile(file, principal).CobaltEndpoint).GetContentStream().CopyTo(ms);
     return(ms);
 }
Ejemplo n.º 3
0
        private EditSession GetEditSession(string fileId)
        {
            var sessionId           = /*Context.Session.GetString("SessionID");
                                       * if (string.IsNullOrEmpty(sessionId))
                                       * {
                                       * sessionId = Guid.NewGuid().ToString();
                                       * Context.Session.SetString("SessionID", sessionId);
                                       * }
                                       * sessionId += "|" +*/fileId;
            EditSession editSession = SessionManager.Current.GetSession(sessionId);

            if (editSession == null)
            {
                IWopiFile file = FileProvider.GetWopiFile(fileId);

                //TODO: remove hardcoded action 'Edit'
                if (WopiDiscoverer.RequiresCobalt(file.Extension, WopiActionEnum.Edit))
                {
                    editSession = new CobaltSession(file, sessionId);
                }
                else
                {
                    editSession = new FileSession(file, sessionId);
                }
                SessionManager.Current.AddSession(editSession);
            }

            return(editSession);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 protected EditSession(IWopiFile file, string sessionId, string login, string name, string email, bool isAnonymous)
 {
     //TODO: work with users
     File = file;
     SessionId = sessionId;
     Name = name;
     Login = login;
     Email = email;
     IsAnonymous = isAnonymous;
 }
Ejemplo n.º 6
0
 protected EditSession(IWopiFile file, string sessionId, string login, string name, string email, bool isAnonymous)
 {
     //TODO: work with users
     File        = file;
     SessionId   = sessionId;
     Name        = name;
     Login       = login;
     Email       = email;
     IsAnonymous = isAnonymous;
 }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public async Task <ActionResult> Detail(string id)
        {
            WopiSecurityHandler securityHandler = new WopiSecurityHandler();

            IWopiFile file  = StorageProvider.GetWopiFile(id);
            var       token = securityHandler.GenerateAccessToken("Anonymous", file.Identifier);


            ViewData["access_token"] = securityHandler.WriteToken(token);
            //TODO: fix
            //ViewData["access_token_ttl"] = //token.ValidTo

            //http://dotnet-stuff.com/tutorials/aspnet-mvc/how-to-render-different-layout-in-asp-net-mvc


            var extension = file.Extension.TrimStart('.');

            ViewData["urlsrc"] = await UrlGenerator.GetFileUrlAsync(extension, $"{WopiOptions.Value.HostUrl}/wopi/files/{id}", WopiActionEnum.Edit);

            ViewData["favicon"] = await Discoverer.GetApplicationFavIconAsync(extension);

            return(View());
        }
Ejemplo n.º 9
0
 public FileSession(IWopiFile file, string sessionId, string login = "******", string name = "Anonymous", string email = "", bool isAnonymous = true)
     : base(file, sessionId, login, name, email, isAnonymous)
 {
 }
Ejemplo n.º 10
0
 public CobaltSession(IWopiFile file, string sessionId, string login = "******", string name = "Anonymous", string email = "", bool isAnonymous = true)
     : base(file, sessionId, login, name, email, isAnonymous)
 {
 }
Ejemplo n.º 11
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);
        }