Example #1
0
def handle_uploaded_file(request, file, obj_id=''):
    prefix = '%s_'%(obj_id)
    sufix = ''
    file_name = file_name_pattern( formatText(str(file)), prefix=prefix, sufix=sufix)
    base_path = urljoin(MEDIA_ROOT+"/", file_path_pattern(request)).replace("//","/")
    storage = DefaultFileStorage(location = base_path, base_url = MEDIA_URL)
    path = storage.save(file_name, file)
    
    # Post Image to Vuforia using RESTful API
    vkeys = vuforia_keys(request)
    vuforia = Vuforia(
        access_key = str(vkeys.get('vuforia_access_key')),
        secret_key = str(vkeys.get('vuforia_secret_key'))
    )
    
    image_f = ''
    for chunk in file.chunks():
        image_f+= chunk
    
    pil_img = Image.open(StringIO(str(image_f)))
    (width, height) = pil_img.size
    
    image_file = file
    image = base64.b64encode(str(image_f))
    #================Meta Data Example==================
    # 
    # metadata_file = open('[METADATA_FILE]')
    # metadata = base64.b64encode(metadata_file.read())
    #===================================================
    
    metadata = base64.b64encode( '%s'%(image_file) ) # Metadata Added/Overrided after submition by ./cron.py

    target_response = vuforia.add_target({
        "name": file_name,
        "width": width,
        "image": image,
        "application_metadata": metadata,
        "active_flag": 1
        })
    
    return (path, target_response)