public async Task <IActionResult> LoadPhoto_V1_0( ApiVersion apiVersion, Guid id, [FromForm] O2EvEventPhotoDto o2EvEventPhotoDto) { var existEvent = await _eventsBaseRepository.GetAsync(id); if (existEvent == null) { return(BadRequest()); } var o2EvPhoto = await PreparePhoto(existEvent, o2EvEventPhotoDto); o2EvPhoto.IsMain = true; var updateEvent = await _eventsBaseRepository.LoadPhoto(existEvent, o2EvPhoto); if (updateEvent == null) { return(StatusCode(500)); } var eventsToReturn = _mapper.Map <O2EvEventForListDto>(existEvent); return(CreatedAtAction(nameof(Get_V1_0), new { id = eventsToReturn.Id, actualInfo = false, v = apiVersion.ToString() }, eventsToReturn)); }
private static async Task <O2EvPhoto> PreparePhoto(O2EvEvent existEvent, O2EvEventPhotoDto o2EvEventPhotoDto = null) { const string notImage = "not_image.jpg"; const string path = "Files/" + notImage; var o2EvPhoto = new O2EvPhoto(); //load default photo if (o2EvEventPhotoDto == null) { if (!System.IO.File.Exists(path)) { throw new Exception("File not found - " + path); } using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { o2EvPhoto.FileName = existEvent.Id.ToString() + '_' + DateTime.Now.ConvertToUnixTime() + Path.GetExtension(notImage).ToLower(); o2EvPhoto.Url = await AzureBlobHelper.UploadFileToStorage(stream, fileName : o2EvPhoto.FileName, TypeTable.Events); o2EvPhoto.IsMain = true; return(o2EvPhoto); } } //prepare file var file = o2EvEventPhotoDto.File; if (file.Length > 0) { using (Stream stream = file.OpenReadStream()) { o2EvPhoto.Url = await AzureBlobHelper.UploadFileToStorage(stream, existEvent.Id.ToString() + '_' + DateTime.Now.ConvertToUnixTime() + Path.GetExtension(notImage).ToLower(), TypeTable.Events); return(o2EvPhoto); } } throw new Exception("File is empty"); }