Skip to content

File Creation

Asher Dale edited this page Nov 25, 2021 · 9 revisions

Because ClassTranscribe has a large amount of files, we are working on organizing the files into a structure that will prevent too many files from existing in a single directory. To do so, files will be located in a particular subdirectory based on the Course and CourseOffering that a file is related to.

When a Course or CourseOffering is created, a new string called FilePath will be computed in the following manner: yymm-xxxx, where yy is the two digit year, mm is the two digit month, and xxxx are four random alphanumeric characters. This field will dictate the subdirectory that related files are placed into. The Course's FilePath will be the parent directory and the CourseOffering's FilePath will be a child directory. To prevent extra lookups, we will include the Course FilePath in the CourseOffering FilePath. These FilePath values will be stored in the database with their respective entities, so they will only need to be computed once, upon creation of the entity. The following is an example:

  1. A Course object is created and is assigned the FilePath value of 2104-yF3d.
  2. A CourseOffering object is created for the above course. The computed FilePath for the CourseOffering is 2104-yF3d/2105-Kpl9 (which includes the parent course's FilePath value).
  3. A video file is uploaded for the above course offering, and it is stored in the directory 2104-yF3d/2105-Kpl9 on the filesystem.

When the FilePath values are computed during Course and CourseOffering creation, we will create the directories on the filesystem at this time so we will not have to check for existence of the directories at later times. Additionally, this will allow us to check for any collisions in the directory names.

Creating a file in the backend is a three step process:

  1. Call the GetTmpFile function in CommonUtils.cs to get the name of a new temp file that you can create.
  2. Write the contents of the desired file to the new temp file.
  3. Call the GetNewFileRecordAsync function in FileRecord.cs, which will move the temp file to a new file that will be placed in a subdirectory based on the Course and CourseOffering that it is related to. A valid CourseOffering must be passed in to this function. You can use the CommonUtils.GetRelatedCourseOffering helper method to find a given entity's related CourseOffering.

If a new file is provided with the contents already in it (such as when a user uploads a video or an image), then only step 3 is required.

Currently, all existing Courses and CourseOfferings do not have the computed FilePath fields, so I will include a new endpoint in the Admin Controller (/api/Admin/GenerateFilePaths) for computing these values for all existing Courses and CourseOfferings that currently do not have a FilePath set.

Clone this wiki locally