-
Notifications
You must be signed in to change notification settings - Fork 17
/
ScannerController.cs
72 lines (63 loc) · 2.28 KB
/
ScannerController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DNTScanner.ASPNETCoreApp.Hubs;
using DNTScanner.ASPNETCoreApp.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace DNTScanner.ASPNETCoreApp.Controllers
{
public class ScannerController : Controller
{
private readonly IHubContext<ScannerHub> _scannerHubContext;
private readonly IWebHostEnvironment _environment;
public ScannerController(IHubContext<ScannerHub> scannerHubContext, IWebHostEnvironment environment)
{
_scannerHubContext = scannerHubContext;
_environment = environment;
}
public IActionResult Index()
{
var model = new NewScanViewModel();
return View(model);
}
[HttpPost]
public async Task<IActionResult> DetectScanner()
{
await _scannerHubContext.Clients.All.SendAsync("DetectScanner", "Find First Scanner");
return Ok();
}
[HttpPost]
public async Task<IActionResult> DoScan([FromForm] NewScanViewModel model)
{
await _scannerHubContext.Clients.All.SendAsync("DoScan", model);
return Ok();
}
[HttpPost]
public async Task<IActionResult> GetScannedImages(IList<IFormFile> files)
{
const string folder = "uploads";
var uploadsRootFolder = Path.Combine(_environment.WebRootPath, folder);
if (!Directory.Exists(uploadsRootFolder))
{
Directory.CreateDirectory(uploadsRootFolder);
}
foreach (var file in files)
{
if (file == null || file.Length == 0)
{
continue;
}
var filePath = Path.Combine(uploadsRootFolder, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
await _scannerHubContext.Clients.All.SendAsync("OnScannedImagesReceived", $"/{folder}/{file.FileName}");
}
}
return Ok();
}
}
}