public async Task TestCreateCopySuggested()
        {
            var fileRepository = new WopiFileRepository();
            var fileName       = Path.GetFileName(this.fileName);

            using (var fileStream = new FileStream(this.fileName, FileMode.Open))
            {
                var wopiFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName);

                // Test suggested file extension
                var result1 = await fileRepository.CreateCopySuggested(wopiFile.FileId, tenant1User1, suggestedFileExtension);

                Assert.AreEqual(HttpStatusCode.OK, result1.Item1);
                Assert.IsNotNull(result1.Item2);

                // test full file name no conflict
                var result2 = await fileRepository.CreateCopySuggested(wopiFile.FileId, tenant1User1, copyFileName);

                Assert.AreEqual(HttpStatusCode.OK, result2.Item1);

                // test full file name conflict
                var result3 = await fileRepository.CreateCopySuggested(wopiFile.FileId, tenant1User1, copyFileName);

                Assert.AreEqual(HttpStatusCode.OK, result3.Item1);


                // test second full file name conflict
                var result4 = await fileRepository.CreateCopySuggested(wopiFile.FileId, tenant1User1, copyFileName);

                Assert.AreEqual(HttpStatusCode.OK, result4.Item1);

                var deleteResponse = await fileRepository.DeleteFile(wopiFile.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                deleteResponse = await fileRepository.DeleteFile(result1.Item2.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                deleteResponse = await fileRepository.DeleteFile(result2.Item2.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                deleteResponse = await fileRepository.DeleteFile(result3.Item2.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                deleteResponse = await fileRepository.DeleteFile(result4.Item2.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
            }
        }
Пример #2
0
        public override async Task <WopiResponse> PutRelativeFileSuggested(PutRelativeFileSuggestedRequest putRelativeFileSuggestedRequest)
        {
            var userId = WopiSecurity.GetIdentityNameFromToken(putRelativeFileSuggestedRequest.AccessToken);

            var wopiFileRepository = new WopiFileRepository();
            var response           = await wopiFileRepository.CreateCopySuggested(putRelativeFileSuggestedRequest.ResourceId, userId, putRelativeFileSuggestedRequest.SuggestedTarget);

            if (response.Item1 == HttpStatusCode.NotFound)
            {
                return(putRelativeFileSuggestedRequest.ResponseNotFound());
            }
            else if (response.Item1 == HttpStatusCode.OK)
            {
                // Get access token for the new file
                WopiSecurity security = new WopiSecurity();
                var          token    = security.GenerateToken(response.Item2.OwnerId);
                var          tokenStr = security.WriteToken(token);

                //var name = newFile.BaseFileName;

                var url = new Uri(string.Format("https://{0}/wopi/files/{1}?access_token={2}",
                                                putRelativeFileSuggestedRequest.RequestUri.Authority, response.Item2.FileId, tokenStr));

                // Add the optional properties to response if applicable (HostViewUrl, HostEditUrl)
                Uri hostViewUrl = null;
                Uri hostEditUrl = null;
                var actions     = await WopiDiscovery.GetActions();

                var view = actions.FirstOrDefault(i => i.ext == response.Item2.FileExtension && i.name == "view");
                if (view != null)
                {
                    hostViewUrl = new Uri(WopiDiscovery.GetActionUrl(view, response.Item2.FileId, putRelativeFileSuggestedRequest.RequestUri.Authority));
                }
                var edit = actions.FirstOrDefault(i => i.ext == response.Item2.FileExtension && i.name == "edit");
                if (edit != null)
                {
                    hostEditUrl = new Uri(WopiDiscovery.GetActionUrl(edit, response.Item2.FileId, putRelativeFileSuggestedRequest.RequestUri.Authority));
                }
                // Write the response and return a success 200
                return(putRelativeFileSuggestedRequest.ResponseOK(response.Item2.FileName, url, hostViewUrl, hostEditUrl));
            }
            else
            {
                return(putRelativeFileSuggestedRequest.ResponseServerError(string.Format("Unknown HTTPStatusCode from WopiFileRepository.CreateCopySuggested: {0}", response.Item1)));
            }
        }