public async Task TestCreateCopy() { var fileRepository = new WopiFileRepository(); var fileName = Path.GetFileName(this.fileName); using (var fileStream = new FileStream(this.fileName, FileMode.Open)) { var originalFile = await fileRepository.AddFile(tenant1User1, tenant1, fileStream, fileName); // Test owner copy var ownerCopyResult = await fileRepository.CreateCopy(originalFile.FileId, tenant1User1, copyFileName); Assert.AreEqual(HttpStatusCode.OK, ownerCopyResult.Item1); Assert.IsNotNull(ownerCopyResult.Item2); // Test copy to an existing file without overwrite var result = await fileRepository.CreateCopy(originalFile.FileId, tenant1User1, copyFileName); Assert.AreEqual(HttpStatusCode.Conflict, result.Item1); // obtain access rights to original file for tenant1User2 in same tenant var result1 = await fileRepository.GetFileInfoByTenantUser(ownerCopyResult.Item2.FileId, tenant1User2, tenant1); Assert.AreEqual(HttpStatusCode.OK, result1.Item1); // Get lock for tenantUser1 var result2 = await fileRepository.LockFile(ownerCopyResult.Item2.FileId, tenant1User1, lockId, null); Assert.AreEqual(HttpStatusCode.OK, result2.Item1); // Test copy to an existing file by tenantUser2 when locked by tenant1User1 var result3 = await fileRepository.CreateCopy(ownerCopyResult.Item2.FileId, tenant1User2, copyFileName, true); Assert.AreEqual(HttpStatusCode.Conflict, result3.Item1); // release lock for tenant1User2 var result4 = await fileRepository.UnlockFile(ownerCopyResult.Item2.FileId, tenant1User1, lockId); Assert.AreEqual(HttpStatusCode.OK, result4.Item1); // Try copy again on existing file with overwrite var tenantCopyResult = await fileRepository.CreateCopy(ownerCopyResult.Item2.FileId, tenant1User2, copyFileName, true); Assert.AreEqual(HttpStatusCode.OK, tenantCopyResult.Item1); var deleteResponse = await fileRepository.DeleteFile(originalFile.FileId, tenant1User1); Assert.AreEqual(HttpStatusCode.OK, deleteResponse); deleteResponse = await fileRepository.DeleteFile(tenantCopyResult.Item2.FileId, tenant1User2); Assert.AreEqual(HttpStatusCode.OK, deleteResponse); } }
public override async Task <WopiResponse> PutRelativeFileSpecific(PutRelativeFileSpecificRequest putRelativeFileSpecificRequest) { var userId = WopiSecurity.GetIdentityNameFromToken(putRelativeFileSpecificRequest.AccessToken); var wopiFileRepository = new WopiFileRepository(); var response = await wopiFileRepository.CreateCopy(putRelativeFileSpecificRequest.ResourceId, userId, putRelativeFileSpecificRequest.RelativeTarget, putRelativeFileSpecificRequest.OverwriteRelativeTarget); if (response.Item1 == HttpStatusCode.NotFound) { return(putRelativeFileSpecificRequest.ResponseNotFound()); } else if (response.Item1 == HttpStatusCode.BadRequest) { return(putRelativeFileSpecificRequest.ResponseBadRequest()); } else if (response.Item1 == HttpStatusCode.Conflict) { return(putRelativeFileSpecificRequest.ResponseLockConflict(response.Item3)); } 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 url = new Uri(string.Format("https://{0}/wopi/files/{1}?access_token={2}", putRelativeFileSpecificRequest.RequestUri.Authority, response.Item2.FileId, tokenStr)); 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, putRelativeFileSpecificRequest.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, putRelativeFileSpecificRequest.RequestUri.Authority)); } return(putRelativeFileSpecificRequest.ResponseOK(response.Item2.FileName, url, hostViewUrl, hostEditUrl)); } else { return(putRelativeFileSpecificRequest.ResponseServerError(string.Format("Unknown HTTPStatusCode from WopiFileRepository.CreateCopy: {0}", response.Item1))); } }