Skip to content

Commit

Permalink
testing streaming directly from bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
enapupe committed Nov 5, 2023
1 parent eddb78b commit 4a4112b
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
with:
service: "media-server"
image: "vnguyen/openbeta-media-server"
env_vars: |
APP_VERSION="{{ GITHUB_SHA }}
- name: "Use output"
run: 'curl "${{ steps.deploy.outputs.url }}"'
1 change: 1 addition & 0 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
image: "vnguyen/openbeta-media-server:staging"
env_vars: |
STORAGE_BUCKET=openbeta-staging
APP_VERSION="{{ GITHUB_SHA }}
- name: "Use output"
run: 'curl "${{ steps.deploy.outputs.url }}"'
40 changes: 25 additions & 15 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
const express = require("express");
const sharp = require("sharp");
const { Storage, ApiError } = require("@google-cloud/storage");

const storage = new Storage();
const app = express();

const PORT = 8080;
const HOST = "0.0.0.0";
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com/";
const BUCKET = process.env.STORAGE_BUCKET || "openbeta-prod";

const getImage = (path) =>
fetch(path).then(async (r) => ({
data: await r.arrayBuffer(),
status: r.status,
}));
const getFormat = (webp, avif) => {
return webp ? "webp" : avif ? "avif" : "jpeg";
};
Expand All @@ -22,9 +18,13 @@ app.get("/healthy", (req, res) => {
res.send("yep.");
});

app.get("/version", (req, res) => {
res.send(proces.env.APP_VERSION);
});

app.get("*", async (req, res) => {
try {
const { searchParams, pathname, href } = new URL(
const { searchParams, pathname } = new URL(
`${BASE_STORAGE_IMAGE_URL}${BUCKET}${req.url}`,
);

Expand All @@ -39,19 +39,29 @@ app.get("*", async (req, res) => {
const height = Number(searchParams.get("h")) || undefined;
const format = getFormat(webp, avif);

const { data, status } = await getImage(href);
if (status > 399) {
return res
.status(415)
.send("upstream server did not respond with a valid status code");
}
const fileBucketName = pathname.replace(BUCKET, "").slice(2);

res
.set("Cache-Control", "public, max-age=15552000")
.set("Vary", "Accept")
.type(`image/${format}`);

sharp(data)
const pipeline = sharp();

storage
.bucket(BUCKET)
.file(fileBucketName)
.createReadStream()
.on("error", function (e) {
if (e instanceof ApiError) {
if (e.message?.includes("No such object"))
return res.status(404).end();
}
return res.status(500).send(JSON.stringify(e));
})
.pipe(pipeline);

pipeline
.rotate()
.resize({ width, height })
.toFormat(format, { effort: 3, quality, progressive: true })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"author": "openbeta.io",
"license": "MIT",
"dependencies": {
"@google-cloud/storage": "^7.5.0",
"express": "^4.18.2",
"node-fetch": "^3.3.2",
"sharp": "^0.32.6"
},
"devDependencies": {
Expand Down

0 comments on commit 4a4112b

Please sign in to comment.