-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e7e8ba
commit b9b5d67
Showing
12 changed files
with
138 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AZURE_BLOB_ACCOUNT_NAME, AZURE_BLOB_SAS } from "./secrets" | ||
import { logger } from "./logging" | ||
|
||
if (!AZURE_BLOB_ACCOUNT_NAME || !AZURE_BLOB_SAS) { | ||
logger.error("Azure environment variables not set") | ||
throw new Error("Azure environment variables not set") | ||
} | ||
|
||
export class AzureStorage { | ||
private urlTemplate | ||
|
||
constructor() { | ||
this.urlTemplate = `https://${AZURE_BLOB_ACCOUNT_NAME}.blob.core.windows.net/{container}/{blob}?${AZURE_BLOB_SAS}` | ||
} | ||
|
||
async putBlob(containerName: string, blobName: string, content: string | Buffer) { | ||
const url = this.urlTemplate.replace("{container}", containerName).replace("{blob}", blobName) | ||
const headers = new Headers({ | ||
"Content-Type": "application/octet-stream" | ||
}) | ||
const body = content instanceof Buffer ? content : Buffer.from(content) | ||
return await fetch(url, { | ||
method: "PUT", | ||
headers: headers, | ||
body: body | ||
}).then(response => { | ||
if (!response.ok) { | ||
throw new Error(`Failed to create blob: ${response.statusText}`) | ||
} | ||
}) | ||
} | ||
|
||
async readBlob(containerName: string, blobName: string) { | ||
const url = this.urlTemplate.replace("{container}", containerName).replace("{blob}", blobName) | ||
return await fetch(url) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`Failed to read blob: ${response.statusText}`) | ||
} | ||
return response | ||
}) | ||
.then(async response => await response.blob()) | ||
.then(blob => blob.arrayBuffer()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { env } from "$env/dynamic/private" | ||
|
||
export const POSTGRES_HOST = env.POSTGRES_HOST | ||
export const POSTGRES_USER = env.POSTGRES_USER | ||
export const POSTGRES_PASSWORD = env.POSTGRES_PASSWORD | ||
export const POSTGRES_DB = env.POSTGRES_DB | ||
export const POSTGRES_PORT = Number(env.POSTGRES_PORT) | ||
|
||
export const AZURE_FUNCTION_PYTHON_URL = env.AZURE_FUNCTION_PYTHON_URL | ||
export const AZURE_FUNCTION_PYTHON_KEY = env.AZURE_FUNCTION_PYTHON_KEY | ||
|
||
export const AZURE_BLOB_ACCOUNT_NAME = env.AZURE_BLOB_ACCOUNT_NAME | ||
export const AZURE_BLOB_SAS = env.AZURE_BLOB_SAS | ||
|
||
export const SHA_256_KEY = env.SHA_256_KEY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { logger } from "$lib/server/logging" | ||
import { AzureStorage } from "$lib/server/azure" | ||
|
||
export async function GET({ params }) { | ||
const name = decodeURIComponent(params.name) | ||
logger.info(`Retriving file ${name} from WOPI server.`) | ||
const response = await new AzureStorage().readBlob("templates", name) | ||
const filesize = response.byteLength | ||
return new Response( | ||
JSON.stringify({ | ||
BaseFileName: name, | ||
Size: filesize, | ||
OwnerId: 1000, | ||
UserId: 1000, | ||
UserCanWrite: true | ||
}) | ||
) | ||
} |
26 changes: 26 additions & 0 deletions
26
src/routes/api/experimental/wopi/files/[name]/contents/+server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { AzureStorage } from "$lib/server/azure" | ||
import { logger } from "$lib/server/logging" | ||
|
||
export async function GET({ params }) { | ||
const name = decodeURIComponent(params.name) | ||
logger.info(`Retrieving file ${name} from Azure.`) | ||
const file = await new AzureStorage().readBlob("templates", name) | ||
if (!file) { | ||
return new Response(null, { status: 404 }) | ||
} | ||
return new Response(file, { | ||
headers: { | ||
"Content-Type": "application/octet-stream", | ||
"Content-Disposition": `attachment; filename="${name}.docx"` | ||
} | ||
}) | ||
} | ||
|
||
export async function PUT({ params, request }) { | ||
const name = decodeURIComponent(params.name) | ||
logger.info(`Saving file ${name} to Azure.`) | ||
const file = Buffer.from(await request.arrayBuffer()) | ||
|
||
await new AzureStorage().putBlob("templates", name, file) | ||
return new Response(null, { status: 204 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script lang="ts"> | ||
const wopisrc: string = "http://localhost:3000/api/experimental/wopi/files/test3.docx?access_token=fake" | ||
const encodedWopisrc: string = encodeURIComponent(wopisrc) | ||
</script> | ||
|
||
<iframe | ||
title={"Editor"} | ||
src={`http://localhost:9980/browser/80a6f97/cool.html?WOPISrc=${encodedWopisrc}`} | ||
width="90%" | ||
height="1200px" | ||
></iframe> |