Chore: Remove Ontology file size limit. Code duplications (#1880)

<!-- .github/pull_request_template.md -->

## Description
We received a complaint about the 10MB file size limit. 
Removed code duplications
More strict types
<!--
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->

## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [ ] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Support for supplying optional per-file descriptions when uploading
multiple ontologies.

* **Improvements**
* Removed the 10MB file size limit for ontology uploads, allowing larger
files.
* Streamlined and more robust upload handling with improved per-file
validation and safer upload behavior.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Pavel Zorin 2025-12-11 10:49:55 +01:00 committed by GitHub
commit fe7e97be45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,7 @@ from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Optional, List from typing import Optional, List
from dataclasses import dataclass from dataclasses import dataclass
from fastapi import UploadFile
@dataclass @dataclass
@ -45,8 +46,10 @@ class OntologyService:
json.dump(metadata, f, indent=2) json.dump(metadata, f, indent=2)
async def upload_ontology( async def upload_ontology(
self, ontology_key: str, file, user, description: Optional[str] = None self, ontology_key: str, file: UploadFile, user, description: Optional[str] = None
) -> OntologyMetadata: ) -> OntologyMetadata:
if not file.filename:
raise ValueError("File must have a filename")
if not file.filename.lower().endswith(".owl"): if not file.filename.lower().endswith(".owl"):
raise ValueError("File must be in .owl format") raise ValueError("File must be in .owl format")
@ -57,8 +60,6 @@ class OntologyService:
raise ValueError(f"Ontology key '{ontology_key}' already exists") raise ValueError(f"Ontology key '{ontology_key}' already exists")
content = await file.read() content = await file.read()
if len(content) > 10 * 1024 * 1024:
raise ValueError("File size exceeds 10MB limit")
file_path = user_dir / f"{ontology_key}.owl" file_path = user_dir / f"{ontology_key}.owl"
with open(file_path, "wb") as f: with open(file_path, "wb") as f:
@ -82,7 +83,11 @@ class OntologyService:
) )
async def upload_ontologies( async def upload_ontologies(
self, ontology_key: List[str], files: List, user, descriptions: Optional[List[str]] = None self,
ontology_key: List[str],
files: List[UploadFile],
user,
descriptions: Optional[List[str]] = None,
) -> List[OntologyMetadata]: ) -> List[OntologyMetadata]:
""" """
Upload ontology files with their respective keys. Upload ontology files with their respective keys.
@ -105,47 +110,17 @@ class OntologyService:
if len(set(ontology_key)) != len(ontology_key): if len(set(ontology_key)) != len(ontology_key):
raise ValueError("Duplicate ontology keys not allowed") raise ValueError("Duplicate ontology keys not allowed")
if descriptions and len(descriptions) != len(files):
raise ValueError("Number of descriptions must match number of files")
results = [] results = []
user_dir = self._get_user_dir(str(user.id))
metadata = self._load_metadata(user_dir)
for i, (key, file) in enumerate(zip(ontology_key, files)): for i, (key, file) in enumerate(zip(ontology_key, files)):
if key in metadata:
raise ValueError(f"Ontology key '{key}' already exists")
if not file.filename.lower().endswith(".owl"):
raise ValueError(f"File '{file.filename}' must be in .owl format")
content = await file.read()
if len(content) > 10 * 1024 * 1024:
raise ValueError(f"File '{file.filename}' exceeds 10MB limit")
file_path = user_dir / f"{key}.owl"
with open(file_path, "wb") as f:
f.write(content)
ontology_metadata = {
"filename": file.filename,
"size_bytes": len(content),
"uploaded_at": datetime.now(timezone.utc).isoformat(),
"description": descriptions[i] if descriptions else None,
}
metadata[key] = ontology_metadata
results.append( results.append(
OntologyMetadata( await self.upload_ontology(
ontology_key=key, ontology_key=key,
filename=file.filename, file=file,
size_bytes=len(content), user=user,
uploaded_at=ontology_metadata["uploaded_at"],
description=descriptions[i] if descriptions else None, description=descriptions[i] if descriptions else None,
) )
) )
self._save_metadata(user_dir, metadata)
return results return results
def get_ontology_contents(self, ontology_key: List[str], user) -> List[str]: def get_ontology_contents(self, ontology_key: List[str], user) -> List[str]: