# Path: cufacesearch/cufacesearch/imgio/imgio.py
# class UnknownImageFormat(Exception):
# DEFAULT_RETRIES = 3
# DEFAULT_BOF = 0.2
# DEFAULT_FL = [500, 502, 503, 504]
# SESSION = requests.Session()
# RETRIES_SETTINGS = Retry(total=DEFAULT_RETRIES, backoff_factor=DEFAULT_BOF,
#                          status_forcelist=DEFAULT_FL)
# def get_SHA1_from_data(data):
# def get_image_size_and_format(input_data):
# def get_SHA1_img_type_from_B64(base64str):
# def get_SHA1_from_buffer(img_buffer):
# def get_SHA1_img_info_from_buffer(img_buffer):
# def buffer_to_B64(img_buffer):
# def get_buffer_from_B64(base64str):
# def get_buffer_from_filepath(filepath):
# def get_buffer_from_URL(img_url, verbose=0, image_dl_timeout=4, retries=DEFAULT_RETRIES):
# def load_image_from_buffer(img_buffer):
# 
# Path: cufacesearch/cufacesearch/detector/utils.py
# def build_bbox_str_list(bbox):
#   """Build bounding box string representation from ``bbox``
# 
#   :param bbox: dictionary with keys: ``left``, ``top``, ``right``, ``bottom``
#   :type bbox: dict
#   :return: bounding box string list [left, top, width, height]
#   :rtype: string
#   """
#   width = bbox['right'] - bbox['left']
#   height = bbox['bottom'] - bbox['top']
#   bbox_str_list = []
#   bbox_str_list.append(str(max(0, bbox['left'])))
#   bbox_str_list.append(str(max(0, bbox['top'])))
#   bbox_str_list.append(str(width))
#   bbox_str_list.append(str(height))
#   return bbox_str_list


import os
import sys
import time
import json
from datetime import datetime
from flask import Markup, flash, request, render_template, make_response
from flask_restful import Resource
from ..imgio.imgio import ImageMIMETypes, get_SHA1_img_type_from_B64, get_SHA1_img_info_from_buffer, \
  buffer_to_B64
from ..detector.utils import build_bbox_str_list
from socket import *
    from ..common.dl import fixurl
    :param options: options JSON string
    :type options: str
    :return: HTML page showing the results.
    """
    if query_type == 'B64':
      # get :
      # - sha1 to be able to map to query response
      # - image type to make sure the image is displayed properly
      # - embedded format for each b64 query
      # TODO: use array parameter
      query_list = query.split(',')
      query_b64_infos = [get_SHA1_img_type_from_B64(q) for q in query_list if not q.startswith('data:')]
      query_urls_map = dict()
      for img_id, img_info in enumerate(query_b64_infos):
        query_urls_map[img_info[0]] = "data:"+ImageMIMETypes[img_info[1]]+";base64,"+str(query_list[img_id])
    elif query_type == "PATH" or (query_type == "SHA1" and self.searcher.file_input):
      # Encode query in B64
      query_infos = []
      query_list = query.split(',')
      # Get images paths from sha1s
      if query_type == 'SHA1' and self.searcher.file_input:
        rows_imgs = self.searcher.indexer.get_columns_from_sha1_rows(query_list, columns=[self.searcher.img_column])
        query_list = [row[1][self.searcher.img_column] for row in rows_imgs]
      query_list_B64 = []
      for q in query_list:
        with open(q,'rb') as img_buffer:
          query_infos.append(get_SHA1_img_info_from_buffer(img_buffer))
          query_list_B64.append(buffer_to_B64(img_buffer))
      query_urls_map = dict()
      for img_id, img_info in enumerate(query_infos):

# Path: cufacesearch/cufacesearch/imgio/imgio.py
# class UnknownImageFormat(Exception):
# DEFAULT_RETRIES = 3
# DEFAULT_BOF = 0.2
# DEFAULT_FL = [500, 502, 503, 504]
# SESSION = requests.Session()
# RETRIES_SETTINGS = Retry(total=DEFAULT_RETRIES, backoff_factor=DEFAULT_BOF,
#                          status_forcelist=DEFAULT_FL)
# def get_SHA1_from_data(data):
# def get_image_size_and_format(input_data):
# def get_SHA1_img_type_from_B64(base64str):
# def get_SHA1_from_buffer(img_buffer):
# def get_SHA1_img_info_from_buffer(img_buffer):
# def buffer_to_B64(img_buffer):
# def get_buffer_from_B64(base64str):
# def get_buffer_from_filepath(filepath):
# def get_buffer_from_URL(img_url, verbose=0, image_dl_timeout=4, retries=DEFAULT_RETRIES):
# def load_image_from_buffer(img_buffer):
# 
# Path: cufacesearch/cufacesearch/detector/utils.py
# def build_bbox_str_list(bbox):
#   """Build bounding box string representation from ``bbox``
# 
#   :param bbox: dictionary with keys: ``left``, ``top``, ``right``, ``bottom``
#   :type bbox: dict
#   :return: bounding box string list [left, top, width, height]
#   :rtype: string
#   """
#   width = bbox['right'] - bbox['left']
#   height = bbox['bottom'] - bbox['top']
#   bbox_str_list = []
#   bbox_str_list.append(str(max(0, bbox['left'])))
#   bbox_str_list.append(str(max(0, bbox['top'])))
#   bbox_str_list.append(str(width))
#   bbox_str_list.append(str(height))
#   return bbox_str_list


# Path: cufacesearch/cufacesearch/api/api.py
import os
import sys
import time
import json
from datetime import datetime
from flask import Markup, flash, request, render_template, make_response
from flask_restful import Resource
from ..imgio.imgio import ImageMIMETypes, get_SHA1_img_type_from_B64, get_SHA1_img_info_from_buffer, \
  buffer_to_B64
from ..detector.utils import build_bbox_str_list
from socket import *
    from ..common.dl import fixurl

    :param options: options JSON string
    :type options: str
    :return: HTML page showing the results.
    """
    if query_type == 'B64':
      # get :
      # - sha1 to be able to map to query response
      # - image type to make sure the image is displayed properly
      # - embedded format for each b64 query
      # TODO: use array parameter
      query_list = query.split(',')
      query_b64_infos = [get_SHA1_img_type_from_B64(q) for q in query_list if not q.startswith('data:')]
      query_urls_map = dict()
      for img_id, img_info in enumerate(query_b64_infos):
        query_urls_map[img_info[0]] = "data:"+ImageMIMETypes[img_info[1]]+";base64,"+str(query_list[img_id])
    elif query_type == "PATH" or (query_type == "SHA1" and self.searcher.file_input):
      # Encode query in B64
      query_infos = []
      query_list = query.split(',')
      # Get images paths from sha1s
      if query_type == 'SHA1' and self.searcher.file_input:
        rows_imgs = self.searcher.indexer.get_columns_from_sha1_rows(query_list, columns=[self.searcher.img_column])
        query_list = [row[1][self.searcher.img_column] for row in rows_imgs]
      query_list_B64 = []
      for q in query_list:
        with open(q,'rb') as img_buffer:
          query_infos.append(get_SHA1_img_info_from_buffer(img_buffer))
          query_list_B64.append(buffer_to_B64(img_buffer))
      query_urls_map = dict()
      for img_id, img_info in enumerate(query_infos):

        query_urls_map[img_info[0]] = "data:" + ImageMIMETypes[img_info[1]] + ";base64," + str(query_list_B64[img_id])