|
2 | 2 |
|
3 | 3 | from bson import ObjectId |
4 | 4 | from elasticsearch import Elasticsearch, NotFoundError |
5 | | - |
| 5 | +from beanie import PydanticObjectId |
6 | 6 | from app.config import settings |
7 | 7 | from app.models.authorization import AuthorizationDB |
8 | | -from app.models.datasets import DatasetOut |
9 | | -from app.models.files import FileOut |
| 8 | +from app.models.datasets import DatasetOut, DatasetDB |
| 9 | +from app.models.files import FileOut, FileDB |
| 10 | +from app.models.thumbnails import ThumbnailOut, ThumbnailDB |
10 | 11 | from app.models.metadata import MetadataDB |
11 | 12 | from app.models.search import ( |
12 | 13 | ElasticsearchEntry, |
@@ -107,3 +108,56 @@ async def index_file( |
107 | 108 | insert_record(es, settings.elasticsearch_index, doc, file.id) |
108 | 109 | else: |
109 | 110 | insert_record(es, settings.elasticsearch_index, doc, file.id) |
| 111 | + |
| 112 | + |
| 113 | +async def index_thumbnail( |
| 114 | + es: Elasticsearch, |
| 115 | + thumbnail_id: str, |
| 116 | + file_id: str, |
| 117 | + dataset_id: str, |
| 118 | + update: bool = False, |
| 119 | +): |
| 120 | + """Create or update an Elasticsearch entry for the file. user_ids is the list of users |
| 121 | + with permission to at least view the file's dataset, it will be queried if not provided. |
| 122 | + """ |
| 123 | + if (file := await FileDB.get(PydanticObjectId(file_id))) is not None: |
| 124 | + if ( |
| 125 | + thumbnail := await ThumbnailDB.get(PydanticObjectId(thumbnail_id)) |
| 126 | + ) is not None: |
| 127 | + # Get authorized users from db |
| 128 | + authorized_user_ids = [] |
| 129 | + async for auth in AuthorizationDB.find( |
| 130 | + AuthorizationDB.dataset_id == ObjectId(dataset_id) |
| 131 | + ): |
| 132 | + authorized_user_ids += auth.user_ids |
| 133 | + # Get full metadata from db (granular updates possible but complicated) |
| 134 | + metadata = [] |
| 135 | + async for md in MetadataDB.find( |
| 136 | + MetadataDB.resource.resource_id == ObjectId(file.id) |
| 137 | + ): |
| 138 | + metadata.append(md.content) |
| 139 | + # Add en entry to the file index |
| 140 | + doc = ElasticsearchEntry( |
| 141 | + resource_type="thumbnail", |
| 142 | + name=file.name, |
| 143 | + creator=thumbnail.creator.email, |
| 144 | + created=thumbnail.created, |
| 145 | + user_ids=authorized_user_ids, |
| 146 | + content_type=thumbnail.content_type.content_type, |
| 147 | + content_type_main=thumbnail.content_type.main_type, |
| 148 | + file_id=str(file.id), |
| 149 | + dataset_id=str(file.dataset_id), |
| 150 | + folder_id=str(file.folder_id), |
| 151 | + bytes=thumbnail.bytes, |
| 152 | + metadata=metadata, |
| 153 | + downloads=thumbnail.downloads, |
| 154 | + ).dict() |
| 155 | + if update: |
| 156 | + try: |
| 157 | + update_record( |
| 158 | + es, settings.elasticsearch_index, {"doc": doc}, file.id |
| 159 | + ) |
| 160 | + except NotFoundError: |
| 161 | + insert_record(es, settings.elasticsearch_index, doc, file.id) |
| 162 | + else: |
| 163 | + insert_record(es, settings.elasticsearch_index, doc, file.id) |
0 commit comments