in reference-implementations/semantic-search-for-images/src/ui-backend/Controllers/SemanticSearchController.cs [50:89]
public async Task<IActionResult> SearchByImage()
{
_logger.LogInformation("Triggered SearchByImageStream");
if (Request.ContentType == null)
return new BadRequestObjectResult("Request Content-Type must be set. If streaming the image as the body, use 'application/octet-stream'. If providing the image as form data, use 'multipart/form-data'");
if (Request.ContentLength == 0)
return new BadRequestObjectResult("Request has no content.");
float[] queryEmbeddings;
if (Request.ContentType.Contains("application/octet-stream", StringComparison.InvariantCultureIgnoreCase)
|| Request.ContentType.Contains("image/png", StringComparison.InvariantCultureIgnoreCase)
|| Request.ContentType.Contains("image/jpeg", StringComparison.InvariantCultureIgnoreCase)
|| Request.ContentType.Contains("image/gif", StringComparison.InvariantCultureIgnoreCase))
{
var sanitizedContentType = Request.ContentType.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
_logger.LogInformation($"Content-Type: {sanitizedContentType}. Passing request body directly to generate embeddings.");
// Assume the body is an image stream and directly pass along...
queryEmbeddings = await _imageService.GenerateImageEmbeddings(Request.Body);
}
else if (Request.ContentType.Contains("multipart/form-data", StringComparison.InvariantCultureIgnoreCase))
{
if (Request.Form.Files.Count == 0)
return new BadRequestObjectResult("Request form data does not include a file.");
if (Request.Form.Files.Count > 1)
return new BadRequestObjectResult("Request form data includes more than one file; only one may be accepted.");
_logger.LogInformation("Content-Type: multipart/form-data. Passing request form file read stream to generate embeddings.");
var file = Request.Form.Files[0];
queryEmbeddings = await _imageService.GenerateImageEmbeddings(file.OpenReadStream());
}
else
{
return new BadRequestObjectResult("Request could not be handled. Expects either image as form data (multipart/form-data) or direct body stream (appliation/octet-stream).");
}
return Ok(await _databaseService.Search(queryEmbeddings));
}