From a60008c6d747985170c3d56479c4a9004864ecf6 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 24 Jan 2025 09:20:07 -0700 Subject: [PATCH] file support in progress --- README.md | 12 ++++ next.config.mjs | 3 +- run.sh | 3 + src/hooks/localCourse/assignmentHooks.ts | 92 +++++++++++++----------- src/websocket.js | 2 + 5 files changed, 69 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 6056cdc..b459235 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,18 @@ Development command: `dotnet watch --project Management.Web/` +## Getting Started and Usage + + + + +### Enable Image Support + + +You must set the `ENABLE_FILE_SYNC` environment variable to true. Image paths will be relative to the `/app/image_storage` directory in the container. + +When an image is detected by canvas manager, it will upload the image to the canvas course and keep a lookup table of the original path/url of the image to the canvas course URL. + # ideas matching questions diff --git a/next.config.mjs b/next.config.mjs index 81aa05b..3f18c2a 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,5 +1,6 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {}; +const nextConfig = { +}; export default nextConfig; diff --git a/run.sh b/run.sh index a7e8d03..ec59290 100755 --- a/run.sh +++ b/run.sh @@ -4,6 +4,7 @@ docker run -it --rm \ --name canvas-manager-2 \ -e TZ=America/Denver \ -e NODE_ENV=development \ + -e NEXT_PUBLIC_ENABLE_FILE_SYNC=true \ -u 1000:1000 \ -p 3000:3000 \ -w /app \ @@ -12,8 +13,10 @@ docker run -it --rm \ -v ~/projects/faculty/4850_AdvancedFE/2024-fall-alex/modules:/app/storage/advanced_frontend \ -v ~/projects/faculty/1810/2025-spring-alex/online:/app/storage/intro_to_web_online \ -v ~/projects/faculty/1400/2025_spring_alex/modules:/app/storage/1400 \ + -v ~/projects/faculty/1405/2025_spring_alex:/app/storage/1405 \ -v ~/projects/faculty/3840_Telemetry/2025_spring_alex/modules:/app/storage/telemetry \ -v ~/projects/faculty/4620_Distributed/2025Spring/modules:/app/storage/distributed \ + -v ~/projects/facultyFiles:/app/public/images/facultyFiles \ node \ sh -c " mkdir -p ~/.npm-global && \ diff --git a/src/hooks/localCourse/assignmentHooks.ts b/src/hooks/localCourse/assignmentHooks.ts index 4fbd447..b787f34 100644 --- a/src/hooks/localCourse/assignmentHooks.ts +++ b/src/hooks/localCourse/assignmentHooks.ts @@ -23,6 +23,8 @@ export const useAssignmentQuery = ( }); }; +const enable_images = process.env.NEXT_PUBLIC_ENABLE_FILE_SYNC === "true"; + export const useUpdateImageSettingsForAssignment = ({ moduleName, assignmentName, @@ -37,50 +39,56 @@ export const useUpdateImageSettingsForAssignment = ({ const createCanvasUrlMutation = trpc.canvasFile.getCanvasFileUrl.useMutation(); - // useEffect(() => { - // if (isUpdatingSettings) { - // console.log("not updating image assets, still loading"); - // return; - // } - // setIsUpdatingSettings(true); - // const assignmentMarkdown = markdownToHtmlNoImages(assignment.description); + useEffect(() => { + if (!enable_images) { + console.log("not uploading images, FILE_POLLING is not set to true"); + return; + } - // const imageSources = extractImageSources(assignmentMarkdown); - // const imagesToUpdate = imageSources.filter((source) => - // settings.assets.every((a) => a.sourceUrl !== source) - // ); - // console.log("images to update", imagesToUpdate); + if (isUpdatingSettings) { + console.log("not updating image assets, still loading"); + return; + } + setIsUpdatingSettings(true); + const assignmentMarkdown = markdownToHtmlNoImages(assignment.description); - // if (imagesToUpdate.length) { - // Promise.all( - // imagesToUpdate.map(async (source) => { - // // todo: get canvas url - // // const canvasUrl = ""; - // const canvasUrl = await createCanvasUrlMutation.mutateAsync({ - // sourceUrl: source, - // canvasCourseId: settings.canvasId, - // }); - // console.log("got canvas url", source, canvasUrl); - // return { sourceUrl: source, canvasUrl }; - // }) - // ).then(async (newAssets) => { - // await updateSettings.mutateAsync({ - // settings: { - // ...settings, - // assets: [...settings.assets, ...newAssets], - // }, - // }); - // setIsUpdatingSettings(false); - // }); - // } - // }, [ - // assignment.description, - // createCanvasUrlMutation, - // isUpdatingSettings, - // settings, - // settings.assets, - // updateSettings, - // ]); + const imageSources = extractImageSources(assignmentMarkdown); + const imagesToUpdate = imageSources.filter((source) => + settings.assets.every((a) => a.sourceUrl !== source) + ); + console.log("images to update", imagesToUpdate); + + if (imagesToUpdate.length) { + Promise.all( + imagesToUpdate.map(async (source) => { + // todo: get canvas url + // const canvasUrl = ""; + console.log(source); + const canvasUrl = await createCanvasUrlMutation.mutateAsync({ + sourceUrl: source, + canvasCourseId: settings.canvasId, + }); + console.log("got canvas url", source, canvasUrl); + return { sourceUrl: source, canvasUrl }; + }) + ).then(async (newAssets) => { + await updateSettings.mutateAsync({ + settings: { + ...settings, + assets: [...settings.assets, ...newAssets], + }, + }); + setIsUpdatingSettings(false); + }); + } + }, [ + assignment.description, + createCanvasUrlMutation, + isUpdatingSettings, + settings, + settings.assets, + updateSettings, + ]); }; export const useAssignmentNamesQuery = (moduleName: string) => { diff --git a/src/websocket.js b/src/websocket.js index 06af285..90ebad7 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -17,7 +17,9 @@ const handler = app.getRequestHandler(); const folderToWatch = path.join(process.cwd(), "./storage", "/"); console.log("watching folder", folderToWatch); const usePolling = process.env.FILE_POLLING === "true"; +const enable_images = process.env.NEXT_PUBLIC_ENABLE_FILE_SYNC === "true"; console.log("FILE_POLLING:", usePolling); +console.log("NEXT_PUBLIC_ENABLE_FILE_SYNC:", enable_images); const watcher = chokidar.watch(folderToWatch, { persistent: true,