-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File uploads + Experimental Media Server #31
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,5 @@ dev.db | |
|
||
.vscode | ||
|
||
# Media files (for uploads) | ||
media/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
All Django access should be encapsulated here. | ||
""" | ||
from django.db.models import Q | ||
|
||
from openedx_learning.core.components.models import ComponentVersionContent | ||
|
||
|
||
def get_content(package_identifier, component_identifier, version_num, asset_path): | ||
""" | ||
""" | ||
cv = ComponentVersionContent.objects.select_related( | ||
"content", | ||
"component_version", | ||
"component_version__component", | ||
"component_version__component__learning_package", | ||
).get( | ||
Q(component_version__component__learning_package__identifier=package_identifier) | ||
& Q(component_version__component__identifier=component_identifier) | ||
& Q(component_version__version_num=version_num) | ||
& Q(identifier=asset_path) | ||
) | ||
return cv.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import django | ||
import os | ||
|
||
|
||
def setup(): | ||
# Django initialization (must precede any Django-related imports): | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projects.dev") | ||
django.setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
|
||
""" | ||
from fastapi import FastAPI | ||
from fastapi.responses import FileResponse | ||
from starlette.responses import StreamingResponse | ||
|
||
from . import django_init | ||
from asgiref.sync import sync_to_async | ||
|
||
django_init.setup() | ||
|
||
from .asset_api import get_content | ||
|
||
# FastAPI init: | ||
app = FastAPI() | ||
|
||
|
||
# Example URL: /components/lp129/finalexam-problem14/1/static/images/finalexam-problem14_fig1.png | ||
@app.get( | ||
"/components/{package_identifier}/{component_identifier}/{version_num}/{asset_path:path}" | ||
) | ||
async def component_asset( | ||
package_identifier: str, | ||
component_identifier: str, | ||
version_num: int, | ||
asset_path: str, | ||
) -> StreamingResponse: | ||
content = await sync_to_async(get_content, thread_sensitive=True)( | ||
package_identifier, component_identifier, version_num, asset_path | ||
) | ||
path = content.file.path | ||
return FileResponse(path, media_type=content.mime_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
openedx_learning/core/components/migrations/0002_add_content_file.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 4.1 on 2023-02-11 21:18 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("components", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="content", | ||
name="file", | ||
field=models.FileField(null=True, upload_to=""), | ||
), | ||
migrations.AlterField( | ||
model_name="content", | ||
name="data", | ||
field=models.BinaryField(max_length=100000, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="content", | ||
name="size", | ||
field=models.PositiveBigIntegerField( | ||
validators=[django.core.validators.MaxValueValidator(100000)] | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
# | ||
# This file is autogenerated by pip-compile with python 3.8 | ||
# To update, run: | ||
# This file is autogenerated by pip-compile with Python 3.8 | ||
# by the following command: | ||
# | ||
# make upgrade | ||
# | ||
asgiref==3.5.2 | ||
asgiref==3.6.0 | ||
# via django | ||
attrs==22.1.0 | ||
attrs==22.2.0 | ||
# via -r requirements/base.in | ||
backports-zoneinfo==0.2.1 | ||
# via django | ||
django==4.1 | ||
django==4.1.6 | ||
# via | ||
# -r requirements/base.in | ||
# django-filter | ||
# djangorestframework | ||
django-filter==22.1 | ||
# via -r requirements/base.in | ||
djangorestframework==3.13.1 | ||
djangorestframework==3.14.0 | ||
# via -r requirements/base.in | ||
importlib-metadata==4.12.0 | ||
importlib-metadata==6.0.0 | ||
# via markdown | ||
markdown==3.4.1 | ||
# via -r requirements/base.in | ||
pytz==2022.2.1 | ||
pytz==2022.7.1 | ||
# via djangorestframework | ||
pyyaml==6.0 | ||
# via -r requirements/base.in | ||
sqlparse==0.4.2 | ||
sqlparse==0.4.3 | ||
# via django | ||
zipp==3.8.1 | ||
zipp==3.13.0 | ||
# via importlib-metadata |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this saving every Content's data to both MySQL and S3 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This code is really hacky. The only content that gets here are the static assets referenced from XBlocks, and they are stored only in storages/s3 (the
data=data_bytes
line was removed, so no db-level storage).There's a separate part where the XBlock's own OLX data is read in, and that part remains db-only for storage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I was reading this quickly and didn't catch that. Makes sense.