public async Task <long> CreateAsync(Visit visit) { var storage = _storageFactory.CreateStorage <VisitEntity>(); var entity = _mapper.Map <VisitEntity>(visit); entity.Created = DateTime.Now; var key = await storage.CreateAsync(entity); return(key.LongValue); }
public async Task <Redirectable> LoadAsync(string key) { var storage = _storageFactory.CreateStorage <MicroUrlEntity>(); var loaded = await storage.LoadAsync(_keyFactory.CreateFromString(key)); if (loaded == null) { return(null); } switch (loaded.Type) { case MicroUrlEntity.TextType: return(_mapper.Map <MicroText>(loaded)); case MicroUrlEntity.UrlType: return(_mapper.Map <MicroUrl>(loaded)); default: throw new ArgumentException("Invalid entity type"); } }
public async Task <DeleteImageCommandResult> Handle(DeleteImageCommand request, CancellationToken cancellationToken) { try { _logger.LogDebug("try to delete images..."); var storage = _storageFactory.CreateStorage(); var result = storage.Remove(request.FolderName); _logger.LogDebug("images was deleted!"); return(new DeleteImageCommandResult(result)); } catch (Exception ex) { _logger.LogError(ex, "delete image failed!"); return(null); } }
public async Task <GetImageViewModel> Handle(GetScaledImageQuery request, CancellationToken cancellationToken) { try { _logger.LogDebug("try to get image..."); var storage = _storageFactory.CreateStorage(); var data = storage.Get(request.FolderName, request.FileName); _logger.LogDebug("image was recieved!"); return(new GetImageViewModel(data)); } catch (Exception ex) { _logger.LogError(ex, "get image failed!"); return(null); } }
public IStorage <T> GetStorage <T>() where T : class, IComponent, new() { Type storageType = typeof(T); IStorage <T> result; if (storages.TryGetValue(storageType, out IStorage storage)) { result = (IStorage <T>)storage; } else { result = factory.CreateStorage <T>(); storages.Add(storageType, result); AddDataEntriesToNewStorage(result); } return(result); }
public async Task <AddImageCommandResult> Handle(AddImageCommand request, CancellationToken cancellationToken) { try { _logger.LogDebug("try to add image..."); List <string> scaledImages = new List <string>(); var match = _regex.Match(request.FileName); var name = match.Result("${name}"); var extension = match.Result("${extension}"); var storage = _storageFactory.CreateStorage(); var imageProcessing = _imageProcessingFactory.CreateImageProcessing(); foreach (var scaledImage in imageProcessing.GetScaledImages(request.Data, extension)) { var storageResult = false; if (!request.RewriteMode) { storageResult = await storage.AddAsync(name, $"{scaledImage.Scale}.{ extension}", scaledImage.Data); } else { storageResult = await storage.UpdateAsync(name, $"{scaledImage.Scale}.{ extension}", scaledImage.Data); } if (storageResult == true) { scaledImages.Add($"{name}/{scaledImage.Scale}.{extension}"); } else { _logger.LogDebug($"image {name}/{scaledImage.Scale}.{extension} was not added!"); } } return(new AddImageCommandResult($"{name}", scaledImages)); } catch (Exception ex) { _logger.LogError(ex, "add image failed!"); return(null); } }
public override Task OnConnectedAsync() { Storage storageToRegister; string clientConnectionId = Context.ConnectionId; var headers = Context.GetHttpContext().Request.Headers; // Check if this storage is already connected. string storageName = headers["x-andyx-storage-name"].ToString(); string agentId = headers["x-andyx-storage-agent-id"].ToString(); string username = headers["x-andyx-storage-username"].ToString(); string password = headers["x-andyx-storage-password"].ToString(); logger.LogInformation($"Storage '{storageName}' with agent id '{agentId}' requested connection"); if (username != credentialsConfiguration.Username || password != credentialsConfiguration.Password) { logger.LogInformation($"Storage '{storageName}' with agent id '{agentId}' can not connect, credentials are not valid"); return(base.OnDisconnectedAsync(new Exception($"Check username and password, can not connect to this node"))); } if (storageHubRepository.GetStorageByName(storageName) == null) { StorageStatus storageStatus; var stroageParsed = Enum.TryParse <StorageStatus>(headers["x-andyx-storage-status"].ToString(), true, out storageStatus); if (stroageParsed != true) { storageStatus = StorageStatus.Blocked; return(base.OnDisconnectedAsync(new Exception($"Invalid status code '{headers["x-andyx-storage-status"]}'"))); } int agentMaxValue = Convert.ToInt32(headers["x-andyx-storage-agent-max"].ToString()); int agentMinValue = Convert.ToInt32(headers["x-andyx-storage-agent-min"].ToString()); bool isLoadBalanced = Convert.ToBoolean(headers["x-andyx-storage-agent-loadbalanced"].ToString()); storageToRegister = storageFactory.CreateStorage(storageName, storageStatus, isLoadBalanced, agentMaxValue, agentMinValue); storageHubRepository.AddStorage(storageName, storageToRegister); } storageToRegister = storageHubRepository.GetStorageByName(headers["x-andyx-storage-name"].ToString()); if (storageToRegister.Agents.Count - 1 == storageToRegister.AgentMaxNumber) { return(base.OnDisconnectedAsync(new Exception($"There are '{storageToRegister.AgentMaxNumber}' agents connected, connection refused"))); } Agent agentToRegister = agentFactory.CreateAgent(agentId, clientConnectionId, $"{storageName}-{Guid.NewGuid()}"); storageHubRepository.AddAgent(storageName, clientConnectionId, agentToRegister); Clients.Caller.StorageConnected(new Model.Storages.Events.Agents.AgentConnectedDetails() { AgentId = agentToRegister.AgentId, Agent = agentToRegister.AgentName, // Send online state of tenants to storage. Tenants = tenantMemoryRepository.GetTenants() }); logger.LogInformation($"Storage '{storageName}' with agent id '{agentId}' is connected"); return(base.OnConnectedAsync()); }
private async Task <bool> ExistsAsync(string key) { var storage = _storageFactory.CreateStorage <MicroUrlEntity>(); return(await storage.ExistsAsync(_keyFactory.CreateFromString(key))); }