public async Task ImportPhotoAsync(ImportPhotoModel importModel) { var photo = new Photo { Title = importModel.Title, Description = importModel.Description, PhotoDate = importModel.PhotoDate, UploadDate = importModel.UploadDate, Author = importModel.Author, //Filename = // Set below }; photo.UserId = await GetOrCreateUserAsync(importModel.CollectionOf); photo.AlbumId = await GetOrCreateAlbumAsync(photo.UserId, importModel.Album); var location = await GetOrCreateLocationAsync(importModel.Location); photo.LocationId = location?.Id; if (importModel.Locomotives?.Any() ?? false) { foreach (var locoModel in importModel.Locomotives) { var locomotive = await GetOrCreateLocomotiveAsync(locoModel); if (locomotive != null) { var photoLocomotive = new PhotoLocomotive { Photo = photo, LocomotiveId = locomotive.Id }; photo.Locomotives.Add(photoLocomotive); } } } if (!importModel.ImageFileUrl.StartsWith("http")) { // Assume the photo is on the same domain as RRPA importModel.ImageFileUrl = $"http://rrpicturearchives.net/{importModel.ImageFileUrl}"; } using (var httpClient = _httpClientFactory.CreateClient()) { var imageResponse = await httpClient.GetAsync(importModel.ImageFileUrl); if (!imageResponse.IsSuccessStatusCode) { _logger.LogError($"Could not retrieve image from {importModel.ImageFileUrl}. Will not save photo."); throw new Exception($"Error retrieving image file from '{importModel.ImageFileUrl}' to save"); } var imageBytes = await imageResponse.Content.ReadAsByteArrayAsync(); var contentType = imageResponse.Content.Headers.ContentType.MediaType; var origFileName = Path.GetFileName(importModel.ImageFileUrl); photo.Filename = await _photoService.SavePhotoFileAsync(photo.AlbumId, imageBytes, origFileName, contentType); } // TODO: Add photo categories await _photoService.CreateAsync(photo); }
[AllowAnonymous] // TODO: Remove public async Task <ActionResult> Import([FromBody] ImportPhotoModel model) { await _importPhotoService.ImportPhotoAsync(model); return(Ok()); }