|
2 | 2 | from unity_sds_client.resources.dataset import Dataset
|
3 | 3 | from unity_sds_client.resources.data_file import DataFile
|
4 | 4 | from pystac import Catalog, get_stac_version, ItemCollection, Item, Asset
|
| 5 | +from pystac.extensions.file import FileExtension |
5 | 6 | from pystac.errors import STACTypeError
|
| 7 | +import hashlib |
6 | 8 | import json
|
7 | 9 | import os
|
8 | 10 | from datetime import datetime
|
@@ -132,15 +134,33 @@ def to_stac(collection, data_dir):
|
132 | 134 | if key.startswith("./"):
|
133 | 135 | key = os.path.basename(key)
|
134 | 136 |
|
135 |
| - item.add_asset( |
136 |
| - key = key, |
137 |
| - asset = Asset( |
138 |
| - href = item_location, |
139 |
| - title = "{} file".format(df.type), |
140 |
| - description = "", |
141 |
| - roles = df.roles |
| 137 | + asset = Asset( |
| 138 | + href = item_location, |
| 139 | + title = "{} file".format(df.type), |
| 140 | + description = "", |
| 141 | + roles = df.roles |
| 142 | + ) |
| 143 | + |
| 144 | + # If the file exists locally on disk then add optional STAC |
| 145 | + # attribuites: file:size, file:checksum |
| 146 | + data_filename = os.path.join(data_dir, item_location) |
| 147 | + if os.path.exists(data_filename): |
| 148 | + # Get file size |
| 149 | + file_stats = os.stat(data_filename) |
| 150 | + |
| 151 | + # Compute MD5SUM |
| 152 | + md5 = hashlib.md5() |
| 153 | + with open(data_filename, "rb") as f: |
| 154 | + while chunk := f.read(4096): |
| 155 | + md5.update(chunk) |
| 156 | + |
| 157 | + file_ext = FileExtension.ext(asset) |
| 158 | + file_ext.apply( |
| 159 | + size=file_stats.st_size, |
| 160 | + checksum=md5.hexdigest() |
142 | 161 | )
|
143 |
| - ) |
| 162 | + |
| 163 | + item.add_asset(key=key, asset=asset) |
144 | 164 |
|
145 | 165 | from pystac.layout import TemplateLayoutStrategy
|
146 | 166 | write_dir = data_dir
|
|
0 commit comments