public async Task TestAddFileGetFileDeleteFile()
        {
            var fileRepository = new WopiFileRepository();

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

                var wopiFile2 = await fileRepository.GetFileInfo(wopiFile1.FileId, tenant1User1);

                Assert.IsNotNull(wopiFile2);
                Assert.AreEqual(wopiFile1.FileId, wopiFile2.FileId);
                Assert.IsTrue(wopiFile2.FilePermissions.Count == 1);

                var contentResponse = await fileRepository.GetFileContent(wopiFile1.FileId, tenant1User1);

                Assert.AreEqual(HttpStatusCode.OK, contentResponse.Item1);
                using (var stream = contentResponse.Item2)
                {
                    Assert.IsNotNull(stream);
                    Assert.IsTrue(stream.Length > 0);
                }

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

                Assert.AreEqual(HttpStatusCode.OK, deleteResponse);
                using (var wopiContext = new WopiContext())
                {
                    var file = await wopiContext.Files.Where(f => f.FileId == wopiFile1.FileId).FirstOrDefaultAsync();

                    Assert.IsNull(file);
                    var filePermission = await wopiContext.FilePermissions.Where(f => f.FileId == wopiFile1.FileId).FirstOrDefaultAsync();

                    Assert.IsNull(filePermission);

                    contentResponse = await fileRepository.GetFileContent(wopiFile1.FileId, wopiFile1.Container);

                    Assert.AreEqual(HttpStatusCode.NotFound, contentResponse.Item1);
                    Assert.IsNull(contentResponse.Item2);
                }
            }
        }
Пример #2
0
        public override async Task <WopiResponse> GetFile(GetFileRequest getFileRequest)
        {
            var userId = WopiSecurity.GetIdentityNameFromToken(getFileRequest.AccessToken);

            // Lookup the file in the database
            var wopiFileRepository = new WopiFileRepository();
            var response           = await wopiFileRepository.GetFileContent(getFileRequest.ResourceId, userId);

            // Check for null file
            if (response.Item1 == HttpStatusCode.NotFound)
            {
                return(getFileRequest.ResponseNotFound());
            }
            else
            {
                // Write the response and return success 200
                return(getFileRequest.ResponseOK(new StreamContent(response.Item2), response.Item3));
            }
        }