def api_media_post(auth_user=None, api_core=None, request=None): u""" Register a media asset and add informations about it. This method only register already uploaded media asset to the shared storage. For example, the WebUI will upload a media asset to uploads path **before** registering it with this method. Medias in the shared storage are renamed with the following convention: ``storage_root``/medias/``user_id``/``media_id`` When published or downloaded, media asset file-name will be ``filename``. Spaces ( ) are not allowed and they will be converted to underscores (_). Media asset's ``metadata`` must contain any valid JSON string. Only the ``title`` key is required. The orchestrator will automatically add ``add_date`` and ``duration`` to ``metadata``. .. note:: Registration of external media assets (aka. http://) will be an interesting improvement. """ data = get_request_data(request, qs_only_first_value=True) media = Media(user_id=auth_user._id, uri=data[u'uri'], filename=data[u'filename'], metadata=data[u'metadata'], status=Media.READY) api_core.save_media(media) return ok_200(media, include_properties=True)
def api_media_id_patch(id=None, auth_user=None, api_core=None, request=None): u"""Update the informations of a media asset (only metadata field can be updated).""" media = api_core.get_media(spec={u'_id': id}) data = get_request_data(request, qs_only_first_value=True) if not media: raise IndexError(to_bytes(u'No media asset with id {0}.'.format(id))) if auth_user._id != media.user_id: flask.abort(403, u'You are not allowed to modify media asset with id {0}.'.format(id)) if u'metadata' in data: media.metadata = data[u'metadata'] api_core.save_media(media) return ok_200(u'The media asset "{0}" has been updated.'.format(media.filename), include_properties=False)
def upload_media(request): u"""Upload a media asset.""" try: auth_user = request.args.get(u'ebuio_u_pk') or request.form.get(u'ebuio_u_pk') # FIXME use temporary filename generator from python standard library ? random_temp_name = (u''.join(random.choice(string.digits + string.ascii_uppercase) for x in range(42)) + unicode(time.time())) tmp_filename = os.path.join(api_core.config.storage_medias_path(), random_temp_name) tmp_uri = os.path.join(api_core.config.storage_medias_uri(), random_temp_name) tmp_file = request.files[u'file'] tmp_file.save(tmp_filename) media = Media(user_id=auth_user, uri=tmp_uri, filename=secure_filename(tmp_file.filename), metadata={u'title': request.form.get(u'title', u'')}, status=Media.READY) api_core.save_media(media) return {u'success': True} except Exception as e: logging.exception(e) return {u'errors': [unicode(e)]}