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))); } }
public override async Task <WopiResponse> PutRelativeFileSuggested(PutRelativeFileSuggestedRequest putRelativeFileSuggestedRequest) { WopiResponse wopiResponse = null; var file = DocumentDBRepository <DetailedFileModel> .GetItem("Files", i => i.id.ToString() == putRelativeFileSuggestedRequest.ResourceId); // Check for null file if (file != null) { var inputStream = await putRelativeFileSuggestedRequest.Content.ReadAsStreamAsync(); // Suggested mode...might just be an extension var fileName = putRelativeFileSuggestedRequest.SuggestedTarget; if (fileName.IndexOf('.') == 0) { fileName = file.BaseFileName.Substring(0, file.BaseFileName.LastIndexOf('.')) + fileName; } // Create the file entity FileModel newFile = new FileModel() { id = Guid.NewGuid(), OwnerId = file.OwnerId, BaseFileName = fileName, Size = inputStream.Length, Container = file.Container, Version = 1 }; // First stream the file into blob storage var bytes = new byte[inputStream.Length]; await inputStream.ReadAsync(bytes, 0, (int)inputStream.Length); var id = await Utils.AzureStorageUtil.UploadFile(newFile.id.ToString(), newFile.Container, bytes); // Write the details into documentDB await DocumentDBRepository <FileModel> .CreateItemAsync("Files", (FileModel)newFile); // Get access token for the new file WopiSecurity security = new WopiSecurity(); var token = security.GenerateToken(newFile.OwnerId, newFile.Container, newFile.id.ToString()); var tokenStr = security.WriteToken(token); // Prepare the Json response var name = newFile.BaseFileName; var url = string.Format("https://{0}/wopi/files/{1}?access_token={2}", putRelativeFileSuggestedRequest.RequestUri.Authority, newFile.id.ToString(), tokenStr); // Add the optional properties to response if applicable (HostViewUrl, HostEditUrl) string hostViewUrl = null; string hostEditUrl = null; var actions = await WopiDiscovery.GetActions(); var fileExt = newFile.BaseFileName.Substring(newFile.BaseFileName.LastIndexOf('.') + 1).ToLower(); var view = actions.FirstOrDefault(i => i.ext == fileExt && i.name == "view"); if (view != null) { hostViewUrl = WopiDiscovery.GetActionUrl(view, newFile.id.ToString(), putRelativeFileSuggestedRequest.RequestUri.Authority); } var edit = actions.FirstOrDefault(i => i.ext == fileExt && i.name == "edit"); if (edit != null) { hostEditUrl = WopiDiscovery.GetActionUrl(edit, newFile.id.ToString(), putRelativeFileSuggestedRequest.RequestUri.Authority); } // Write the response and return a success 200 wopiResponse = putRelativeFileSuggestedRequest.ResponseOK(fileName, new Uri(url), new Uri(hostViewUrl), new Uri(hostEditUrl)); } else { wopiResponse = putRelativeFileSuggestedRequest.ResponseNotFound(); } return(wopiResponse); }