def GET(self): """ List all available repositories """ try: rm = rutils.get_repository_manager() inputs = web.input() if 'displayName' in inputs or 'genusTypeId' in inputs: querier = rm.get_repository_query() if 'displayName' in inputs: if utilities.unescaped(inputs['displayName']): querier.match_display_name(quote(inputs['displayName'], safe='/ '), match=True) else: querier.match_display_name(inputs['displayName'], match=True) if 'genusTypeId' in inputs: if utilities.unescaped(inputs['genusTypeId']): querier.match_genus_type(quote(inputs['genusTypeId'], safe='/ '), match=True) else: querier.match_genus_type(inputs['genusTypeId'], match=True) repositories = rm.get_repositories_by_query(querier) else: repositories = rm.repositories repositories = utilities.extract_items(repositories) return repositories except Exception as ex: utilities.handle_exceptions(ex)
def DELETE(self, repository_id): try: rm = rutils.get_repository_manager() data = rm.delete_repository(utilities.clean_id(repository_id)) return utilities.success() except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id, asset_id, content_id): try: rm = rutils.get_repository_manager() repository = rm.get_repository(utilities.clean_id(repository_id)) asset = repository.get_asset(utilities.clean_id(asset_id)) asset_contents = asset.get_asset_contents() data = {} for asset_content in asset_contents: if str(asset_content.ident) == str( utilities.clean_id(content_id)): data = asset_content.object_map break if 'fullUrl' in self.data(): contents = rutils.update_asset_map_with_content_url( rm, asset.object_map)['assetContents'] for content in contents: if content['id'] == str(utilities.clean_id(content_id)): data = content break return json.dumps(data) except Exception as ex: utilities.handle_exceptions(ex)
def POST(self): """ Create a new log, if authorized """ try: logm = logutils.get_logging_manager() if 'bankId' not in self.data(): utilities.verify_keys_present(self.data(), ['name', 'description']) form = logm.get_log_form_for_create([]) finalize_method = logm.create_log else: log = logm.get_log(Id(self.data()['bankId'])) form = logm.get_log_form_for_update(log.ident) finalize_method = logm.update_log if 'name' in self.data(): form.display_name = self.data()['name'] if 'description' in self.data(): form.description = self.data()['description'] if 'genusTypeId' in self.data(): form.set_genus_type(Type(self.data()['genusTypeId'])) new_log = utilities.convert_dl_object(finalize_method(form)) return new_log except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id): try: rm = rutils.get_repository_manager() repository = rm.get_repository(utilities.clean_id(repository_id)) repository = utilities.convert_dl_object(repository) return repository except Exception as ex: utilities.handle_exceptions(ex)
def DELETE(self, log_id, entry_id): try: logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) log.delete_log_entry(utilities.clean_id(entry_id)) return utilities.success() except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, log_id): try: logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) log = utilities.convert_dl_object(log) return log except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, log_id): try: logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) entries = log.get_log_entries() data = utilities.extract_items(entries) return data except Exception as ex: utilities.handle_exceptions(ex)
def DELETE(self, log_id): try: logm = logutils.get_logging_manager() logm.delete_log(utilities.clean_id(log_id)) return utilities.success() except IllegalState as ex: modified_ex = type(ex)('Log is not empty.') utilities.handle_exceptions(modified_ex) except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, log_id, entry_id): try: logm = logutils.get_logging_manager() lels = logm.get_log_entry_lookup_session(proxy=logm._proxy) lels.use_federated_log_view() entry = lels.get_log_entry(utilities.clean_id(entry_id)) entry_map = entry.object_map return entry_map except Exception as ex: utilities.handle_exceptions(ex)
def GET(self): """ List all available logs """ try: logm = logutils.get_logging_manager() logs = logm.logs logs = utilities.extract_items(logs) return logs except Exception as ex: utilities.handle_exceptions(ex)
def GET(self): """ List all available repositories """ try: rm = rutils.get_repository_manager() repositories = rm.repositories repositories = utilities.extract_items(repositories) return repositories except Exception as ex: utilities.handle_exceptions(ex)
def PUT(self, repository_id, asset_id, content_id): """ replace the asset content data ... keep the same name (ugh), but hacking this in for the Onyx workflow for CLIx. They want to author in Onyx with high-res images but then replace them with the right-sized images via a script. So we'll enable this endpoint and let the script find / replace the right asset content. """ try: x = web.input(inputFile={}) rm = rutils.get_repository_manager() als = rm.get_asset_lookup_session(proxy=rm._proxy) als.use_federated_repository_view() asset = als.get_asset(utilities.clean_id(asset_id)) asset_content = rutils.get_asset_content_by_id( asset, utilities.clean_id(content_id)) repository = rm.get_repository(utilities.clean_id(repository_id)) repository.use_isolated_repository_view() form = repository.get_asset_content_form_for_update( asset_content.ident) try: input_file = x['inputFile'].file except AttributeError: pass # no file included else: file_name = x['inputFile'].filename data = DataInputStream(input_file) data.name = file_name # default, but can be over-ridden by user params form.set_genus_type( rutils.get_asset_content_genus_type(file_name)) try: form.add_display_name( utilities.create_display_text(file_name)) except AttributeError: form.display_name = file_name form.set_data(data) params = self.data() form = utilities.set_form_basics(form, params) repository.update_asset_content(form) return utilities.convert_dl_object( repository.get_asset(asset.ident)) except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id, asset_id): try: rm = rutils.get_repository_manager() repository = rm.get_repository(utilities.clean_id(repository_id)) asset = repository.get_asset(utilities.clean_id(asset_id)) data = utilities.extract_items(asset.get_asset_contents()) if 'fullUrls' in self.data(): data = json.dumps( rutils.update_asset_map_with_content_url( rm, asset.object_map)['assetContents']) return data except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id=None): try: if repository_id is None: raise PermissionDenied rm = rutils.get_repository_manager() params = web.input() if 'allAssets' in params: als = rm.get_asset_lookup_session(proxy=rm._proxy) als.use_federated_repository_view() assets = als.get_assets() elif 'genusTypeId' in params: repository = rm.get_repository( utilities.clean_id(repository_id)) querier = repository.get_asset_query() if utilities.unescaped(params['genusTypeId']): querier.match_genus_type(quote(params['genusTypeId'], safe='/ '), match=True) else: querier.match_genus_type(params['genusTypeId'], match=True) assets = repository.get_assets_by_query(querier) else: repository = rm.get_repository( utilities.clean_id(repository_id)) assets = repository.get_assets() data = utilities.extract_items(assets) # Update the source field with the displayName.text of the actual resource data = json.loads(data) for asset_map in data: resource_utils.update_asset_map_with_resource(asset_map) data = json.dumps(data) if 'fullUrls' in params: data = json.loads(data) updated_data = [] for asset in data: updated_data.append( rutils.update_asset_map_with_content_url(rm, asset)) data = json.dumps(updated_data) return data except Exception as ex: utilities.handle_exceptions(ex)
def PUT(self, repository_id): try: rm = rutils.get_repository_manager() data = self.data() form = rm.get_repository_form_for_update( utilities.clean_id(repository_id)) form = utilities.set_form_basics(form, data) updated_repository = rm.update_repository(form) if 'aliasId' in data: rm.alias_repository(updated_repository.ident, utilities.clean_id(data['aliasId'])) repository = utilities.convert_dl_object(updated_repository) return repository except Exception as ex: utilities.handle_exceptions(ex)
def POST(self, log_id): try: utilities.verify_keys_present(self.data(), ['data']) logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) form = log.get_log_entry_form_for_create([TEXT_BLOB_RECORD_TYPE]) if isinstance(self.data()['data'], dict): blob = json.dumps(self.data()['data']) else: blob = str(self.data()['data']) form.set_text(blob) entry = log.create_log_entry(form) return utilities.convert_dl_object(entry) except Exception as ex: utilities.handle_exceptions(ex)
def PUT(self, log_id): try: logm = logutils.get_logging_manager() form = logm.get_log_form_for_update(utilities.clean_id(log_id)) utilities.verify_at_least_one_key_present(self.data(), ['name', 'description']) # should work for a form or json data if 'name' in self.data(): form.display_name = self.data()['name'] if 'description' in self.data(): form.description = self.data()['description'] updated_log = logm.update_log(form) updated_log = utilities.convert_dl_object(updated_log) return updated_log except Exception as ex: utilities.handle_exceptions(ex)
def POST(self): # get or find a default log genus type try: input_data = self.data() utilities.verify_keys_present(input_data, ['data']) log = self._get_log() form = log.get_log_entry_form_for_create([TEXT_BLOB_RECORD_TYPE]) if isinstance(input_data['data'], dict): blob = json.dumps(input_data['data']) else: blob = str(input_data['data']) form.set_text(blob) form = utilities.set_form_basics(form, input_data) entry = log.create_log_entry(form) return utilities.convert_dl_object(entry) except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id, asset_id): try: rm = rutils.get_repository_manager() als = rm.get_asset_lookup_session(proxy=rm._proxy) als.use_federated_repository_view() data = utilities.convert_dl_object( als.get_asset(utilities.clean_id(asset_id))) if 'fullUrls' in self.data().keys(): data = json.loads(data) data = rutils.update_asset_map_with_content_url(rm, data) data = json.dumps(data) # Update the source field with the displayName.text of the actual resource data = resource_utils.update_asset_map_with_resource(data) return data except Exception as ex: utilities.handle_exceptions(ex)
def POST(self, repository_id, asset_id): try: x = web.input(inputFile={}) rm = rutils.get_repository_manager() repository = rm.get_repository(utilities.clean_id(repository_id)) repository.use_isolated_repository_view() asset = repository.get_asset(utilities.clean_id(asset_id)) # get each set of files individually, because # we are doing this in memory, so the file pointer changes # once we read in a file # https://docs.python.org/2/library/zipfile.html params = self.data() try: input_file = x['inputFile'].file except AttributeError: asset_content_type_list = rutils.get_asset_content_records( repository) form = repository.get_asset_content_form_for_create( asset.ident, asset_content_type_list) form = utilities.set_form_basics(form, params) asset_content = repository.create_asset_content(form) else: file_name = x['inputFile'].filename # now let's create an asset content for this asset, with the # right genus type and file data. Also set the form basics, if passed in updated_asset, asset_content = rutils.append_file_as_asset_content( repository, asset.ident, file_name, input_file, params) # need to get the updated asset with Contents asset_content_map = json.loads( utilities.convert_dl_object(asset_content)) if 'fullUrl' in params: asset_content_map = rutils.update_asset_map_with_content_url( rm, asset_content_map) return json.dumps(asset_content_map) except Exception as ex: utilities.handle_exceptions(ex)
def POST(self): """ Create a new repository, if authorized """ try: rm = rutils.get_repository_manager() form = rm.get_repository_form_for_create([]) data = self.data() form = utilities.set_form_basics(form, data) new_repository = utilities.convert_dl_object( rm.create_repository(form)) if 'aliasId' in data: rm.alias_repository( utilities.clean_id(json.loads(new_repository)['id']), utilities.clean_id(data['aliasId'])) return new_repository except Exception as ex: utilities.handle_exceptions(ex)
def PUT(self, log_id, entry_id): try: utilities.verify_at_least_one_key_present(self.data(), ['data']) logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) form = log.get_log_entry_form_for_update( utilities.clean_id(entry_id)) if 'data' in self.data(): if isinstance(self.data()['data'], dict): blob = json.dumps(self.data()['data']) else: blob = str(self.data()['data']) form.set_text(blob) log.update_log_entry(form) entry = log.get_log_entry(utilities.clean_id(entry_id)) return utilities.convert_dl_object(entry) except Exception as ex: utilities.handle_exceptions(ex)
def POST(self, repository_id): try: main_input_file = web.input(inputFile={}) vtt_input_file = web.input(vttFile={}) transcript_input_file = web.input(transcriptFile={}) rm = rutils.get_repository_manager() repository = rm.get_repository(utilities.clean_id(repository_id)) repository.use_isolated_repository_view() # get each set of files individually, because # we are doing this in memory, so the file pointer changes # once we read in a file # https://docs.python.org/2/library/zipfile.html params = self.data() locale = 'en' if 'locale' in params: locale = params['locale'] if locale not in ['en', 'hi', 'te']: raise TypeError( "The 'locale' parameter must be one of: 'en', 'hi', or 'te'" ) try: input_file = main_input_file['inputFile'].file except AttributeError: form = repository.get_asset_form_for_create([]) form = utilities.set_form_basics(form, params) asset = repository.create_asset(form) else: # first, let's search the database to see if an asset exists with # the file name -- file name will be everything minus the # extension and language indicator. # for example, for one video there might be four files: # * ee_u1l01a01v01.mov # * ee_u1l01a01v01_en.vtt # * ee_u1l01a01v01_hi.vtt # * ee_u1l01a01v01_te.vtt # In this case, the "filename" / asset displayName.text is `ee_u1l01a01v01`. # If that already exists, add the input_file as an asset content. # If that asset does not exist, create it. file_name = main_input_file['inputFile'].filename if 'createNew' in params.keys() and params['createNew']: asset = rutils.create_asset(repository, file_name) else: try: querier = repository.get_asset_query() except (AttributeError, Unimplemented, PermissionDenied): assets = None else: querier.match_display_name( rutils.get_singular_filename(file_name), match=True) assets = repository.get_assets_by_query(querier) if assets is not None and assets.available() > 0: asset = assets.next() else: asset = rutils.create_asset(repository, file_name) # now let's create an asset content for this asset, with the # right genus type and file data rutils.append_file_as_asset_content(repository, asset.ident, file_name, input_file) # Check if transcripts or VTT files are included try: vtt_file = vtt_input_file['vttFile'].file except AttributeError: pass else: file_name = vtt_input_file['vttFile'].filename rutils.append_vtt_file_as_asset_content( repository, asset.ident, file_name, vtt_file, locale) try: transcript_file = transcript_input_file['transcriptFile'].file except AttributeError: pass else: file_name = transcript_input_file['transcriptFile'].filename rutils.append_transcript_file_as_asset_content( repository, asset.ident, file_name, transcript_file, locale) if any(term in params.keys() for term in ['license', 'copyright', 'source', 'provider']): form = repository.get_asset_form_for_update(asset.ident) if 'license' in params.keys(): form.set_license(params['license']) if 'copyright' in params.keys(): form.set_copyright(params['copyright']) if 'source' in params.keys(): resource_id = resource_utils.get_or_create_resource_id( repository, params['source']) form.set_source(resource_id) if 'provider' in params.keys(): resource_id = resource_utils.get_or_create_resource_id( repository, params['provider']) form.set_provider(resource_id) asset = repository.update_asset(form) # Handle the alt-text for images if 'altText' in params.keys(): rutils.append_text_as_asset_content( repository, asset.ident, utilities.clean_json(params['altText']), 'Alt text', 'alt-text') if 'mediaDescription' in params.keys(): rutils.append_text_as_asset_content( repository, asset.ident, utilities.clean_json(params['mediaDescription']), 'Description', 'mediaDescription') if 'createdDate' in params.keys(): # set the createdDate rutils.set_asset_created_date( repository, asset.ident, DateTime(**utilities.clean_json(params['createdDate']))) # need to get the updated asset with Contents asset = repository.get_asset(asset.ident) asset_map = json.loads(utilities.convert_dl_object(asset)) if 'returnUrl' in web.input().keys(): asset_map = rutils.update_asset_map_with_content_url( rm, asset_map) # Update the source field with the displayName.text of the actual resource asset_map = resource_utils.update_asset_map_with_resource( asset_map) return json.dumps(asset_map) except Exception as ex: utilities.handle_exceptions(ex)
def GET(self, repository_id, asset_id, content_id): try: rm = rutils.get_repository_manager() als = rm.get_asset_lookup_session(proxy=rm._proxy) als.use_federated_repository_view() asset = als.get_asset(utilities.clean_id(asset_id)) asset_content = rutils.get_asset_content_by_id( asset, utilities.clean_id(content_id)) if asset_content.genus_type == rutils.TRANSCRIPT_ASSET_CONTENT_GENUS_TYPE: web.header('Content-Type', 'text/plain') yield asset_content.get_transcript_text() elif asset_content.genus_type == rutils.VTT_ASSET_CONTENT_GENUS_TYPE: web.header('Content-Type', 'text/plain') yield asset_content.get_vtt_text() else: asset_content_data = asset_content.get_data() filespace_path = '' try: config = asset._runtime.get_configuration() parameter_id = utilities.clean_id( 'parameter:dataStoreFullPath@mongo') filespace_path = config.get_value_by_parameter( parameter_id).get_string_value() except (AttributeError, KeyError): pass # the asset_url is relative, so add in the path asset_content_path = '{0}/{1}'.format(filespace_path, asset_content_data.name) web.header('Content-Type', mimetypes.guess_type(asset_content_path)[0]) web.header('Accept-Ranges', 'bytes') # The algorithm below for streaming partial content was based off of this # post: # https://benramsey.com/blog/2008/05/206-partial-content-and-range-requests/ continue_with_stream = True byte_range = rutils.get_byte_ranges() total_bytes_to_read = os.path.getsize(asset_content_data.name) content_length = os.path.getsize(asset_content_data.name) bytes_to_throw_away = 0 if byte_range is not None: bytes_to_throw_away = int(byte_range[0]) if bytes_to_throw_away > total_bytes_to_read or bytes_to_throw_away < 0: web.ctx.status = '416 Requested Range Not Satisfiable' continue_with_stream = False asset_content_data.close() yield '' else: asset_content_data.read(bytes_to_throw_away) total_bytes_to_read = os.path.getsize( asset_content_data.name) - bytes_to_throw_away if byte_range[1] != '': total_bytes_to_read = int( byte_range[1]) - bytes_to_throw_away if continue_with_stream: bytes_read = 0 num_bytes_to_read = 1024 * 8 starting_bytes = bytes_to_throw_away web.ctx.status = '206 Partial Content' while continue_with_stream: remaining_bytes = total_bytes_to_read - bytes_read bytes_to_read = min(num_bytes_to_read, remaining_bytes) buf = asset_content_data.read(bytes_to_read) if not buf: asset_content_data.close() break bytes_actually_read = len(buf) # web.header('Content-Length', total_bytes_to_read) # Introduce a -1 here because otherwise the bytes_read indicator is off # by one to the browser. The ending value {1} has to be the last byte # actually read. That broke video streaming in Chrome, not in FF. range_header_str = 'bytes {0}-{1}/{2}'.format( str(starting_bytes), str(starting_bytes + bytes_actually_read - 1), str(content_length)) web.header('Content-Range', range_header_str) bytes_read += bytes_actually_read starting_bytes += bytes_actually_read yield buf except Exception as ex: utilities.handle_exceptions(ex)
def PUT(self, repository_id, asset_id): try: main_input_file = web.input(inputFile={}) vtt_input_file = web.input(vttFile={}) transcript_input_file = web.input(transcriptFile={}) asset_id = utilities.clean_id(asset_id) rm = rutils.get_repository_manager() repo = rm.get_repository(utilities.clean_id(repository_id)) params = self.data() locale = 'en' if 'locale' in params: locale = params['locale'] # update the asset contents here, for convenience if 'clearAltTexts' in params and params['clearAltTexts']: rutils.clear_alt_texts(repo, asset_id) if 'altText' in params: # find the right asset content by genus type. Grab its ID # then get the asset content update form # call form.add_display_name(new alt text) # update form rutils.add_alt_text_to_asset( repo, asset_id, utilities.clean_json(params['altText'])) if 'removeAltTextLanguage' in params: rutils.remove_alt_text_language( repo, asset_id, params['removeAltTextLanguage']) if 'clearMediaDescriptions' in params and params[ 'clearMediaDescriptions']: rutils.clear_media_descriptions(repo, asset_id) if 'mediaDescription' in params: rutils.add_media_description_to_asset( repo, asset_id, utilities.clean_json(params['mediaDescription'])) if 'removeMediaDescriptionLanguage' in params: rutils.remove_media_description_language( repo, asset_id, params['removeMediaDescriptionLanguage']) # Now handle the vtt and transcript uploaded files if 'clearVTTFiles' in params and params['clearVTTFiles']: rutils.clear_vtt_files(repo, asset_id) try: vtt_file = vtt_input_file['vttFile'].file except AttributeError: pass else: file_name = vtt_input_file['vttFile'].filename rutils.add_vtt_file_to_asset(repo, asset_id, file_name, vtt_file, locale) if 'removeVTTFileLanguage' in params: rutils.remove_vtt_file_language( repo, asset_id, params['removeVTTFileLanguage']) if 'clearTranscriptFiles' in params and params[ 'clearTranscriptFiles']: rutils.clear_transcript_files(repo, asset_id) try: transcript_file = transcript_input_file['transcriptFile'].file except AttributeError: pass else: file_name = transcript_input_file['transcriptFile'].filename rutils.add_transcript_file_to_asset(repo, asset_id, file_name, transcript_file, locale) if 'removeTranscriptFileLanguage' in params: rutils.remove_transcript_file_language( repo, asset_id, params['removeTranscriptFileLanguage']) # also handle updating the main asset content (image or video or audio) try: main_file = main_input_file['inputFile'].file except AttributeError: pass else: file_name = main_input_file['inputFile'].filename rutils.replace_asset_main_content(repo, asset_id, file_name, main_file) form = repo.get_asset_form_for_update(asset_id) form = utilities.set_form_basics(form, params) if 'license' in params.keys(): form.set_license(params['license']) if 'copyright' in params.keys(): form.set_copyright(params['copyright']) if 'source' in params.keys(): resource_id = resource_utils.get_or_create_resource_id( repo, params['source']) form.set_source(resource_id) if 'provider' in params.keys(): resource_id = resource_utils.get_or_create_resource_id( repo, params['provider']) form.set_provider(resource_id) repo.update_asset(form) if 'createdDate' in params.keys(): rutils.set_asset_created_date( repo, asset_id, DateTime(**params['createdDate'])) data = utilities.convert_dl_object(repo.get_asset(asset_id)) # Update the source field with the displayName.text of the actual resource data = resource_utils.update_asset_map_with_resource(data) return data except Exception as ex: utilities.handle_exceptions(ex)