private bool UploadAction(string filePath) { var uploadParams = new ImageUploadParams { File = new FileDescription(filePath), Colors = true, Backup = true, AccessMode = "public", UseFilename = true }; var result = _cloudinary.Upload(uploadParams); if (result.Error != null) { FailedUploads.Enqueue(filePath); _logger.LogWarning($"Upload failed for {filePath} at {DateTime.Now.ToLongDateString()}"); return(false); } UploadedImages.Add(filePath); PhotosUrls.Add(result.SecureUri.AbsoluteUri); _logger.LogInformation($"Image with id: {result.PublicId} was uploaded at {DateTime.Now.ToLongDateString()} with accesible url: {result.SecureUri.AbsoluteUri}"); return(true); }
private async Task TryToUploadFailed() { await Task.Run(async() => { while (FailedUploads.Any()) { FailedUploads.TryDequeue(out string current); var success = UploadAction(current); if (success) { _logger.LogInformation($"Uploaded {current} at {DateTime.Now.ToLongDateString()} from failed uploads"); File.Delete(current); } else { FailedUploads.Enqueue(current); } } await Task.CompletedTask; }); // awaiting 5 mins before we try again Thread.Sleep(DelayAwait); }