Skip to content

A path can be both a file and a directory (e.g. a and a/b) in Asset and ComponentVersionMedia #954

Open

Description

@kdmccormick

Note

This issue was authored by Claude (Fable 5.2) and has not yet been closely reviewed by @kdmccormick.

Found while reviewing #904 (the new Asset model). Nothing stops one learning package from holding a file at a path and another file "underneath" that same path, for example a and a/b. This applies to the new Asset.path and to the existing ComponentVersionMedia.path.

Why it matters

Paths are plain strings in a table, so the database, the serving code and the in-process backup/restore all handle this without complaint. It only breaks when the files have to exist on a real filesystem, where a cannot be both a file and a directory.

Observed with one component version holding static/a and static/a/b:

Step Result
Create the component version Works
Create a backup zip with create_zip_file Works. The archive has a file entry .../static/a, a folder entry .../static/a/ and a file entry .../static/a/b (zipper.py:385).
Restore it with load_learning_package Works; both paths come back. Restore reads entries straight from the archive.
Extract the zip with unzip Fails: ".../static/a exists but is not directory"

So the places that would be affected are: anyone extracting a backup zip, a course export that writes Assets into a static/ directory, and any tooling that mirrors files to a filesystem.

This is more pressing for Asset than for component media, because an Asset's path is its permanent identity and cannot be renamed, and because the export format for Assets has not been designed yet. Assets migrated from contentstore cannot arrive with this problem, since contentstore names are flat; it can only come from new uploads.

Suggested approach for Asset

The rule. A new path P conflicts if an existing asset sits at one of P's ancestor paths (for a/b/c: a and a/b), or if an existing asset's path starts with P/. Both lookups can use the existing unique index on learning package and path.

Which existing assets count. Only those that currently have a draft or a published version.

  • Placeholder assets (no versions) should not count. They are created automatically when content references a /static/ name that does not exist, so a mistyped reference to /static/images would otherwise block every upload under images/, with no way for an author to remove it.
  • Fully deleted assets should not count, so that deleting a and publishing the deletion frees a/b.
  • Counting "draft or published", rather than only "draft", keeps the published set consistent when the deletion of a has not been published yet.

When to check. When an asset becomes real, not when its row is created: on creating its first version, and on reviving a deleted asset by uploading to the same path. Both go through create_asset_and_version. The failure would be a ValidationError subclass, like AssetAlreadyExists.

Concurrency. Two simultaneous uploads of a and a/b could both pass the check. A database constraint cannot express this rule directly; enforcing it that way would need a side table in which each asset claims its own path and every ancestor folder. Given that the consequence is an archive that will not extract cleanly rather than data loss, the suggestion is to do the plain check inside the creating transaction, document the gap, and have the exporter detect the conflict and fail with a clear message.

Suggested approach for ComponentVersionMedia

A component version receives all of its paths at once (api.py:336-338 is where paths are checked today), so this can be checked in memory with no query and no race. Existing data would need auditing before rejecting it.

Out of scope

Activity

  1. bradenmacdonald commented on Nov 20, 2026

    @bradenmacdonald
    Contributor

    I think this situation is unlikely to arise very often in practice, because most files uploaded by authors are going to have file extensions, and most folder names are not.

    So I suggest we don't worry about it at the path/Asset level (after all, it doesn't cause problems there), and just throw an exception if trying to create a backup file with component names that conflict in this way.

    For ComponentVersionMedia, it makes sense to do a check as you suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions