Skip to content

Commit

Permalink
feat: add file size length to returned json (openedx#33068)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki authored and mehedikhan72 committed Aug 24, 2023
1 parent 57f88d2 commit ba02714
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cms/djangoapps/contentstore/asset_storage_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def _get_assets_in_json_format(assets, course_key):
for asset in assets:
thumbnail_asset_key = _get_thumbnail_asset_key(asset, course_key)
asset_is_locked = asset.get('locked', False)
asset_file_size = asset.get('length', None)

asset_in_json = get_asset_json(
asset['displayname'],
Expand All @@ -361,6 +362,7 @@ def _get_assets_in_json_format(assets, course_key):
thumbnail_asset_key,
asset_is_locked,
course_key,
asset_file_size,
)

assets_in_json_format.append(asset_in_json)
Expand Down Expand Up @@ -426,6 +428,7 @@ def _upload_asset(request, course_key):
# readback the saved content - we need the database timestamp
readback = contentstore().find(content.location)
locked = getattr(content, 'locked', False)
length = getattr(content, 'length', None)
return JsonResponse({
'asset': get_asset_json(
content.name,
Expand All @@ -435,6 +438,7 @@ def _upload_asset(request, course_key):
content.thumbnail_location,
locked,
course_key,
length,
),
'msg': _('Upload completed')
})
Expand Down Expand Up @@ -491,12 +495,13 @@ def _get_file_too_large_error_message(filename):
def _get_file_content_and_path(file_metadata, course_key):
"""returns contents of the uploaded file and path for temporary uploaded file"""
content_location = StaticContent.compute_location(course_key, file_metadata['filename'])
upload_file_size = str(file_metadata['upload_file_size'])
upload_file = file_metadata['upload_file']

file_can_be_chunked = upload_file.multiple_chunks()

static_content_partial = partial(StaticContent, content_location, file_metadata['filename'],
file_metadata['mime_type'])
file_metadata['mime_type'], length=upload_file_size)

if file_can_be_chunked:
content = static_content_partial(upload_file.chunks())
Expand Down Expand Up @@ -599,7 +604,7 @@ def _delete_thumbnail(thumbnail_location, course_key, asset_key): # lint-amnest
logging.warning('Could not delete thumbnail: %s', thumbnail_location)


def get_asset_json(display_name, content_type, date, location, thumbnail_location, locked, course_key):
def get_asset_json(display_name, content_type, date, location, thumbnail_location, locked, course_key, file_size=None):
'''
Helper method for formatting the asset information to send to client.
'''
Expand All @@ -617,5 +622,6 @@ def get_asset_json(display_name, content_type, date, location, thumbnail_locatio
'locked': locked,
'static_full_url': StaticContent.get_canonicalized_asset_path(course_key, portable_url, '', []),
# needed for Backbone delete/update.
'id': str(location)
'id': str(location),
'file_size': file_size,
}

0 comments on commit ba02714

Please sign in to comment.