From a9bc8ef390c11bfd0547ac3bb07f30a44185d39f Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Wed, 16 Apr 2025 08:44:18 -0600 Subject: [PATCH] if image cannot be downloaded, fall back to oringinal url --- Dockerfile | 1 + build.sh | 2 +- docker-compose.yml | 2 +- src/services/canvas/canvasServiceUtils.ts | 4 +--- src/services/canvas/files/canvasFileService.ts | 9 +++++---- .../serverFunctions/router/canvasFileRouter.ts | 11 ++++++++--- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index badf4b5..1ee594a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ COPY . . RUN mkdir -p storage RUN rm -rf /app/storage/* +ENV NEXT_PUBLIC_ENABLE_FILE_SYNC=true RUN pnpm run build FROM node:22-alpine AS production diff --git a/build.sh b/build.sh index 7ab2dad..aa42297 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash MAJOR_VERSION="2" -MINOR_VERSION="6" +MINOR_VERSION="7" VERSION="$MAJOR_VERSION.$MINOR_VERSION" TAG_FLAG=false diff --git a/docker-compose.yml b/docker-compose.yml index 9a2b8e1..d8fafe7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: canvas_manager: - image: alexmickelson/canvas_management:2.6 + image: alexmickelson/canvas_management:2.7 user: "1000:1000" container_name: canvas-manager-2 ports: diff --git a/src/services/canvas/canvasServiceUtils.ts b/src/services/canvas/canvasServiceUtils.ts index 00ff59b..8aded91 100644 --- a/src/services/canvas/canvasServiceUtils.ts +++ b/src/services/canvas/canvasServiceUtils.ts @@ -39,7 +39,7 @@ export async function paginatedRequest(request: { var requestCount = 1; const url = new URL(request.url); url.searchParams.set("per_page", "100"); - + const { data: firstData, headers: firstHeaders } = await axiosClient.get( url.toString() ); @@ -64,5 +64,3 @@ export async function paginatedRequest(request: { return returnData as T; } - - diff --git a/src/services/canvas/files/canvasFileService.ts b/src/services/canvas/files/canvasFileService.ts index 2075ae9..2647dd6 100644 --- a/src/services/canvas/files/canvasFileService.ts +++ b/src/services/canvas/files/canvasFileService.ts @@ -7,7 +7,7 @@ import FormData from "form-data"; export const downloadUrlToTempDirectory = async ( sourceUrl: string -): Promise => { +): Promise<{fileName: string, success: boolean}> => { try { const fileName = path.basename(new URL(sourceUrl).pathname) || `tempfile-${Date.now()}`; @@ -16,10 +16,10 @@ export const downloadUrlToTempDirectory = async ( responseType: "arraybuffer", }); await fs.writeFile(tempFilePath, response.data); - return tempFilePath; + return {fileName: tempFilePath, success: true}; } catch (error) { - console.error("Error downloading or saving the file:", error); - throw error; + console.log("Error downloading or saving the file:", sourceUrl, error); + return {fileName: sourceUrl, success: false}; } }; @@ -93,6 +93,7 @@ export const uploadToCanvasPart2 = async ({ const redirectResponse = await axiosClient.get(redirectUrl); console.log("redirect response", redirectResponse.data); } + // console.log("returning from part 2", JSON.stringify(response.data)); return response.data.url; } catch (error) { console.error("Error uploading file to Canvas part 2:", error); diff --git a/src/services/serverFunctions/router/canvasFileRouter.ts b/src/services/serverFunctions/router/canvasFileRouter.ts index 2e7253d..9ba5c9c 100644 --- a/src/services/serverFunctions/router/canvasFileRouter.ts +++ b/src/services/serverFunctions/router/canvasFileRouter.ts @@ -18,9 +18,14 @@ export const canvasFileRouter = router({ }) ) .mutation(async ({ input: { sourceUrl, canvasCourseId } }) => { - const localFile = sourceUrl.startsWith("/") - ? fileStorageLocation + sourceUrl + const { fileName: localFile, success } = sourceUrl.startsWith("/") + ? { fileName: fileStorageLocation + sourceUrl, success: true } : await downloadUrlToTempDirectory(sourceUrl); + + if (!success) { + console.log("could not download file, returning sourceUrl", sourceUrl); + return sourceUrl; + } console.log("local temp file", localFile); const { upload_url, upload_params } = await uploadToCanvasPart1( localFile, @@ -32,7 +37,7 @@ export const canvasFileRouter = router({ upload_url, upload_params, }); - console.log("canvas url done"); + console.log("canvas url done", canvasUrl); return canvasUrl; }), });