public async static void SetAccessToken(SwapResponse token) { await SecureStorage.SetAsync("access_token", token.AccessToken); Preferences.Set("token_type", token.TokenType); Preferences.Set("token_scope", token.Scope); Preferences.Set("token_expires_in", token.ExpiresIn); Preferences.Set("token_created", DateTime.UtcNow); Preferences.Set("has_auth", true); SetRefreshToken(token.RefreshToken); }
public async Task <Response> CreateResponseAsync(Request request) { var requestRepository = storage.GetRepository <IRequestRepository>(); var responseRepository = storage.GetRepository <IResponseRepository>(); var responseImageRepository = storage.GetRepository <IResponseImageRepository>(); Response response; try { switch (request.Discriminator) { case nameof(SwapRequest): { string fromImagePath = Path.Combine(fileService.GlobalRequestImagesPath, request.Images.First().ImageName); string toImagePath = Path.Combine(fileService.GlobalRequestImagesPath, request.Images.Skip(1).First().ImageName); Bitmap swapFaces = FaceReplacer.ReplaceFaces(fromImagePath, toImagePath); response = new SwapResponse() { Discriminator = nameof(SwapResponse) }; await responseRepository.CreateAsync(response); ResponseImage responseImage = new ResponseImage() { Response = response }; await responseImageRepository.CreateAsync(responseImage); await storage.SaveAsync(); responseImage.ImageName = $"{responseImage.ImageId}.jpg"; responseImageRepository.Edit(responseImage); fileService.SaveFile(swapFaces, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName)); break; } case nameof(CutRequest): { FaceRecognizer faceRecognizer = new FaceRecognizer(request.Images.First().ImageName, new RgbPixel(0, 0, 255), facePathSystem); var faceBuffers = faceRecognizer.GetAllFacesImages(); response = new CutResponse() { Discriminator = nameof(CutResponse) }; await responseRepository.CreateAsync(response); foreach (byte[] faceBuffer in faceBuffers) { ResponseImage responseImage = new ResponseImage() { Response = response }; await responseImageRepository.CreateAsync(responseImage); await storage.SaveAsync(); responseImage.ImageName = $"{responseImage.ImageId}.jpg"; responseImageRepository.Edit(responseImage); await fileService.SaveFileAsync(faceBuffer, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName)); } break; } case nameof(DetectRequest): { FaceRecognizer faceRecognizer = new FaceRecognizer(request.Images.First().ImageName, new RgbPixel(0, 0, 255), facePathSystem); byte[] faceBuffer = faceRecognizer.GetOutlinedFacesImage(); response = new DetectResponse() { Discriminator = nameof(DetectResponse) }; await responseRepository.CreateAsync(response); ResponseImage responseImage = new ResponseImage() { Response = response }; await responseImageRepository.CreateAsync(responseImage); await storage.SaveAsync(); responseImage.ImageName = $"{responseImage.ImageId}.jpg"; responseImageRepository.Edit(responseImage); await fileService.SaveFileAsync(faceBuffer, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName)); break; } default: { throw new KeyNotFoundException($"RequestType is {request.Discriminator} not exist."); } } ; } catch { foreach (RequestImage requestImage in request.Images) { fileService.DeleteFile(Path.Combine(fileService.GlobalRequestImagesPath, requestImage.ImageName)); } requestRepository.Delete(request); await storage.SaveAsync(); throw new InvalidDataException("Non valid images."); } request.Response = response; requestRepository.Edit(request); await storage.SaveAsync(); return(response); }