def generate_thumbnail_from_image(value, data, data_type): # generate / get thumbnail for image if not value: return data, data_type try: # try to get file info from "data:image/png;xxxxxxxxxxxxx" data infos, content = get_info_content(value) storage_file_path = get_storage_path_from_infos(infos) data['url'] = get_storage_file_url(storage_file_path) if infos and infos.split(';')[0].split(':')[1].split( '/')[0] == 'image': # apply special cases for images data_type = 'image' try: data.update({ "thumbnail": thumbnail_backend.get_thumbnail(storage_file_path, "500x500", upscale=False).url }) except ValueError: pass except IndexError: pass return data, data_type
def serialize_group_properties(feature, final_properties): properties = {} for key, value in final_properties.items(): data_type = 'data' data = value data_format = feature.layer.schema.get('properties', {}).get(key, {}).get('format') if data_format == 'data-url': # apply special cases for files data_type = 'file' data = {"url": None} if value: # generate / get thumbnail for image try: # try to get file info from "data:image/png;xxxxxxxxxxxxx" data infos, content = get_info_content(value) storage_file_path = get_storage_path_from_infos(infos) data['url'] = get_storage_file_url(storage_file_path) if infos and infos.split(';')[0].split(':')[1].split( '/')[0] == 'image': # apply special cases for images data_type = 'image' try: data.update({ "thumbnail": thumbnail_backend.get_thumbnail( storage_file_path, "500x500", upscale=False).url }) except ValueError: pass except IndexError: pass elif data_format == "date": data_type = 'date' try: value_date = datetime.strptime(str(value), "%Y-%m-%d").date() data = date(value_date, 'SHORT_DATE_FORMAT') except ValueError: pass properties.update({ key: { "display_value": data, "type": data_type, "title": feature.layer.get_property_title(key), "value": feature.properties.get(key), "schema": feature.layer.schema.get('properties', {}).get(key), "ui_schema": feature.layer.crud_view.ui_schema.get(key, {}) } }) return properties
def stored_image_base64(value): """ As data-url file are stored in custom storage and not in b64, we need to prepare data to use """ infos, content = get_info_content(value) infos = infos.split(';') data_type = infos[0] data_path = infos[1].strip('name=') storage = get_storage() with storage.open(data_path, 'rb') as data_file: file_bytes = data_file.read() file_b64 = base64.encodebytes(file_bytes) result = f"{data_type};base64," + file_b64.decode() return result
def test_get_info_content_no_file_name(self): value = self.feature_without_file_name.properties[self.property_key] info, content = get_info_content(self.feature_without_file_name.properties[self.property_key]) path = generate_storage_file_path(self.property_key, value, self.feature_without_file_name) self.assertTrue(path.endswith(f'{self.property_key}.png'), path)
def test_get_info_content_no_data(self): info, content = get_info_content(self.feature_without_file_data.properties[self.property_key]) self.assertIsNone(info) self.assertIsNone(content)