[13860] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[13862] | 22 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
| 23 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
| 24 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using Microsoft.AspNetCore.Hosting;
|
---|
| 28 | using Microsoft.AspNetCore.Http;
|
---|
[13860] | 29 | using Microsoft.AspNetCore.Mvc;
|
---|
[13862] | 30 | using Microsoft.Net.Http.Headers;
|
---|
[13656] | 31 | using System;
|
---|
| 32 | using System.Collections.Generic;
|
---|
[13862] | 33 | using System.IO;
|
---|
[13656] | 34 | using System.Linq;
|
---|
[13862] | 35 | using System.Threading;
|
---|
[13656] | 36 | using System.Threading.Tasks;
|
---|
[13860] | 37 |
|
---|
[13656] | 38 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
| 39 | {
|
---|
[13733] | 40 | /// <summary>
|
---|
| 41 | /// Controller for everything Job related (includes uploads and uploader)
|
---|
| 42 | /// </summary>
|
---|
[13656] | 43 | public class JobController : Controller
|
---|
| 44 | {
|
---|
[13739] | 45 | private WebLoginService weblog;
|
---|
| 46 |
|
---|
| 47 | private HiveServiceLocatorWeb serviceLocator;
|
---|
| 48 | private HiveServiceClient serviceClient;
|
---|
| 49 | private HiveClientWeb clientWeb;
|
---|
| 50 | private Guid userId;
|
---|
| 51 |
|
---|
[13847] | 52 |
|
---|
[13656] | 53 | private JobViewModel vm;
|
---|
[13847] | 54 | private UploadedJobViewModel upper;
|
---|
[13656] | 55 | private IHostingEnvironment _environment;
|
---|
| 56 |
|
---|
[13739] | 57 |
|
---|
| 58 |
|
---|
[13656] | 59 | public JobController(IHostingEnvironment env)
|
---|
| 60 | {
|
---|
[13739] | 61 | weblog = WebLoginService.Instance;
|
---|
[13854] | 62 |
|
---|
[13739] | 63 | _environment = env;
|
---|
| 64 | }
|
---|
[13656] | 65 |
|
---|
[13739] | 66 | /// <summary>
|
---|
| 67 | /// initializes the connection for the right user. Not constructor because httpcontext is needed.
|
---|
| 68 | /// </summary>
|
---|
[13754] | 69 | private bool init()
|
---|
[13739] | 70 | {
|
---|
| 71 | var u = HttpContext.Session.GetString("UserId");
|
---|
[13854] | 72 |
|
---|
[13739] | 73 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
| 74 | {
|
---|
[13754] | 75 | return false;
|
---|
[13739] | 76 | }
|
---|
| 77 | else
|
---|
| 78 | {
|
---|
| 79 | userId = Guid.Parse(u);
|
---|
[13847] | 80 | vm = new JobViewModel(weblog.getCurrentUser(userId));
|
---|
| 81 | upper = new UploadedJobViewModel(weblog.getCurrentUser(userId));
|
---|
[13854] | 82 | try
|
---|
| 83 | {
|
---|
| 84 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
| 85 | serviceClient = serviceLocator.getHiveServiceClient();
|
---|
| 86 | clientWeb = weblog.getClientWeb(userId);
|
---|
| 87 | if (serviceLocator != null && serviceClient != null && clientWeb != null)
|
---|
| 88 | return serviceLocator.CheckLogin();
|
---|
| 89 | }
|
---|
| 90 | catch (NullReferenceException)
|
---|
| 91 | {
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
[13739] | 94 | }
|
---|
[13854] | 95 | return false;
|
---|
[13739] | 96 | }
|
---|
[13656] | 97 |
|
---|
[13689] | 98 | #region Jobs
|
---|
[13733] | 99 | /// <summary>
|
---|
| 100 | /// initial job page, shows all jobs
|
---|
| 101 | /// </summary>
|
---|
| 102 | /// <returns></returns>
|
---|
[13656] | 103 | public IActionResult Index()
|
---|
| 104 | {
|
---|
[13754] | 105 | if (init())
|
---|
[13656] | 106 | {
|
---|
[13739] | 107 | clientWeb.Refresh();
|
---|
| 108 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
[13754] | 109 |
|
---|
| 110 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
| 111 | ViewBag.Title = "Jobs";
|
---|
[13860] | 112 | return View("Index", vm);
|
---|
[13656] | 113 | }
|
---|
[13754] | 114 | else
|
---|
[13656] | 115 | {
|
---|
[13754] | 116 | return RedirectToAction("Index", "Home");
|
---|
[13656] | 117 | }
|
---|
| 118 | }
|
---|
[13733] | 119 | /// <summary>
|
---|
| 120 | /// Initial page and a selected job
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <param name="id">Job id selected</param>
|
---|
| 123 | /// <returns></returns>
|
---|
[13656] | 124 | public IActionResult Selected(Guid id)
|
---|
| 125 | {
|
---|
[13754] | 126 | if (init())
|
---|
[13656] | 127 | {
|
---|
[13739] | 128 | clientWeb.Refresh();
|
---|
| 129 |
|
---|
| 130 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
| 131 | foreach (var j in vm.userJobs)
|
---|
[13689] | 132 | {
|
---|
[13739] | 133 | if (j.Id == id)
|
---|
[13712] | 134 | {
|
---|
| 135 | vm.selectedJob = j;
|
---|
| 136 | }
|
---|
[13689] | 137 | }
|
---|
[13739] | 138 | //vm.selectedJob.RefreshAutomatically = true;
|
---|
| 139 | clientWeb.LoadJob(vm.selectedJob);
|
---|
[13841] | 140 | weblog.getJobOpener(userId).NewModel();
|
---|
| 141 | weblog.getJobOpener(userId).Job = vm.selectedJob;
|
---|
[13712] | 142 | ViewBag.Title = vm.selectedJob.Job.Name + " - Jobs";
|
---|
[13739] | 143 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13656] | 144 | return View("Index", vm);
|
---|
| 145 | }
|
---|
| 146 | else
|
---|
| 147 | {
|
---|
| 148 | return RedirectToAction("Index", "Home");
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[13733] | 151 | /// <summary>
|
---|
| 152 | /// Deletes a job
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <param name="id">Job id</param>
|
---|
| 155 | /// <returns></returns>
|
---|
| 156 | public IActionResult Delete(Guid id)
|
---|
[13656] | 157 | {
|
---|
[13754] | 158 | if (init())
|
---|
[13656] | 159 | {
|
---|
| 160 |
|
---|
[13739] | 161 | vm.message = serviceClient.GetJob(id).Name + " deleted";
|
---|
| 162 | serviceClient.DeleteJob(id);
|
---|
| 163 | clientWeb.Refresh();
|
---|
[13712] | 164 |
|
---|
[13739] | 165 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
[13740] | 166 | ViewBag.Title = vm.message + " - Jobs";
|
---|
[13656] | 167 | return View("Index", vm);
|
---|
| 168 | }
|
---|
| 169 | else
|
---|
| 170 | {
|
---|
| 171 | return RedirectToAction("Index", "Home");
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
[13689] | 174 | #endregion
|
---|
[13656] | 175 |
|
---|
[13689] | 176 | #region Uploads
|
---|
[13733] | 177 | /// <summary>
|
---|
| 178 | /// Shows the uploaded directories
|
---|
| 179 | /// </summary>
|
---|
| 180 | /// <returns></returns>
|
---|
[13689] | 181 | public IActionResult Uploads()
|
---|
[13656] | 182 | {
|
---|
[13754] | 183 | if (init())
|
---|
[13656] | 184 | {
|
---|
[13689] | 185 | fillUploadsPaths(upper, -1);
|
---|
[13739] | 186 | ViewBag.Name = serviceClient.ClientCredentials.UserName.UserName;
|
---|
[13740] | 187 | ViewBag.Title = "Uploads";
|
---|
[13841] | 188 | //Recently opened
|
---|
| 189 | if (weblog.getFileOpener(userId).Job != null)
|
---|
| 190 | ViewBag.active = true;
|
---|
[13689] | 191 | return View("Uploads", upper);
|
---|
| 192 | }
|
---|
| 193 | else
|
---|
| 194 | {
|
---|
| 195 | return RedirectToAction("Index", "Home");
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
[13733] | 198 | /// <summary>
|
---|
| 199 | /// //Shows content of selected dir
|
---|
| 200 | /// </summary>
|
---|
| 201 | /// <param name="index">Array index selected directory</param>
|
---|
| 202 | /// <returns></returns>
|
---|
[13689] | 203 | public IActionResult UploadDir(int index)
|
---|
| 204 | {
|
---|
[13754] | 205 | if (init())
|
---|
[13689] | 206 | {
|
---|
| 207 | fillUploadsPaths(upper, index);
|
---|
[13754] | 208 | if (index != -1)
|
---|
[13740] | 209 | ViewBag.Title = upper.DisplayDatePaths[index] + " - Uploads";
|
---|
| 210 | else
|
---|
| 211 | ViewBag.Title = "Add files - Uploads";
|
---|
[13841] | 212 | //Recently opened
|
---|
| 213 | if (weblog.getFileOpener(userId).Job != null)
|
---|
| 214 | ViewBag.active = true;
|
---|
[13689] | 215 | return View("Uploads", upper);
|
---|
| 216 | }
|
---|
| 217 | else
|
---|
| 218 | {
|
---|
| 219 | return RedirectToAction("Index", "Home");
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
[13847] | 222 | [HttpPost]
|
---|
| 223 | public IActionResult changeDirName(string olddir, string dirname)
|
---|
| 224 | {
|
---|
| 225 | if (init())
|
---|
| 226 | {
|
---|
| 227 | fillUploadsPaths(upper, -1);
|
---|
| 228 | var ind = upper.DisplayDatePaths.IndexOf(olddir);
|
---|
[13854] | 229 | try
|
---|
| 230 | {
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | var start = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName, dirname);
|
---|
[13847] | 234 | if (Directory.Exists(start))
|
---|
| 235 | {
|
---|
| 236 | var files = Directory.GetFiles(upper.FullDatePaths[ind]);
|
---|
[13854] | 237 | foreach (var f in files)
|
---|
| 238 | {
|
---|
[13847] | 239 | var target = start + "\\" + f.Split('\\').Last();
|
---|
| 240 | if (System.IO.File.Exists(target))
|
---|
| 241 | System.IO.File.Delete(target);
|
---|
| 242 | System.IO.File.Move(f, target);
|
---|
[13854] | 243 | }
|
---|
[13847] | 244 | }
|
---|
| 245 | else
|
---|
| 246 | {
|
---|
| 247 | Directory.Move(upper.FullDatePaths[ind], start);
|
---|
| 248 | }
|
---|
[13854] | 249 |
|
---|
| 250 | upper.clear();
|
---|
| 251 | fillUploadsPaths(upper, -1);
|
---|
| 252 | ind = upper.DisplayDatePaths.IndexOf(dirname);
|
---|
| 253 | upper.clear();
|
---|
| 254 | return RedirectToAction("UploadDir", new { index = ind });
|
---|
| 255 | }
|
---|
| 256 | catch (IOException e)
|
---|
[13847] | 257 | {
|
---|
| 258 | upper.clear();
|
---|
| 259 | fillUploadsPaths(upper, ind);
|
---|
| 260 | upper.message = "Error in new directory name. Try again and don't use any special signs";
|
---|
[13854] | 261 | ViewBag.Title = "Error renaming - " + upper.DisplayDatePaths[ind] + " - Uploads";
|
---|
[13847] | 262 | if (weblog.getFileOpener(userId).Job != null)
|
---|
| 263 | ViewBag.active = true;
|
---|
| 264 | return View("Uploads", upper);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | else
|
---|
| 268 | {
|
---|
| 269 | return RedirectToAction("Index", "Home");
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
[13733] | 272 | /// <summary>
|
---|
| 273 | /// Loads all the paths from the selected uploads folder
|
---|
| 274 | /// </summary>
|
---|
| 275 | /// <param name="vm">Viewmodel to save every directory path</param>
|
---|
| 276 | /// <param name="index">Index selected directory</param>
|
---|
[13689] | 277 | private void fillUploadsPaths(UploadedJobViewModel vm, int index)
|
---|
| 278 | {
|
---|
[13854] | 279 |
|
---|
[13689] | 280 | var tempdex = index; //Fix when maps gets deleted
|
---|
[13739] | 281 | var start = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName);
|
---|
[13847] | 282 | try
|
---|
| 283 | {
|
---|
| 284 | Directory.GetDirectories(start);
|
---|
| 285 | }
|
---|
[13854] | 286 | catch (DirectoryNotFoundException e)
|
---|
[13847] | 287 | {
|
---|
| 288 | Directory.CreateDirectory(start);
|
---|
| 289 | }
|
---|
[13689] | 290 | var dirs = Directory.GetDirectories(start);
|
---|
| 291 | foreach (string dir in dirs)
|
---|
| 292 | {
|
---|
| 293 | if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
|
---|
| 294 | {
|
---|
| 295 | Directory.Delete(dir, false);
|
---|
| 296 | tempdex = -1;
|
---|
| 297 | }
|
---|
[13754] | 298 | else
|
---|
| 299 | {
|
---|
[13689] | 300 | vm.FullDatePaths.Add(dir);
|
---|
| 301 | var temp = dir.Split('\\');
|
---|
| 302 | vm.DisplayDatePaths.Add(temp[temp.Length - 1]);
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | if (tempdex != -1)
|
---|
| 306 | {
|
---|
| 307 | vm.SelectedIndex = tempdex;
|
---|
| 308 | dirs = Directory.GetFiles(vm.FullDatePaths[tempdex]);
|
---|
| 309 | foreach (string dir in dirs)
|
---|
| 310 | {
|
---|
| 311 | vm.FullFilesPaths.Add(dir);
|
---|
| 312 | var temp = dir.Split('\\');
|
---|
| 313 | vm.DisplayFilesPaths.Add(temp[temp.Length - 1]);
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
[13733] | 317 | /// <summary>
|
---|
| 318 | /// Deletes a file
|
---|
| 319 | /// </summary>
|
---|
| 320 | /// <param name="index">Index directory</param>
|
---|
| 321 | /// <param name="filedex">Index file to delete</param>
|
---|
| 322 | /// <returns></returns>
|
---|
[13689] | 323 | public IActionResult DeleteFile(int index, int filedex)
|
---|
| 324 | {
|
---|
[13754] | 325 | if (init())
|
---|
[13689] | 326 | {
|
---|
| 327 | fillUploadsPaths(upper, index);
|
---|
| 328 | System.IO.File.Delete(upper.FullFilesPaths[filedex]);
|
---|
| 329 | var message = upper.DisplayFilesPaths[filedex] + " has been deleted";
|
---|
| 330 |
|
---|
[13847] | 331 | upper = new UploadedJobViewModel(weblog.getCurrentUser(userId));
|
---|
[13689] | 332 | fillUploadsPaths(upper, index);
|
---|
| 333 | upper.message = message;
|
---|
[13841] | 334 | //recently opened
|
---|
| 335 | if (weblog.getFileOpener(userId).Job != null)
|
---|
| 336 | ViewBag.active = true;
|
---|
[13740] | 337 | ViewBag.Title = "File deleted - Uploads";
|
---|
[13689] | 338 |
|
---|
| 339 | return View("Uploads", upper);
|
---|
| 340 | }
|
---|
| 341 | else
|
---|
| 342 | {
|
---|
| 343 | return RedirectToAction("Index", "Home");
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
[13733] | 346 | /// <summary>
|
---|
| 347 | /// Opens a selected file
|
---|
| 348 | /// </summary>
|
---|
| 349 | /// <param name="index">Index directory</param>
|
---|
| 350 | /// <param name="filedex">Index selected file</param>
|
---|
| 351 | /// <returns></returns>
|
---|
[13689] | 352 | public IActionResult OpenFile(int index, int filedex)
|
---|
| 353 | {
|
---|
[13754] | 354 | if (init())
|
---|
[13689] | 355 | {
|
---|
| 356 | fillUploadsPaths(upper, index);
|
---|
| 357 |
|
---|
[13739] | 358 | var serve = weblog.getFileOpener(userId);
|
---|
[13689] | 359 | serve.NewModel();
|
---|
[13860] | 360 | serve.env = (Microsoft.AspNetCore.Hosting.IHostingEnvironment)_environment;
|
---|
[13847] | 361 | serve.vm.directories = upper.DisplayDatePaths;
|
---|
[13689] | 362 | var ioptimizer = ContentManager.Load(upper.FullFilesPaths[filedex]);
|
---|
| 363 |
|
---|
| 364 | serve.vm.SelectedTask = new OptimizerHiveTask((IOptimizer)ioptimizer);
|
---|
| 365 | if (serve.vm.SelectedTask.ItemTask.Item is IAlgorithm)
|
---|
| 366 | {
|
---|
| 367 | serve.vm.SelectedAlgorithm = (IAlgorithm)serve.vm.SelectedTask.ItemTask.Item;
|
---|
| 368 | }
|
---|
| 369 | else if (serve.vm.SelectedTask.ItemTask.Item is BatchRun)
|
---|
| 370 | {
|
---|
| 371 | serve.vm.SelectedBatchRun = (BatchRun)serve.vm.SelectedTask.ItemTask.Item;
|
---|
| 372 | }
|
---|
| 373 | else if (serve.vm.SelectedTask.ItemTask.Item is Experiment)
|
---|
| 374 | {
|
---|
| 375 | serve.vm.SelectedExperiment = (Experiment)serve.vm.SelectedTask.ItemTask.Item;
|
---|
| 376 | }
|
---|
[13696] | 377 | serve.setTasks();
|
---|
[13735] | 378 | ViewBag.JobsCount = serve.Job.Job.JobCount;
|
---|
[13740] | 379 | ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Uploads";
|
---|
[13739] | 380 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13689] | 381 | return View("OpenFile", serve.vm);
|
---|
| 382 | }
|
---|
| 383 | else
|
---|
| 384 | {
|
---|
| 385 | return RedirectToAction("Index", "Home");
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
[13733] | 388 | /// <summary>
|
---|
[13841] | 389 | /// Opens the last opened file in the fileopening service
|
---|
| 390 | /// </summary>
|
---|
| 391 | /// <returns></returns>
|
---|
| 392 | public IActionResult OpenRecent()
|
---|
| 393 | {
|
---|
| 394 | if (init())
|
---|
| 395 | {
|
---|
| 396 | var serve = weblog.getFileOpener(userId);
|
---|
[13847] | 397 | fillUploadsPaths(upper, -1);
|
---|
| 398 | serve.vm.directories = upper.DisplayDatePaths;
|
---|
[13841] | 399 | ViewBag.JobsCount = serve.Job.Job.JobCount;
|
---|
| 400 | ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Uploads";
|
---|
| 401 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
| 402 | return View("OpenFile", serve.vm);
|
---|
| 403 | }
|
---|
| 404 | else
|
---|
| 405 | {
|
---|
| 406 | return RedirectToAction("Index", "Home");
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 | /// <summary>
|
---|
[13733] | 410 | /// Adds current opened file to hive, uses FileOpeningService singleton
|
---|
| 411 | /// </summary>
|
---|
| 412 | /// <returns></returns>
|
---|
[13689] | 413 | public IActionResult AddToHive()
|
---|
| 414 | {
|
---|
[13754] | 415 | if (init())
|
---|
[13689] | 416 | {
|
---|
[13739] | 417 | var job = weblog.getFileOpener(userId).AddCurrentModelToHive();
|
---|
| 418 | while (job.Progress.ProgressValue != 1)
|
---|
| 419 | { }
|
---|
[13689] | 420 |
|
---|
[13739] | 421 | Thread.Sleep(1000);
|
---|
| 422 | job.Progress.Status = "Upload finished";
|
---|
| 423 | Thread.Sleep(2000);
|
---|
| 424 | return RedirectToAction("Index", "Job");
|
---|
[13689] | 425 | }
|
---|
| 426 | else
|
---|
| 427 | {
|
---|
| 428 | return RedirectToAction("Index", "Home");
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
[13827] | 431 | [HttpPost]
|
---|
[13847] | 432 | public IActionResult saveToFile(string fname, string dname)
|
---|
[13827] | 433 | {
|
---|
| 434 | if (init())
|
---|
| 435 | {
|
---|
[13847] | 436 | weblog.getFileOpener(userId).SaveToFile(fname, dname);
|
---|
| 437 | fillUploadsPaths(upper, -1);
|
---|
| 438 | var ind = upper.DisplayDatePaths.IndexOf(dname);
|
---|
[13854] | 439 | return RedirectToAction("UploadDir", new { index = ind });
|
---|
[13827] | 440 | }
|
---|
[13841] | 441 | else
|
---|
[13827] | 442 | {
|
---|
| 443 | return RedirectToAction("Index", "Home");
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
[13689] | 446 |
|
---|
[13827] | 447 | public FileResult DownloadFile(int index, int filedex)
|
---|
| 448 | {
|
---|
| 449 | if (init())
|
---|
| 450 | {
|
---|
| 451 | fillUploadsPaths(upper, index);
|
---|
[13689] | 452 |
|
---|
[13827] | 453 | System.IO.FileInfo Dfile = new System.IO.FileInfo(upper.FullFilesPaths[filedex]);
|
---|
| 454 | var fileName = Dfile.Name;
|
---|
| 455 | byte[] fileBytes = System.IO.File.ReadAllBytes(upper.FullFilesPaths[filedex]);
|
---|
| 456 | return File(fileBytes, "application/x-msdownload", fileName);
|
---|
| 457 | }
|
---|
| 458 | else
|
---|
| 459 | {
|
---|
| 460 | return null;
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
[13689] | 463 |
|
---|
| 464 | #endregion
|
---|
| 465 |
|
---|
| 466 | #region Uploader
|
---|
[13733] | 467 |
|
---|
| 468 | /// <summary>
|
---|
| 469 | /// Uploads file onto server directory
|
---|
| 470 | /// </summary>
|
---|
| 471 | /// <param name="files">Files</param>
|
---|
| 472 | /// <param name="directory">Directory path for server</param>
|
---|
| 473 | /// <returns></returns>
|
---|
[13656] | 474 | [HttpPost]
|
---|
[13689] | 475 | public async Task<IActionResult> Uploader(ICollection<IFormFile> files, string directory)
|
---|
[13656] | 476 | {
|
---|
[13754] | 477 | if (init())
|
---|
[13656] | 478 | {
|
---|
[13754] | 479 | var uploads = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName,
|
---|
| 480 | directory);
|
---|
| 481 | Directory.CreateDirectory(uploads);
|
---|
| 482 | foreach (var file in files)
|
---|
[13656] | 483 | {
|
---|
[13754] | 484 | if (file.Length > 0)
|
---|
| 485 | {
|
---|
| 486 | var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
|
---|
[13860] | 487 | await file.CopyToAsync(new FileStream(Path.Combine(uploads, fileName),FileMode.OpenOrCreate));
|
---|
[13754] | 488 | // var ioptimizer = ContentManager.Load(Path.Combine(uploads, fileName));
|
---|
| 489 | //OptimizerHiveTask task = new OptimizerHiveTask((IOptimizer)ioptimizer);
|
---|
| 490 | //upper.Tasks.Add(task);
|
---|
| 491 | }
|
---|
[13656] | 492 | }
|
---|
[13754] | 493 | ViewBag.Title = "Upload complete - Uploads";
|
---|
| 494 | return RedirectToAction("Uploads", "Job");
|
---|
[13656] | 495 | }
|
---|
[13754] | 496 | else
|
---|
| 497 | {
|
---|
| 498 | return RedirectToAction("Index", "Home");
|
---|
| 499 | }
|
---|
[13656] | 500 | }
|
---|
[13689] | 501 | #endregion
|
---|
[13656] | 502 | }
|
---|
| 503 | }
|
---|