Exemplo n.º 1
0
 def get(self, request, usage_key_str, file_path):
     """
     Get a static asset file belonging to this block.
     """
     key = LibraryUsageLocatorV2.from_string(usage_key_str)
     api.require_permission_for_library_key(key.lib_key, request.user, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY)
     files = api.get_library_block_static_asset_files(key)
     for f in files:
         if f.path == file_path:
             return Response(LibraryXBlockStaticFileSerializer(f).data)
     raise NotFound
Exemplo n.º 2
0
 def put(self, request, usage_key_str, file_path):
     """
     Replace a static asset file belonging to this block.
     """
     usage_key = LibraryUsageLocatorV2.from_string(usage_key_str)
     api.require_permission_for_library_key(
         usage_key.lib_key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
     )
     file_wrapper = request.data['content']
     if file_wrapper.size > 20 * 1024 * 1024:  # > 20 MiB
         # In the future, we need a way to use file_wrapper.chunks() to read
         # the file in chunks and stream that to Blockstore, but Blockstore
         # currently lacks an API for streaming file uploads.
         raise ValidationError("File too big")
     file_content = file_wrapper.read()
     try:
         result = api.add_library_block_static_asset_file(usage_key, file_path, file_content)
     except ValueError:
         raise ValidationError("Invalid file path")
     return Response(LibraryXBlockStaticFileSerializer(result).data)