public async Task <ClientImageModel> DownloadLatest(ImageQuality quality)
        {
            FileModel file = await service.FindLatestImageAsync();

            Stream content = file.Content.AsStreamForRead();

            switch (quality)
            {
            case ImageQuality.Full:
                break;

            case ImageQuality.Medium:
                int width = 600;
                content = await ResizeImage(content, width, 0);

                break;

            case ImageQuality.Thumbnail:
                width   = 200;
                content = await ResizeImage(content, width, 0);

                break;

            default:
                throw new NotSupportedException(quality.ToString());
            }

            BitmapImage image = new BitmapImage();
            await image.SetSourceAsync(content.AsRandomAccessStream());

            return(new ClientImageModel()
            {
                Image = image,
                Stream = content,
                Date = file.CreatedAt
            });
        }
        public async Task <bool> TryHandleAsync(HttpRequest request, HttpResponse response)
        {
            if (request.Method == "POST")
            {
                if (request.Path == "/start")
                {
                    captureService.Start();
                    response.StatusCode = 200;
                }
                else if (request.Path == "/stop")
                {
                    captureService.Stop();
                    response.StatusCode = 200;
                }
                else if (request.Path == "/status")
                {
                    StatusResponse output = new StatusResponse();
                    lock (lastCaptureErrorLock)
                    {
                        output.Running   = captureService.IsRunning;
                        output.LastError = lastCaptureError;
                        lastCaptureError = null;
                    }

                    response.Output.WriteLine(JsonConvert.SerializeObject(output));
                    response.StatusCode = 200;
                }
                else if (request.Path == "/latest")
                {
                    FileModel file = await captureService.FindLatestImageAsync();

                    if (file == null)
                    {
                        response.StatusCode = 404;
                    }
                    else
                    {
                        ImageQuality quality  = GetImageQuality(request);
                        string       fileEtag = file.CreatedAt.ToString("yyyyMMddHHmmss") + quality;
                        string       etag;
                        if (request.Headers.TryGetValue("If-None-Match", out etag) && fileEtag == etag)
                        {
                            response.StatusCode = 304;
                            return(true);
                        }

                        response.StatusCode = 200;
                        response.Headers["Content-Type"] = "image/jpeg";
                        response.Headers["Date"]         = file.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss");
                        response.Headers["ETag"]         = fileEtag;

                        Stream content = file.Content.AsStreamForRead();

                        switch (quality)
                        {
                        case ImageQuality.Full:
                            break;

                        case ImageQuality.Medium:
                            int width = 600;
                            content = await ResizeImage(content, width, 0);

                            break;

                        case ImageQuality.Thumbnail:
                            width   = 200;
                            content = await ResizeImage(content, width, 0);

                            break;

                        default:
                            throw new NotSupportedException(quality.ToString());
                        }

                        await content.CopyToAsync(response.Output.BaseStream);

                        file.Content.Dispose();
                    }
                }
            }

            return(true);
        }