public async Task <ActionResult <HRSubmitPictureOutputDto> > Post([FromBody] FileToCreateDto theFile) { //TODO move in a specific serviec, controller is doing too much stuffs! if (theFile == null || theFile.SubmittedPicture == null || theFile.SubmittedPicture.Id == null || theFile.SubmittedPicture.Id == System.Guid.Empty) { return(new StatusCodeResult(StatusCodes.Status400BadRequest)); } try { //1- using var uploadTask = _storageService.UploadAsync(theFile); await uploadTask; if (uploadTask.IsCompletedSuccessfully) { //2- HRSubmitPictureInputDto pictureToSubmit = new HRSubmitPictureInputDto(); pictureToSubmit.VernacularName = theFile.SubmittedPicture?.VernacularName; pictureToSubmit.SourceType = theFile.SubmittedPicture?.SourceType; pictureToSubmit.Id = theFile.SubmittedPicture.Id; pictureToSubmit.GenderType = theFile.SubmittedPicture?.GenderType; pictureToSubmit.Credit = theFile.SubmittedPicture?.Credit; pictureToSubmit.Comment = theFile.SubmittedPicture?.Comment; pictureToSubmit.AgeType = theFile.SubmittedPicture?.AgeType; pictureToSubmit.ThumbnailUrl = theFile.SubmittedPicture?.ThumbnailUrl; pictureToSubmit.FullImageUrl = uploadTask.Result; using var subTask = _birdsSubmissionService.UpdatePictureDataAsync(pictureToSubmit); await subTask; if (subTask.IsCompletedSuccessfully) { return(Ok(subTask.Result)); } return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } else { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } } catch { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
/// <summary> /// 1- upload in Blob storage /// 2- queue new message /// 3- Temporary cheat, wake up listening Azure functions (fire and forget) /// </summary> /// <param name="theFile"></param> /// <returns></returns> public async Task <string> UploadAsync(FileToCreateDto theFile) { if (theFile == null || String.IsNullOrEmpty(theFile.FileAsBase64) || theFile.SubmittedPicture == null || theFile.SubmittedPicture.Id == Guid.Empty) { throw new ArgumentNullException(); } //1- using var blobTask = UploadInBlobAsync(theFile); await blobTask; if (blobTask.IsCompletedSuccessfully) { theFile.SubmittedPicture.FullImageUrl = blobTask.Result; //2- using var getTask = _repo.GetSubmittedPicturesByIDAsync(theFile.SubmittedPicture.Id.ToString()); await getTask; if (getTask.IsCompletedSuccessfully) { var itemToQueue = _mapper.Map <HRSubmitPictureListItemDto>(getTask.Result); itemToQueue.FullImageUrl = blobTask.Result; using var queueTask = _queueService.OnNewImageAsync(itemToQueue); await queueTask; if (queueTask.IsCompletedSuccessfully) { //3- WakeUpAzureFunction(); return(blobTask.Result); } else { throw new Exception("UploadInBlobAsync fail"); } } else { throw new Exception("_repo.GetSubmittedPicturesByIDAsync fail"); } } else { throw new Exception("UploadInBlobAsync fail"); } }
/// <summary> /// /// </summary> /// <param name="theFile"></param> /// <returns></returns> private async Task <String> UploadInBlobAsync(FileToCreateDto theFile) { FileToCreate fToCreate = _mapper.Map <FileToCreate>(theFile); fToCreate.FileAsBase64 = _picFormatter.CleanData(theFile.FileAsBase64); fToCreate.FileAsByteArray = Convert.FromBase64String(fToCreate.FileAsBase64); var directoryTask = _picFormatter.GetPathAsync(fToCreate); await directoryTask; if (directoryTask.IsCompletedSuccessfully) { using var fs = new MemoryStream(fToCreate.FileAsByteArray); String rootUrl = _config.Value?.BlobRootURI; String blobPath = rootUrl + directoryTask.Result + theFile.SubmittedPicture.Id.ToString() + "." + "jpeg"; Uri blobUri = new Uri(blobPath); // Create StorageSharedKeyCredentials object by reading // the values from the configuration (appsettings.json) String credNameValue = _config?.Value?.StorageSharedKeyCredentialName; String pwd = _config?.Value?.Password; StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(credNameValue, pwd); // Create the blob client. BlobClient blobClient = new BlobClient(blobUri, storageCredentials); // Upload the file var uploadTask = blobClient.UploadAsync(fs); await uploadTask; if (uploadTask.IsCompletedSuccessfully) { return(blobPath); } else { throw new Exception("blobClient.UploadAsync failed."); } } else { throw new Exception("_picFormatter.GetPathAsync failed."); } }