Skip to content

Commit

Permalink
Feat(p-gslide): Rework thumbnail generation, download & upload pdf, s…
Browse files Browse the repository at this point in the history
…et media dependency
  • Loading branch information
Vija02 committed Feb 19, 2025
1 parent 1645282 commit e154ac1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
3 changes: 3 additions & 0 deletions plugins/google-slides/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@r2wc/react-to-web-component": "^2.0.3",
"@repo/base-plugin": "*",
"@repo/lib": "*",
"@repo/observability": "*",
"@repo/ui": "*",
"@tanstack/react-query": "^5.62.3",
"@trpc/client": "^11.0.0-rc.657",
Expand All @@ -32,6 +33,7 @@
"axios": "^1.7.9",
"framer-motion": "^11.13.1",
"http-proxy-middleware": "^3.0.3",
"node-pdftocairo": "^1.2.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-icons": "^5.4.0",
Expand All @@ -45,6 +47,7 @@
"@repo/prettier-config": "*",
"@repo/typescript-config": "*",
"@vitejs/plugin-react-swc": "^3.7.2",
"concurrently": "^9.1.2",
"eslint": "^8.57.1",
"pkgroll": "^2.5.1",
"vite": "5.4.11"
Expand Down
66 changes: 46 additions & 20 deletions plugins/google-slides/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
ServerPluginApi,
TRPCObject,
} from "@repo/base-plugin/server";
import { logger } from "@repo/observability";
import axios from "axios";
import { createProxyMiddleware } from "http-proxy-middleware";
import { input } from "node-pdftocairo";
import { typeidUnboxed } from "typeid-js";
import { proxy } from "valtio";
import { bind } from "valtio-yjs";
Expand Down Expand Up @@ -214,6 +216,8 @@ const getAppRouter = (serverPluginApi: ServerPluginApi) => (t: TRPCObject) => {
)
.mutation(
async ({ input: { pluginId, presentationId, token }, ctx }) => {
const log = logger.child({ pluginId, presentationId });

// TODO: Validation
const loadedPlugin = loadedPlugins[pluginId]!;
const loadedContextData = loadedContext[pluginId]!;
Expand All @@ -240,7 +244,9 @@ const getAppRouter = (serverPluginApi: ServerPluginApi) => (t: TRPCObject) => {
if (loadedPlugin.pluginData.thumbnailLinks.length > 0) {
await Promise.all(
loadedPlugin.pluginData.thumbnailLinks.map((mediaId) =>
serverPluginApi.deleteMedia(mediaId),
serverPluginApi.deleteMedia(mediaId).catch((err) => {
log.error({ err }, "Failed to delete media");
}),
),
);
}
Expand All @@ -249,38 +255,58 @@ const getAppRouter = (serverPluginApi: ServerPluginApi) => (t: TRPCObject) => {
loadedPlugin.pluginData.fetchId = typeidUnboxed("fetch");
loadedPlugin.pluginData.presentationId = presentationId;
loadedPlugin.pluginData.pageIds = pageIds;
loadedPlugin.pluginData.thumbnailLinks = [];
loadedPlugin.pluginData.thumbnailLinks = new Array(
pageIds.length,
).fill("");
loadedPlugin.pluginData.html = processHtml(htmlData.data);
});

log.info("Downloading PDF");
const pdfRes = await axios(
`https://docs.google.com/feeds/download/presentations/Export?id=${presentationId}&exportFormat=pdf`,
{
headers: { Authorization: `Bearer ${token}` },
responseType: "arraybuffer",
},
);
const pdfBuffer = Buffer.from(pdfRes.data);
const uploadPdfPromise = serverPluginApi.uploadMedia(
pdfBuffer,
"pdf",
{
organizationId: loadedContextData.organizationId,
userId: ctx.userId,
},
);

log.info("Downloaded. Size: " + pdfBuffer.length);

log.info("Converting...");
const output = await input(pdfBuffer, {
format: "jpeg",
}).output();
log.info("Convert done, uploading...");

const uploadedPdf = await uploadPdfPromise;

// DEBT: Make this runnable somewhere else
// The problem is, if that's the case then we'll need to store the token
// TODO: Problem if we run this again while still running
for (const pageId of pageIds) {
const thumbnailDataRes = await axios(
`https://slides.googleapis.com/v1/presentations/${presentationId}/pages/${pageId}/thumbnail?thumbnailProperties.thumbnailSize=SMALL`,
{
headers: { Authorization: `Bearer ${token}` },
},
);

const picture = await axios(thumbnailDataRes.data.contentUrl, {
responseType: "arraybuffer",
});

output.forEach(async (img, i) => {
const uploadedMedia = await serverPluginApi.uploadMedia(
picture.data,
"png",
img,
"jpg",
{
organizationId: loadedContextData.organizationId,
userId: ctx.userId,
parentMediaIdOrUUID: uploadedPdf.mediaId,
},
);
loadedPlugin.pluginData.thumbnailLinks[i] =
uploadedMedia.fileName;
});

loadedPlugin.pluginData.thumbnailLinks.push(
uploadedMedia.fileName,
);
}
log.info("Uploading done");

return {};
},
Expand Down

0 comments on commit e154ac1

Please sign in to comment.