Skip to content

Commit a53289d

Browse files
author
Konduru Keerthi Konduru Ravichandra Raju
committed
Google Cloud Storage
1 parent 7fc7940 commit a53289d

File tree

156 files changed

+149958
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+149958
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34928.147
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-Word-document", "Open-Word-document\Open-Word-document.csproj", "{E732C351-53B8-4986-971E-50D074486ACB}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E732C351-53B8-4986-971E-50D074486ACB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E732C351-53B8-4986-971E-50D074486ACB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E732C351-53B8-4986-971E-50D074486ACB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E732C351-53B8-4986-971E-50D074486ACB}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {518AB72D-48F2-48F0-998F-73D48821A204}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Cloud.Storage.V1;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Open_Word_document.Models;
5+
using Syncfusion.DocIO.DLS;
6+
using System.Diagnostics;
7+
8+
namespace Open_Word_document.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public IActionResult Index()
20+
{
21+
return View();
22+
}
23+
public async Task<IActionResult> EditDocument()
24+
{
25+
//Your bucket name
26+
string bucketName = "Your_bucket_name";
27+
28+
//Your service account key path
29+
string keyPath = "Your_service_account_key_path";
30+
31+
//Name of the file to download from the Google Cloud Storage
32+
string fileName = "WordTemplate.docx";
33+
34+
//Download the file from Google
35+
MemoryStream memoryStream = await GetDocumentFromGoogle(bucketName, keyPath, fileName);
36+
37+
//Create an instance of WordDocument
38+
using (WordDocument wordDocument = new WordDocument(memoryStream, Syncfusion.DocIO.FormatType.Docx))
39+
{
40+
//Access the section in a Word document
41+
IWSection section = wordDocument.Sections[0];
42+
43+
//Add new paragraph to the section
44+
IWParagraph paragraph = section.AddParagraph();
45+
paragraph.ParagraphFormat.FirstLineIndent = 36;
46+
paragraph.BreakCharacterFormat.FontSize = 12f;
47+
48+
//Add new text to the paragraph
49+
IWTextRange textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as IWTextRange;
50+
textRange.CharacterFormat.FontSize = 12f;
51+
52+
//Saving the Word document to a MemoryStream
53+
MemoryStream outputStream = new MemoryStream();
54+
wordDocument.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);
55+
56+
//Download the Word file in the browser
57+
FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/msword");
58+
fileStreamResult.FileDownloadName = "EditWord.docx";
59+
return fileStreamResult;
60+
}
61+
}
62+
/// <summary>
63+
/// Download file from Google
64+
/// </summary>
65+
/// <param name="bucketName"></param>
66+
/// <param name="keyPath"></param>
67+
/// <param name="fileName"></param>
68+
/// <returns></returns>
69+
public async Task<MemoryStream> GetDocumentFromGoogle(string bucketName, string keyPath, string fileName)
70+
{
71+
try
72+
{
73+
//Create Google Credential from the service account key file
74+
GoogleCredential credential = GoogleCredential.FromFile(keyPath);
75+
76+
//Instantiates a storage client to interact with Google Cloud Storage
77+
StorageClient storageClient = StorageClient.Create(credential);
78+
79+
//Download a file from Google Cloud Storage
80+
using (MemoryStream memoryStream = new MemoryStream())
81+
{
82+
await storageClient.DownloadObjectAsync(bucketName, fileName, memoryStream);
83+
memoryStream.Position = 0;
84+
85+
return memoryStream;
86+
}
87+
}
88+
catch (Exception ex)
89+
{
90+
Console.WriteLine($"Error retrieving document from Google Cloud Storage: {ex.Message}");
91+
throw; // or handle the exception as needed
92+
}
93+
}
94+
public IActionResult Privacy()
95+
{
96+
return View();
97+
}
98+
99+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
100+
public IActionResult Error()
101+
{
102+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
103+
}
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Open_Word_document.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>Open_Word_document</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Google.Cloud.Storage.V1" Version="4.10.0" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.1.40" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllersWithViews();
5+
6+
var app = builder.Build();
7+
8+
// Configure the HTTP request pipeline.
9+
if (!app.Environment.IsDevelopment())
10+
{
11+
app.UseExceptionHandler("/Home/Error");
12+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
13+
app.UseHsts();
14+
}
15+
16+
app.UseHttpsRedirection();
17+
app.UseStaticFiles();
18+
19+
app.UseRouting();
20+
21+
app.UseAuthorization();
22+
23+
app.MapControllerRoute(
24+
name: "default",
25+
pattern: "{controller=Home}/{action=Index}/{id?}");
26+
27+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:2283",
8+
"sslPort": 44366
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5022",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7087;http://localhost:5022",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
@{
6+
Html.BeginForm("EditDocument", "Home", FormMethod.Get);
7+
{
8+
<div>
9+
<input type="submit" value="Edit Document" style="width:150px;height:27px" />
10+
</div>
11+
}
12+
Html.EndForm();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - Open_Word_document</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/Open_Word_document.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Open_Word_document</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
</ul>
29+
</div>
30+
</div>
31+
</nav>
32+
</header>
33+
<div class="container">
34+
<main role="main" class="pb-3">
35+
@RenderBody()
36+
</main>
37+
</div>
38+
39+
<footer class="border-top footer text-muted">
40+
<div class="container">
41+
&copy; 2024 - Open_Word_document - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
42+
</div>
43+
</footer>
44+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
45+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
46+
<script src="~/js/site.js" asp-append-version="true"></script>
47+
@await RenderSectionAsync("Scripts", required: false)
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)