using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; namespace IoTDameon.Controllers { public class HomeController : Controller { private readonly IWebHostEnvironment _env; public HomeController(IWebHostEnvironment env) { this._env = env; } public IActionResult Index() { var path = Path.Combine(this._env.WebRootPath, "upload"); return View(model: path); } public IActionResult GetVersion() { return Content(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); } private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { throw new System.NotImplementedException(); } public IActionResult Upload() { if (Request.Form.Files != null && Request.Form.Files.Count > 0) { var file = Request.Form.Files[0]; using Stream stream = file.OpenReadStream(); var phicyPath = Path.Combine(this._env.WebRootPath, "upload"); Directory.CreateDirectory(phicyPath); var name = file.FileName; var fullName = Path.Combine(phicyPath, name); if (System.IO.File.Exists(fullName)) { System.IO.File.Delete(fullName); } using (FileStream fs = System.IO.File.Create(fullName)) { file.CopyTo(fs); } var ext = Path.GetExtension(fullName); if (ext == "zip") { var zipDirectory = Path.Combine(phicyPath, Path.GetFileName(fullName)); if (!Directory.Exists(zipDirectory)) { Directory.CreateDirectory(zipDirectory); } ZipFile.ExtractToDirectory(fullName, zipDirectory); } } return RedirectToAction("Index"); } } }