restructuring file storage service

This commit is contained in:
2024-09-20 11:34:47 -06:00
parent 6e0526ee08
commit 6fdba7506f
13 changed files with 276 additions and 253 deletions

View File

@@ -24,7 +24,7 @@ export default async function RootLayout({
<html lang="en">
<head></head>
<body className="flex justify-center">
<div className="bg-slate-900 h-screen p-1 text-slate-300 w-full">
<div className="bg-slate-900 h-screen text-slate-300 w-full p-1">
<MyToaster />
<Suspense>
<Providers>

View File

@@ -155,6 +155,7 @@ function OtherSettings({
label={"Storage Folder"}
options={emptyDirectories}
getOptionName={(d) => d}
emptyOptionText="--- add a new folder to your docker compose to add more folders ---"
/>
<div>
New folders will not be created automatically, you are expected to mount

View File

@@ -3,7 +3,7 @@ import AddNewCourse from "./newCourse/AddNewCourse";
export default async function Home() {
return (
<main className="min-h-screen flex justify-center">
<main className="min-h-0 flex justify-center">
<div>
<CourseList />
<br />

View File

@@ -4,12 +4,14 @@ export default function SelectInput<T>({
label,
options,
getOptionName,
emptyOptionText,
}: {
value: T | undefined;
setValue: (newValue: T | undefined) => void;
label: string;
options: T[];
getOptionName: (item: T) => string;
emptyOptionText?: string;
}) {
return (
<label className="block">
@@ -25,6 +27,7 @@ export default function SelectInput<T>({
}}
>
<option></option>
{emptyOptionText && <option>{emptyOptionText}</option>}
{options.map((o) => (
<option key={getOptionName(o)}>{getOptionName(o)}</option>
))}

View File

@@ -17,13 +17,14 @@ export const useCanvasTermsQuery = (queryDate: Date) => {
return useSuspenseQuery({
queryKey: canvasKeys.allAroundDate(queryDate),
queryFn: () => {
const currentTerms = terms
.filter((t) => {
const finiteTerms = terms.filter((t) => {
if (!t.end_at) return false;
const endDate = new Date(t.end_at);
return endDate > queryDate;
})
});
console.log("finite terms", finiteTerms, terms);
const currentTerms = finiteTerms
.sort(
(a, b) =>
new Date(a.start_at ?? "").getTime() -

View File

@@ -1,24 +1,25 @@
import { CanvasEnrollmentTermModel } from "@/models/canvas/enrollmentTerms/canvasEnrollmentTermModel";
import { canvasApi } from "./canvasServiceUtils";
import { canvasApi, paginatedRequest } from "./canvasServiceUtils";
import { CanvasCourseModel } from "@/models/canvas/courses/canvasCourseModel";
import { CanvasEnrollmentModel } from "@/models/canvas/enrollments/canvasEnrollmentModel";
import { axiosClient } from "../axiosUtils";
const getAllTerms = async () => {
const url = `${canvasApi}/accounts/10/terms`;
const { data } = await axiosClient.get<{
const url = `${canvasApi}/accounts/10/terms?per_page=100`;
const data = await paginatedRequest<
{
enrollment_terms: CanvasEnrollmentTermModel[];
}>(url);
const terms = data.enrollment_terms;
}[]
>({ url });
const terms = data.flatMap((t) => t.enrollment_terms);
return terms;
};
export const canvasService = {
getAllTerms,
async getCourses(termId: number) {
const url = `${canvasApi}/courses`;
const response = await axiosClient.get<CanvasCourseModel[]>(url);
const allCourses = response.data;
const url = `${canvasApi}/courses?per_page=100`;
const allCourses = await paginatedRequest<CanvasCourseModel[]>({ url });
const coursesInTerm = allCourses
.flatMap((l) => l)
.filter((c) => c.enrollment_term_id === termId);

View File

@@ -44,19 +44,14 @@ export async function paginatedRequest<T extends any[]>(request: {
url.toString()
);
// if (!Array.isArray(firstData)) {
// return firstData;
// }
var returnData = [...firstData];
var returnData = Array.isArray(firstData) ? [...firstData] : [firstData]; // terms come across as nested objects {enrolmentTerms: terms[]}
var nextUrl = getNextUrl(firstHeaders);
// console.log("got first request", nextUrl, firstHeaders);
while (nextUrl) {
requestCount += 1;
const { data, headers } = await axiosClient.get<T>(nextUrl);
if (data) {
returnData = returnData.concat(data);
returnData = returnData.concat(Array.isArray(data) ? [...data] : [data]);
}
nextUrl = getNextUrl(headers);
}

View File

@@ -1,230 +1,19 @@
import { promises as fs } from "fs";
import path from "path";
import {
LocalCourseSettings,
localCourseYamlUtils,
} from "@/models/local/localCourse";
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
import {
LocalQuiz,
localQuizMarkdownUtils,
} from "@/models/local/quiz/localQuiz";
import {
LocalCoursePage,
localPageMarkdownUtils,
} from "@/models/local/page/localCoursePage";
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
import { assignmentsFileStorageService } from "./assignmentsFileStorageService";
const quizzes = {
async getQuizNames(courseName: string, moduleName: string) {
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, quiz folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
},
async getQuiz(courseName: string, moduleName: string, quizName: string) {
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
/\r\n/g,
"\n"
);
return localQuizMarkdownUtils.parseMarkdown(rawFile);
},
async updateQuiz(
courseName: string,
moduleName: string,
quizName: string,
quiz: LocalQuiz
) {
const folder = path.join(basePath, courseName, moduleName, "quizzes");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const quizMarkdown = quizMarkdownUtils.toMarkdown(quiz);
console.log(`Saving quiz ${filePath}`);
await fs.writeFile(filePath, quizMarkdown);
},
};
const pages = {
async getPageNames(courseName: string, moduleName: string) {
const filePath = path.join(basePath, courseName, moduleName, "pages");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, pages folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
},
async getPage(courseName: string, moduleName: string, pageName: string) {
const filePath = path.join(
basePath,
courseName,
moduleName,
"pages",
pageName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
/\r\n/g,
"\n"
);
return localPageMarkdownUtils.parseMarkdown(rawFile);
},
async updatePage(
courseName: string,
moduleName: string,
pageName: string,
page: LocalCoursePage
) {
const folder = path.join(basePath, courseName, moduleName, "pages");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
basePath,
courseName,
moduleName,
"pages",
page.name + ".md"
);
const pageMarkdown = localPageMarkdownUtils.toMarkdown(page);
console.log(`Saving page ${filePath}`);
await fs.writeFile(filePath, pageMarkdown);
const pageNameIsChanged = pageName !== page.name;
if (pageNameIsChanged) {
console.log("removing old page after name change " + pageName);
const oldFilePath = path.join(
basePath,
courseName,
moduleName,
"pages",
pageName + ".md"
);
await fs.unlink(oldFilePath);
}
},
};
const modules = {
async getModuleNames(courseName: string) {
const courseDirectory = path.join(basePath, courseName);
const moduleDirectories = await fs.readdir(courseDirectory, {
withFileTypes: true,
});
const modulePromises = moduleDirectories
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const modules = await Promise.all(modulePromises);
return modules.sort((a, b) => a.localeCompare(b));
},
async createModule(courseName: string, moduleName: string) {
const courseDirectory = path.join(basePath, courseName);
await fs.mkdir(courseDirectory + "/" + moduleName, { recursive: true });
},
};
const settings = {
async getAllCoursesSettings() {
const courses = await fileStorageService.getCourseNames();
const courseSettings = await Promise.all(
courses.map(
async (c) => await fileStorageService.settings.getCourseSettings(c)
)
);
return courseSettings;
},
async getCourseSettings(courseName: string): Promise<LocalCourseSettings> {
const courseDirectory = path.join(basePath, courseName);
const settingsPath = path.join(courseDirectory, "settings.yml");
if (!(await directoryOrFileExists(settingsPath))) {
const errorMessage = `Error loading settings for ${courseName}, settings file ${settingsPath}`;
console.log(errorMessage);
throw new Error(errorMessage);
}
const settingsString = await fs.readFile(settingsPath, "utf-8");
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
const folderName = path.basename(courseDirectory);
return { ...settings, name: folderName };
},
async updateCourseSettings(
courseName: string,
settings: LocalCourseSettings
) {
const courseDirectory = path.join(basePath, courseName);
const settingsPath = path.join(courseDirectory, "settings.yml");
const { name, ...settingsWithoutName } = settings;
const settingsMarkdown =
localCourseYamlUtils.settingsToYaml(settingsWithoutName);
console.log(`Saving settings ${settingsPath}`);
await fs.writeFile(settingsPath, settingsMarkdown);
},
};
import { quizFileStorageService } from "./quizFileStorageService";
import { pageFileStorageService } from "./pageFileStorageService";
import { moduleFileStorageService } from "./moduleFileStorageService";
import { settingsFileStorageService } from "./settingsFileStorageService";
export const fileStorageService = {
async getCourseNames() {
console.log("loading course ids");
const courseDirectories = await fs.readdir(basePath, {
withFileTypes: true,
});
const coursePromises = await Promise.all(
courseDirectories
.filter((dirent) => dirent.isDirectory())
.map(async (dirent) => {
const coursePath = path.join(basePath, dirent.name);
const settingsPath = path.join(coursePath, "settings.yml");
const hasSettings = await directoryOrFileExists(settingsPath);
return {
dirent,
hasSettings,
};
})
);
const courseNamesFromDirectories = coursePromises
.filter(({ hasSettings }) => hasSettings)
.map(({ dirent }) => dirent.name);
return courseNamesFromDirectories;
},
settings,
modules,
settings: settingsFileStorageService,
modules: moduleFileStorageService,
assignments: assignmentsFileStorageService,
quizzes,
pages,
quizzes: quizFileStorageService,
pages: pageFileStorageService,
async getEmptyDirectories(): Promise<string[]> {
if (!(await directoryOrFileExists(basePath))) {

View File

@@ -0,0 +1,24 @@
import { promises as fs } from "fs";
import path from "path";
import { basePath } from "./utils/fileSystemUtils";
export const moduleFileStorageService = {
async getModuleNames(courseName: string) {
const courseDirectory = path.join(basePath, courseName);
const moduleDirectories = await fs.readdir(courseDirectory, {
withFileTypes: true,
});
const modulePromises = moduleDirectories
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const modules = await Promise.all(modulePromises);
return modules.sort((a, b) => a.localeCompare(b));
},
async createModule(courseName: string, moduleName: string) {
const courseDirectory = path.join(basePath, courseName);
await fs.mkdir(courseDirectory + "/" + moduleName, { recursive: true });
},
};

View File

@@ -0,0 +1,68 @@
import { localPageMarkdownUtils, LocalCoursePage } from "@/models/local/page/localCoursePage";
import { promises as fs } from "fs";
import path from "path";
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
export const pageFileStorageService = {
async getPageNames(courseName: string, moduleName: string) {
const filePath = path.join(basePath, courseName, moduleName, "pages");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, pages folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
},
async getPage(courseName: string, moduleName: string, pageName: string) {
const filePath = path.join(
basePath,
courseName,
moduleName,
"pages",
pageName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
/\r\n/g,
"\n"
);
return localPageMarkdownUtils.parseMarkdown(rawFile);
},
async updatePage(
courseName: string,
moduleName: string,
pageName: string,
page: LocalCoursePage
) {
const folder = path.join(basePath, courseName, moduleName, "pages");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
basePath,
courseName,
moduleName,
"pages",
page.name + ".md"
);
const pageMarkdown = localPageMarkdownUtils.toMarkdown(page);
console.log(`Saving page ${filePath}`);
await fs.writeFile(filePath, pageMarkdown);
const pageNameIsChanged = pageName !== page.name;
if (pageNameIsChanged) {
console.log("removing old page after name change " + pageName);
const oldFilePath = path.join(
basePath,
courseName,
moduleName,
"pages",
pageName + ".md"
);
await fs.unlink(oldFilePath);
}
},
};

View File

@@ -0,0 +1,58 @@
import {
localQuizMarkdownUtils,
LocalQuiz,
} from "@/models/local/quiz/localQuiz";
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
import path from "path";
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
import { promises as fs } from "fs";
export const quizFileStorageService = {
async getQuizNames(courseName: string, moduleName: string) {
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, quiz folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
},
async getQuiz(courseName: string, moduleName: string, quizName: string) {
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
/\r\n/g,
"\n"
);
return localQuizMarkdownUtils.parseMarkdown(rawFile);
},
async updateQuiz(
courseName: string,
moduleName: string,
quizName: string,
quiz: LocalQuiz
) {
const folder = path.join(basePath, courseName, moduleName, "quizzes");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const quizMarkdown = quizMarkdownUtils.toMarkdown(quiz);
console.log(`Saving quiz ${filePath}`);
await fs.writeFile(filePath, quizMarkdown);
},
};

View File

@@ -0,0 +1,56 @@
import {
LocalCourseSettings,
localCourseYamlUtils,
} from "@/models/local/localCourse";
import { promises as fs } from "fs";
import path from "path";
import {
basePath,
directoryOrFileExists,
getCourseNames,
} from "./utils/fileSystemUtils";
const getCourseSettings = async (
courseName: string
): Promise<LocalCourseSettings> => {
const courseDirectory = path.join(basePath, courseName);
const settingsPath = path.join(courseDirectory, "settings.yml");
if (!(await directoryOrFileExists(settingsPath))) {
const errorMessage = `Error loading settings for ${courseName}, settings file ${settingsPath}`;
console.log(errorMessage);
throw new Error(errorMessage);
}
const settingsString = await fs.readFile(settingsPath, "utf-8");
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
const folderName = path.basename(courseDirectory);
return { ...settings, name: folderName };
};
export const settingsFileStorageService = {
getCourseSettings,
async getAllCoursesSettings() {
const courses = await getCourseNames();
const courseSettings = await Promise.all(
courses.map(async (c) => await getCourseSettings(c))
);
return courseSettings;
},
async updateCourseSettings(
courseName: string,
settings: LocalCourseSettings
) {
const courseDirectory = path.join(basePath, courseName);
const settingsPath = path.join(courseDirectory, "settings.yml");
const { name, ...settingsWithoutName } = settings;
const settingsMarkdown =
localCourseYamlUtils.settingsToYaml(settingsWithoutName);
console.log(`Saving settings ${settingsPath}`);
await fs.writeFile(settingsPath, settingsMarkdown);
},
};

View File

@@ -1,4 +1,5 @@
import { promises as fs } from "fs";
import path from "path";
export const hasFileSystemEntries = async (
directoryPath: string
@@ -19,6 +20,32 @@ export const directoryOrFileExists = async (directoryPath: string): Promise<bool
}
};
export async function getCourseNames() {
console.log("loading course ids");
const courseDirectories = await fs.readdir(basePath, {
withFileTypes: true,
});
const coursePromises = await Promise.all(
courseDirectories
.filter((dirent) => dirent.isDirectory())
.map(async (dirent) => {
const coursePath = path.join(basePath, dirent.name);
const settingsPath = path.join(coursePath, "settings.yml");
const hasSettings = await directoryOrFileExists(settingsPath);
return {
dirent,
hasSettings,
};
})
);
const courseNamesFromDirectories = coursePromises
.filter(({ hasSettings }) => hasSettings)
.map(({ dirent }) => dirent.name);
return courseNamesFromDirectories;
}
export const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";