kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Content import downloads files that already exist in secondary storage locations

Open jamalexbot opened this issue 7 months ago • 3 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Target branch: release-v0.19.x

Observed behavior

When a secondary storage location is configured (via "Other storage locations" in device settings) that already contains content files, importing content still downloads files from the remote source instead of recognizing they already exist on disk in the secondary location.

Expected behavior

Content import should detect that files already exist in configured secondary storage locations and skip downloading them, just as it skips files that already exist in the primary storage location.

Root cause

In ResourceImportManagerBase._start_file_transfer() (kolibri/core/content/utils/resource_import.py), the destination path is resolved via:

dest = paths.get_content_storage_file_path(
    filename, contentfolder=self.content_dir
)

Because contentfolder is not None, get_content_storage_file_path (kolibri/core/content/utils/paths.py) always returns the primary path without checking fallback directories:

if (
    os.path.exists(primary_path)
    or datafolder is not None
    or contentfolder is not None  # <-- this prevents fallback check
):
    return primary_path

The file doesn't exist at the primary path, so the subsequent existence check fails and Kolibri proceeds to download.

Note: the annotation code path (_check_file_availability in annotation.py) calls get_content_storage_file_path without contentfolder, so it correctly finds files in fallback dirs. The bug is only in the import transfer path.

Steps to reproduce

  1. Set up a Kolibri instance with content already downloaded (the "source" instance).
  2. Set up a second Kolibri instance and configure the source instance's content directory as a secondary storage location via Device > Other storage locations.
  3. In the second instance, import a channel whose content files exist in the secondary location.
  4. Observe that Kolibri downloads files from the internet instead of skipping them.

User-facing consequences

Users who configure secondary storage locations to share content across Kolibri instances (or to avoid re-downloading content) find that content is downloaded anyway, wasting bandwidth and time.

Proposed fix

In _start_file_transfer, after the existing primary-path existence check, add a fallback check that inspects the configured fallback directories directly via existing_file_path_in_content_fallback_dirs. This keeps the destination path unchanged (still inside self.content_dir) while treating the fallback copy as sufficient to skip the transfer:

def _start_file_transfer(self, f):
    filename = get_content_file_name(f)
    dest = paths.get_content_storage_file_path(
        filename, contentfolder=self.content_dir
    )

    # if the file already exists at the destination, skip
    if os.path.isfile(dest) and os.path.getsize(dest) == f["file_size"]:
        return

    # also skip if a matching-size copy already exists in a configured fallback dir
    suffix = os.path.join("storage", filename[0], filename[1], filename)
    fallback_path = paths.existing_file_path_in_content_fallback_dirs(suffix)
    if fallback_path and os.path.getsize(fallback_path) == f["file_size"]:
        return

    filetransfer = self.create_file_transfer(f, filename, dest)
    if filetransfer:
        with filetransfer:
            filetransfer.run()

Notes on the fix

  • Size-mismatch handling: the fallback check must compare os.path.getsize(fallback_path) == f["file_size"], matching the existing primary-path check, so partial or corrupt files in a fallback location still trigger a re-download.
  • Why existing_file_path_in_content_fallback_dirs rather than get_content_storage_file_path(filename): the latter re-resolves the primary path and re-checks it before considering fallbacks, duplicating the work the preceding os.path.isfile(dest) check has already done. Calling existing_file_path_in_content_fallback_dirs directly inspects only the fallback list and reads plainly at the call site.
  • Custom content_dir edge case: when a caller passes a non-default content_dir (e.g. importing to an external drive), fallback dirs still belong to the running instance's configuration. If a fallback contains the file, skip the download — this matches how _check_file_availability in annotation.py already treats availability instance-wide, independent of where a specific import would have written the file.

Acceptance Criteria

  • [ ] ResourceImportManagerBase._start_file_transfer skips the download when the file exists in any path returned by get_content_fallback_paths() with a matching file size.
  • [ ] Existing skip behaviour for files already in the primary content directory is preserved (no regression).
  • [ ] Files with a size mismatch (partial/corrupt) are re-downloaded, whether the mismatched copy lives in primary or fallback storage.
  • [ ] Unit test added in kolibri/core/content/test/ covering: file present in a fallback dir → skip; file present in primary → skip; file absent everywhere → download proceeds; wrong-size file in primary or fallback → download proceeds.
  • [ ] Manual verification per the reproduction steps: a second Kolibri instance configured with a fallback dir pointing at another instance's content directory completes channel import without network downloads, and the imported resources appear as available in Learn.

AI usage

I used Claude to help trace through the codebase to identify the root cause and draft this issue. I reviewed and edited the content for accuracy.

jamalexbot avatar Apr 22 '26 00:04 jamalexbot

I've locally tested applying the proposed fix, and it does seem to resolve the issue -- content imports are now fast and don't re-download the content.

jamalex avatar Apr 22 '26 00:04 jamalex

Plan: Fix ResourceImportManagerBase._start_file_transfer to skip downloading a content file when a copy with the correct size already exists in a configured secondary ("fallback") storage directory.

  • [ ] Write tests for the fallback-skip behaviour
  • [ ] Implement the fix in _start_file_transfer
  • [ ] Run tests, lint, and commit

status


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Apr 22 '26 01:04 rtibblesbot

Fixed in #14630

rtibbles avatar Apr 28 '26 15:04 rtibbles