public async Task <HttpResponseMessage> CheckFileInfo(string file_id)
        {
            var          checkFileInfoRequest = new CheckFileInfoRequest(this.Request, file_id);
            WopiResponse wopiResponse         = null;

            try
            {
                if (await Authorize(checkFileInfoRequest))
                {
                    if (await WopiProof.Validate(checkFileInfoRequest))
                    {
                        wopiResponse = await CheckFileInfo(checkFileInfoRequest);
                    }
                    else
                    {
                        wopiResponse = checkFileInfoRequest.ResponseServerError("Proof validation failed");
                    }
                }
                else
                {
                    wopiResponse = checkFileInfoRequest.ResponseUnauthorized();
                }
            }
            catch (Exception ex)
            {
                wopiResponse = checkFileInfoRequest.ResponseServerError(ex.Message);
            }
            return(wopiResponse.ToHttpResponse());
        }
Exemplo n.º 2
0
        public override async Task <WopiResponse> CheckFileInfo(CheckFileInfoRequest checkFileInfoRequest)
        {
            var userId = WopiSecurity.GetIdentityNameFromToken(checkFileInfoRequest.AccessToken);

            // For this demo server, determine tenant by host part of email address
            var tenant = new MailAddress(userId).Host.Replace(".", "-");

            // Lookup the file in the database using special repository method which grants access limited access to users in same tenant (same email domain)
            var wopiFileRepository = new WopiFileRepository();
            var response           = await wopiFileRepository.GetFileInfoByTenantUser(checkFileInfoRequest.ResourceId, userId, tenant);

            // Check for null file
            if (response.Item1 == HttpStatusCode.NotFound)
            {
                return(checkFileInfoRequest.ResponseNotFound());
            }

            else if (response.Item1 == HttpStatusCode.OK)
            {
                var wopiFile = response.Item2;
                // Get discovery information
                var actions = await WopiDiscovery.GetActions();

                string hostViewUrl = null, hostEditUrl = null;

                var closeUrl = String.Format("https://{0}", checkFileInfoRequest.RequestUri.Authority);

                var view = actions.FirstOrDefault(i => i.ext == wopiFile.FileExtension && i.name == "view");
                if (view != null)
                {
                    hostViewUrl = WopiDiscovery.GetActionUrl(view, wopiFile.FileId.ToString(), checkFileInfoRequest.RequestUri.Authority);
                }

                var edit = actions.FirstOrDefault(i => i.ext == wopiFile.FileExtension && i.name == "edit");
                if (edit != null)
                {
                    hostEditUrl = WopiDiscovery.GetActionUrl(edit, wopiFile.FileId.ToString(), checkFileInfoRequest.RequestUri.Authority);
                }

                // Write the response and return a success 200
                var wopiResponse = checkFileInfoRequest.ResponseOK(wopiFile.FileName, wopiFile.OwnerId, wopiFile.Size, userId, wopiFile.Version.ToString());
                // Add optional items
                wopiResponse.CloseUrl = new Uri(closeUrl);
                if (hostViewUrl != null)
                {
                    wopiResponse.HostViewUrl = new Uri(hostViewUrl);
                }
                if (hostEditUrl != null)
                {
                    wopiResponse.HostEditUrl = new Uri(hostEditUrl);
                }

                wopiResponse.UserInfo = wopiFile.FilePermissions.First().UserInfo;

                return(wopiResponse);
            }
            else
            {
                return(checkFileInfoRequest.ResponseServerError(string.Format("Unknown response from WopiFileRepository.GetFileInfoByTenantUser: {0}", response.Item1)));
            }
        }