public async Task <ActionResult <PDQHashCalculation> > GetHash([FromForm, SwaggerParameter("File for upload (named in form as \"file\"", Required = true)] IFormFile file) { var tempFile = Path.GetTempFileName(); try { using (var stream = new FileStream(tempFile, FileMode.Create)) { await file.CopyToAsync(stream); } return(new ActionResult <PDQHashCalculation>(_wrapper.GetHash(tempFile))); } catch (Exception e) { throw e; } finally { if (System.IO.File.Exists(tempFile)) { System.IO.File.Delete(tempFile); } } #pragma warning restore 1591 }
/// <summary> /// Run worker. /// </summary> /// <param name="ct">CancellationToken. </param> /// <returns>Task</returns> private async Task RunWorker(CancellationToken ct) { while (!ct.IsCancellationRequested) { var tasking = await _hashRequestQueue.DequeueAsync(ct); if (tasking != null) { var tempFile = Path.GetTempFileName(); try { File.WriteAllBytes(tempFile, tasking.Item1); await _hubContext.Clients.Client(tasking.Item3).SendAsync("HashResult", new HashResult() { Id = tasking.Item2, Result = _wrapper.GetHash(tempFile) }, ct); // Console.WriteLine($"Response sent to client {tasking.Item3}"); } catch (Exception e) { Console.WriteLine(e); //connection probably lost - need to add some proper catching here } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } } else { await Task.Delay(100, ct); } } }