public override async Task <WopiResponse> CheckFileInfo(CheckFileInfoRequest checkFileInfoRequest) { // Lookup the file in the database var itemId = new Guid(checkFileInfoRequest.ResourceId); var wopiFile = DocumentDBRepository <DetailedFileModel> .GetItem("Files", file => file.id == itemId); // Check for null file if (wopiFile != null) { // Get discovery information var fileExt = wopiFile.BaseFileName.Substring(wopiFile.BaseFileName.LastIndexOf('.') + 1).ToLower(); var actions = await WopiDiscovery.GetActions(); // Augments the file with additional properties CloseUrl, HostViewUrl, HostEditUrl wopiFile.CloseUrl = String.Format("https://{0}", checkFileInfoRequest.RequestUri.Authority); var view = actions.FirstOrDefault(i => i.ext == fileExt && i.name == "view"); if (view != null) { wopiFile.HostViewUrl = WopiDiscovery.GetActionUrl(view, wopiFile.id.ToString(), checkFileInfoRequest.RequestUri.Authority); } var edit = actions.FirstOrDefault(i => i.ext == fileExt && i.name == "edit"); if (edit != null) { wopiFile.HostEditUrl = WopiDiscovery.GetActionUrl(edit, wopiFile.id.ToString(), checkFileInfoRequest.RequestUri.Authority); } // Get the user from the token (token is already validated) wopiFile.UserId = WopiSecurity.GetUserFromToken(checkFileInfoRequest.AccessToken); // Write the response and return a success 200 var wopiResponse = checkFileInfoRequest.ResponseOK(wopiFile.BaseFileName, wopiFile.OwnerId, wopiFile.Size, wopiFile.UserId, wopiFile.Version.ToString()); // Add optional items wopiResponse.CloseUrl = new Uri(wopiFile.CloseUrl); if (wopiFile.HostViewUrl != null) { wopiResponse.HostViewUrl = new Uri(wopiFile.HostViewUrl); } if (wopiFile.HostEditUrl != null) { wopiResponse.HostEditUrl = new Uri(wopiFile.HostEditUrl); } return(wopiResponse); } else { return(checkFileInfoRequest.ResponseNotFound()); } }
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))); } }