public async Task <ActionResult> Detail(Guid id)
        {
            // Make sure an action was passed in
            if (String.IsNullOrEmpty(Request["action"]))
            {
                return(RedirectToAction("Error", "Home", new { error = "No action provided" }));
            }

            // Get the specific file from DocumentDB
            var file = DocumentRepository <FileModel> .GetItem("Files",
                                                               i => true);

            // Check for null file
            if (file == null)
            {
                return(RedirectToAction("Error", "Home", new { error = "Files does not exist" }));
            }

            // Use discovery to determine endpoint to leverage
            List <WopiAction> discoData = await WopiUtil.GetDiscoveryInfo();

            var fileExt = file.BaseFileName.Substring(file.BaseFileName.LastIndexOf('.') + 1).ToLower();
            var action  = discoData.FirstOrDefault(i => i.name == Request["action"] && i.ext == fileExt);

            // Make sure the action isn't null
            if (action != null)
            {
                string urlsrc = WopiUtil.GetActionUrl(action, file, Request.Url.Authority);

                // Generate JWT token for the user/document

                /*
                 *              WopiSecurity wopiSecurity = new WopiSecurity();
                 *              var token = wopiSecurity.GenerateToken(User.Identity.Name.ToLower(), getUserContainer(), id.ToString());
                 *              ViewData["access_token"] = wopiSecurity.WriteToken(token);
                 *              ViewData["access_token_ttl"] = token.ValidTo.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
                 */

                ViewData["access_token"]     = "MY-VERY-SUPER-SECRET-TOKEN";
                ViewData["access_token_ttl"] = 1529837761073;
                ViewData["wopi_urlsrc"]      = urlsrc;
                return(View());
            }
            else
            {
                // This will only hit if the extension isn't supported by WOPI
                return(RedirectToAction("Error", "Home", new { error = "File is not a supported WOPI extension" }));
            }
        }
Ejemplo n.º 2
0
        public async System.Threading.Tasks.Task <string> Url(UserAction ua = null)
        {
            if (ua == null)
            {
                ua = new UserAction();
            }

            // Use discovery to determine endpoint to leverage
            List <WopiAction> discoData = await WopiUtil.GetDiscoveryInfo();

            var fileExt = BaseFileName.Substring(BaseFileName.LastIndexOf('.') + 1).ToLower();
            var action  = discoData.FirstOrDefault(i => i.name == ua.Action && i.ext == fileExt);

            if (action != null)
            {
                string urlsrc = WopiUtil.GetActionUrl(action, this, ServerUtil.Config().Domain);

                WopiSecurity wopiSecurity = new WopiSecurity();

                var token = wopiSecurity.GenerateToken(id.ToString(), ua.UserId, ua.UserDisplayName);

                if (action.name == "edit")
                {
                    token.ReadOnly = false;
                }
                else
                {
                    token.ReadOnly = true;
                }
                token.Save();

                return(String.Format("{0}&access_token={1}&access_token_ttl={2}", urlsrc, token.Identity, token.ValidTo().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString()));
            }
            else
            {
                // This will only hit if the extension isn't supported by WOPI
                throw new Exception("Bad action on this file.");
            }
        }