diff --git a/ckanext/odsh/tools.py b/ckanext/odsh/tools.py index 9fbd4f90a879f595418f1e493f315b2a2ebcc58c..ab7c1ba60885d1a4589b1bc533f9d22e4d74d27b 100644 --- a/ckanext/odsh/tools.py +++ b/ckanext/odsh/tools.py @@ -13,22 +13,33 @@ def add_attributes_resources(context, resource): for item in resources: if item.get('id') == resource.get('id'): path = get_filepath_to_resource(resource) - with open(path, 'rb') as file: - if not item.get('size'): - resource_size = os.path.getsize(path) - item.update({'size': resource_size}) - file.seek(0) - hash = calculate_hash(file) - item.update({'hash':hash}) - file_type = magic.from_file(path, mime = True) - if file_type == 'application/pdf': - file.seek(0) - pdf = pdftotext.PDF(file) - number_of_pages = len(pdf) - item.update({'number_of_pages':number_of_pages}) - item.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'}) - resources[i] = item - break + if os.path.exists(path): + with open(path, 'rb') as file: + + #size + if not item.get('size'): + resource_size = os.path.getsize(path) + item.update({'size': resource_size}) + + #hash + file.seek(0) + hash = calculate_hash(file) + item.update({'hash':hash}) + + #hash algorithm + item.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'}) + + + #number of pages + file_type = magic.from_file(path, mime = True) + if file_type == 'application/pdf': + file.seek(0) + pdf = pdftotext.PDF(file) + number_of_pages = len(pdf) + item.update({'number_of_pages':number_of_pages}) + + resources[i] = item + break i = i + 1 package.update({'resources':resources}) toolkit.get_action('package_update')(context, package)