more refactor

This commit is contained in:
2025-07-23 09:55:30 -06:00
parent 1885431574
commit aa15b2b335
19 changed files with 48 additions and 54 deletions

View File

@@ -0,0 +1,44 @@
import publicProcedure from "../publicProcedure";
import { z } from "zod";
import { router } from "../trpcSetup";
import {
downloadUrlToTempDirectory,
uploadToCanvasPart1,
uploadToCanvasPart2,
} from "@/services/canvas/files/canvasFileService";
const fileStorageLocation = process.env.FILE_STORAGE_LOCATION ?? "/app/public";
export const canvasFileRouter = router({
getCanvasFileUrl: publicProcedure
.input(
z.object({
sourceUrl: z.string(),
canvasCourseId: z.number(),
})
)
.mutation(async ({ input: { sourceUrl, canvasCourseId } }) => {
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);
// make a toast or some other way of notifying the user
return sourceUrl;
}
console.log("local temp file", localFile);
const { upload_url, upload_params } = await uploadToCanvasPart1(
localFile,
canvasCourseId
);
console.log("part 1 done", upload_url, upload_params);
const canvasUrl = await uploadToCanvasPart2({
pathToUpload: localFile,
upload_url,
upload_params,
});
console.log("canvas url done", canvasUrl);
return canvasUrl;
}),
});