X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=httpserver.py;h=870d680247f4f4b398c5a80a2881c86c75ab946f;hb=refs%2Fchanges%2F04%2F604%2F1;hp=86f96a685df946d5854089306e2e95a15ccc6ed5;hpb=9a61c6b761065160d0889e7bd1e0f9fc37de5310;p=osm%2Fopenvim.git diff --git a/httpserver.py b/httpserver.py index 86f96a6..870d680 100644 --- a/httpserver.py +++ b/httpserver.py @@ -30,10 +30,13 @@ __author__="Alfonso Tierno" __date__ ="$10-jul-2014 12:07:15$" import bottle +import urlparse import yaml import json import threading import datetime +import hashlib +import os import RADclass from jsonschema import validate as js_v, exceptions as js_e import host_thread as ht @@ -61,6 +64,12 @@ HTTP_Conflict = 409 HTTP_Service_Unavailable = 503 HTTP_Internal_Server_Error= 500 +def md5(fname): + hash_md5 = hashlib.md5() + with open(fname, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() def check_extended(extended, allow_net_attach=False): '''Makes and extra checking of extended input that cannot be done using jsonschema @@ -443,6 +452,18 @@ def check_valid_uuid(uuid): except js_e.ValidationError: return False + +def is_url(url): + ''' + Check if string value is a well-wormed url + :param url: string url + :return: True if is a valid url, False if is not well-formed + ''' + + parsed_url = urlparse.urlparse(url) + return parsed_url + + @bottle.error(400) @bottle.error(401) @bottle.error(404) @@ -1077,6 +1098,22 @@ def http_post_images(tenant_id): metadata_dict = http_content['image'].pop('metadata', None) if metadata_dict is not None: http_content['image']['metadata'] = json.dumps(metadata_dict) + #calculate checksum + host_test_mode = True if config_dic['mode']=='test' or config_dic['mode']=="OF only" else False + try: + image_file = http_content['image'].get('path',None) + if os.path.exists(image_file): + http_content['image']['checksum'] = md5(image_file) + elif is_url(image_file): + pass + else: + if not host_test_mode: + content = "Image file not found" + print "http_post_images error: %d %s" % (HTTP_Bad_Request, content) + bottle.abort(HTTP_Bad_Request, content) + except Exception as e: + print "ERROR. Unexpected exception: %s" % (str(e)) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) #insert in data base result, content = my.db.new_image(http_content['image'], tenant_id) if result >= 0: