public async Task <IActionResult> GetPhotoAsync(int id) { var photo = await _photoService.GetAsync(id); var user = await _userService.GetAsync(photo.UserId); var httpClient = new HttpClient(); var config = new DropboxClientConfig("PhotoMap") { HttpClient = httpClient }; var dropboxClient = new DropboxClient(user.DropboxAccessToken, config); var fileMetadata = await dropboxClient.Files.DownloadAsync(photo.Path); var fileContents = await fileMetadata.GetContentAsByteArrayAsync(); if (photo.FileName.ToUpper().EndsWith("HEIC")) { var commandId = Guid.NewGuid(); var convertImageCommand = new ConvertImageEvent { Id = commandId, FileContents = fileContents }; _messageSender.Send(convertImageCommand); const int maxTimeout = 5000; int waitTime = 0; byte[] convertedBytes; do { await Task.Delay(1000); convertedBytes = _convertedImageHolder.Get(commandId); waitTime += 1000; } while (waitTime <= maxTimeout || convertedBytes == null); if (convertedBytes != null) { return(new FileContentResult(convertedBytes, "image/jpg")); } return(BadRequest()); } return(new FileContentResult(fileContents, "image/jpg")); }
public async Task <IActionResult> GetPhotoAsync(int id) { var photo = await _photoService.GetAsync(id); var user = await _userService.GetAsync(photo.UserId); var httpClient = new HttpClient(); var yandexDiskApiClient = new ApiClient(user.YandexDiskAccessToken, httpClient); var downloadUrl = await yandexDiskApiClient.GetDownloadUrlAsync(photo.Path, new CancellationToken()); var bytes = await httpClient.GetByteArrayAsync(downloadUrl.Href); if (photo.FileName.ToUpper().EndsWith("HEIC")) { var commandId = Guid.NewGuid(); var convertImageCommand = new ConvertImageEvent { Id = commandId, FileContents = bytes }; _messageSender.Send(convertImageCommand); const int maxTimeout = 5000; int waitTime = 0; byte[] convertedBytes; do { await Task.Delay(1000); convertedBytes = _convertedImageHolder.Get(commandId); waitTime += 1000; } while (waitTime <= maxTimeout || convertedBytes == null); if (convertedBytes != null) { return(new FileContentResult(convertedBytes, "image/jpg")); } return(BadRequest()); } return(new FileContentResult(bytes, "image/jpg")); }