Skip to content

Commit

Permalink
Add experimental wopi api
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinderVosDeWael committed May 17, 2024
1 parent 8e7e8ba commit b9b5d67
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ POSTGRES_PORT=5432

AZURE_FUNCTION_PYTHON_URL=http://localhost:8080/api
AZURE_FUNCTION_PYTHON_KEY=fake_key

AZURE_BLOB_STORAGE_URL=fake_url
AZURE_BLOB_SAS=fake_sas
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"postcss-load-config": "^5.0.3",
"prettier": "^3.2.5",
"prettier-plugin-svelte": "^3.2.2",
"showdown": "^2.1.0",
"sortablejs": "^1.15.2",
"svelte": "^4.2.12",
"svelte-check": "^3.6.9",
Expand All @@ -58,8 +59,5 @@
"arrowParens": "avoid",
"tabWidth": 4,
"printWidth": 120
},
"dependencies": {
"showdown": "^2.1.0"
}
}
45 changes: 45 additions & 0 deletions src/lib/server/azure.ts
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())
}
}
15 changes: 15 additions & 0 deletions src/lib/server/secrets.ts
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
14 changes: 7 additions & 7 deletions src/lib/server/sql.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pkg from "pg"
const { Pool } = pkg
import { env } from "$env/dynamic/private"
import { POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_PORT } from "./secrets"

const { Pool } = pkg
const config = {
host: env.POSTGRES_HOST,
user: env.POSTGRES_USER,
password: env.POSTGRES_PASSWORD,
database: env.POSTGRES_DB,
port: Number(env.POSTGRES_PORT),
host: POSTGRES_HOST,
user: POSTGRES_USER,
password: POSTGRES_PASSWORD,
database: POSTGRES_DB,
port: Number(POSTGRES_PORT),
ssl: true
}

Expand Down
18 changes: 18 additions & 0 deletions src/routes/api/experimental/wopi/files/[name]/+server.ts
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 src/routes/api/experimental/wopi/files/[name]/contents/+server.ts
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 })
}
6 changes: 3 additions & 3 deletions src/routes/api/health/+server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { env } from "$env/dynamic/private"
import { logger } from "$lib/server/logging"
import { AZURE_FUNCTION_PYTHON_KEY, AZURE_FUNCTION_PYTHON_URL } from "$lib/server/secrets"

export async function GET() {
logger.info("Warming up the server.")
const headers = new Headers({ "x-functions-key": env.AZURE_FUNCTION_PYTHON_KEY || "" })
return await fetch(`${env.AZURE_FUNCTION_PYTHON_URL}/health`, { headers: headers })
const headers = new Headers({ "x-functions-key": AZURE_FUNCTION_PYTHON_KEY || "" })
return await fetch(`${AZURE_FUNCTION_PYTHON_URL}/health`, { headers: headers })
.then(async response => {
if (response.ok && response.body) {
return new Response(response.body)
Expand Down
6 changes: 3 additions & 3 deletions src/routes/api/intake-report/[id]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { env } from "$env/dynamic/private"
import { logger } from "$lib/server/logging"
import { AZURE_FUNCTION_PYTHON_KEY, AZURE_FUNCTION_PYTHON_URL } from "$lib/server/secrets"

export async function GET({ params, fetch }) {
const id = params.id
logger.info("Getting intake with id ", id)
const headers = new Headers({ "x-functions-key": env.AZURE_FUNCTION_PYTHON_KEY || "" })
return await fetch(`${env.AZURE_FUNCTION_PYTHON_URL}/intake-report/${id}`, { headers: headers })
const headers = new Headers({ "x-functions-key": AZURE_FUNCTION_PYTHON_KEY || "" })
return await fetch(`${AZURE_FUNCTION_PYTHON_URL}/intake-report/${id}`, { headers: headers })
.then(async response => {
if (response.ok && response.body) {
return new Response(response.body)
Expand Down
6 changes: 3 additions & 3 deletions src/routes/api/markdown2docx/+server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { env } from "$env/dynamic/private"
import { logger } from "$lib/server/logging"
import { AZURE_FUNCTION_PYTHON_KEY, AZURE_FUNCTION_PYTHON_URL } from "$lib/server/secrets"

export async function POST({ fetch, request }) {
logger.info("Converting markdown to docx")
const headers = new Headers({
"x-functions-key": env.AZURE_FUNCTION_PYTHON_KEY || "",
"x-functions-key": AZURE_FUNCTION_PYTHON_KEY || "",
"X-Correct-They": request.headers.get("x-correct-they") || "",
"X-Correct-Capitalization": request.headers.get("x-correct-capitalization") || ""
})
return await fetch(`${env.AZURE_FUNCTION_PYTHON_URL}/markdown2docx/`, {
return await fetch(`${AZURE_FUNCTION_PYTHON_URL}/markdown2docx/`, {
method: "POST",
headers: headers,
body: JSON.stringify({
Expand Down
11 changes: 11 additions & 0 deletions src/routes/experimental/diagnoses/+page.svelte
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>

0 comments on commit b9b5d67

Please sign in to comment.