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 |
|
---|
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;
|
---|
29 | using Microsoft.AspNetCore.Mvc;
|
---|
30 | using Microsoft.Net.Http.Headers;
|
---|
31 | using System;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.IO;
|
---|
34 | using System.Linq;
|
---|
35 | using System.Threading;
|
---|
36 | using System.Threading.Tasks;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// Controller for everything Job related (includes uploads and uploader)
|
---|
42 | /// </summary>
|
---|
43 | public class JobController : Controller
|
---|
44 | {
|
---|
45 | private WebLoginService weblog;
|
---|
46 |
|
---|
47 | private HiveServiceLocatorWeb serviceLocator;
|
---|
48 | private HiveServiceClient serviceClient;
|
---|
49 | private HiveClientWeb clientWeb;
|
---|
50 | private Guid userId;
|
---|
51 |
|
---|
52 |
|
---|
53 | private JobViewModel vm;
|
---|
54 | private UploadedJobViewModel upper;
|
---|
55 | private IHostingEnvironment _environment;
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | public JobController(IHostingEnvironment env)
|
---|
60 | {
|
---|
61 | weblog = WebLoginService.Instance;
|
---|
62 |
|
---|
63 | _environment = env;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// initializes the connection for the right user. Not constructor because httpcontext is needed.
|
---|
68 | /// </summary>
|
---|
69 | private bool init()
|
---|
70 | {
|
---|
71 | var u = HttpContext.Session.GetString("UserId");
|
---|
72 |
|
---|
73 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
74 | {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | userId = Guid.Parse(u);
|
---|
80 | vm = new JobViewModel(weblog.getCurrentUser(userId));
|
---|
81 | upper = new UploadedJobViewModel(weblog.getCurrentUser(userId));
|
---|
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 | }
|
---|
94 | }
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region Jobs
|
---|
99 | /// <summary>
|
---|
100 | /// initial job page, shows all jobs
|
---|
101 | /// </summary>
|
---|
102 | /// <returns></returns>
|
---|
103 | public IActionResult Index()
|
---|
104 | {
|
---|
105 | if (init())
|
---|
106 | {
|
---|
107 | clientWeb.Refresh();
|
---|
108 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
109 |
|
---|
110 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
111 | ViewBag.Title = "Jobs";
|
---|
112 | return View("Index", vm);
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | return RedirectToAction("Index", "Home");
|
---|
117 | }
|
---|
118 | }
|
---|
119 | /// <summary>
|
---|
120 | /// Initial page and a selected job
|
---|
121 | /// </summary>
|
---|
122 | /// <param name="id">Job id selected</param>
|
---|
123 | /// <returns></returns>
|
---|
124 | public IActionResult Selected(Guid id)
|
---|
125 | {
|
---|
126 | if (init())
|
---|
127 | {
|
---|
128 | clientWeb.Refresh();
|
---|
129 |
|
---|
130 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
131 | foreach (var j in vm.userJobs)
|
---|
132 | {
|
---|
133 | if (j.Id == id)
|
---|
134 | {
|
---|
135 | vm.selectedJob = j;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | //vm.selectedJob.RefreshAutomatically = true;
|
---|
139 | clientWeb.LoadJob(vm.selectedJob);
|
---|
140 | weblog.getJobOpener(userId).NewModel();
|
---|
141 | weblog.getJobOpener(userId).Job = vm.selectedJob;
|
---|
142 | ViewBag.Title = vm.selectedJob.Job.Name + " - Jobs";
|
---|
143 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
144 | return View("Index", vm);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | return RedirectToAction("Index", "Home");
|
---|
149 | }
|
---|
150 | }
|
---|
151 | /// <summary>
|
---|
152 | /// Deletes a job
|
---|
153 | /// </summary>
|
---|
154 | /// <param name="id">Job id</param>
|
---|
155 | /// <returns></returns>
|
---|
156 | public IActionResult Delete(Guid id)
|
---|
157 | {
|
---|
158 | if (init())
|
---|
159 | {
|
---|
160 |
|
---|
161 | vm.message = serviceClient.GetJob(id).Name + " deleted";
|
---|
162 | serviceClient.DeleteJob(id);
|
---|
163 | clientWeb.Refresh();
|
---|
164 |
|
---|
165 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
166 | ViewBag.Title = vm.message + " - Jobs";
|
---|
167 | return View("Index", vm);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | return RedirectToAction("Index", "Home");
|
---|
172 | }
|
---|
173 | }
|
---|
174 | #endregion
|
---|
175 |
|
---|
176 | #region Uploads
|
---|
177 | /// <summary>
|
---|
178 | /// Shows the uploaded directories
|
---|
179 | /// </summary>
|
---|
180 | /// <returns></returns>
|
---|
181 | public IActionResult Uploads()
|
---|
182 | {
|
---|
183 | if (init())
|
---|
184 | {
|
---|
185 | fillUploadsPaths(upper, -1);
|
---|
186 | ViewBag.Name = serviceClient.ClientCredentials.UserName.UserName;
|
---|
187 | ViewBag.Title = "Uploads";
|
---|
188 | //Recently opened
|
---|
189 | if (weblog.getFileOpener(userId).Job != null)
|
---|
190 | ViewBag.active = true;
|
---|
191 | return View("Uploads", upper);
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | return RedirectToAction("Index", "Home");
|
---|
196 | }
|
---|
197 | }
|
---|
198 | /// <summary>
|
---|
199 | /// //Shows content of selected dir
|
---|
200 | /// </summary>
|
---|
201 | /// <param name="index">Array index selected directory</param>
|
---|
202 | /// <returns></returns>
|
---|
203 | public IActionResult UploadDir(int index)
|
---|
204 | {
|
---|
205 | if (init())
|
---|
206 | {
|
---|
207 | fillUploadsPaths(upper, index);
|
---|
208 | if (index != -1)
|
---|
209 | ViewBag.Title = upper.DisplayDatePaths[index] + " - Uploads";
|
---|
210 | else
|
---|
211 | ViewBag.Title = "Add files - Uploads";
|
---|
212 | //Recently opened
|
---|
213 | if (weblog.getFileOpener(userId).Job != null)
|
---|
214 | ViewBag.active = true;
|
---|
215 | return View("Uploads", upper);
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | return RedirectToAction("Index", "Home");
|
---|
220 | }
|
---|
221 | }
|
---|
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);
|
---|
229 | try
|
---|
230 | {
|
---|
231 |
|
---|
232 |
|
---|
233 | var start = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName, dirname);
|
---|
234 | if (Directory.Exists(start))
|
---|
235 | {
|
---|
236 | var files = Directory.GetFiles(upper.FullDatePaths[ind]);
|
---|
237 | foreach (var f in files)
|
---|
238 | {
|
---|
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);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | Directory.Move(upper.FullDatePaths[ind], start);
|
---|
248 | }
|
---|
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)
|
---|
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";
|
---|
261 | ViewBag.Title = "Error renaming - " + upper.DisplayDatePaths[ind] + " - Uploads";
|
---|
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 | }
|
---|
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>
|
---|
277 | private void fillUploadsPaths(UploadedJobViewModel vm, int index)
|
---|
278 | {
|
---|
279 |
|
---|
280 | var tempdex = index; //Fix when maps gets deleted
|
---|
281 | var start = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName);
|
---|
282 | try
|
---|
283 | {
|
---|
284 | Directory.GetDirectories(start);
|
---|
285 | }
|
---|
286 | catch (DirectoryNotFoundException e)
|
---|
287 | {
|
---|
288 | Directory.CreateDirectory(start);
|
---|
289 | }
|
---|
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 | }
|
---|
298 | else
|
---|
299 | {
|
---|
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 | }
|
---|
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>
|
---|
323 | public IActionResult DeleteFile(int index, int filedex)
|
---|
324 | {
|
---|
325 | if (init())
|
---|
326 | {
|
---|
327 | fillUploadsPaths(upper, index);
|
---|
328 | System.IO.File.Delete(upper.FullFilesPaths[filedex]);
|
---|
329 | var message = upper.DisplayFilesPaths[filedex] + " has been deleted";
|
---|
330 |
|
---|
331 | upper = new UploadedJobViewModel(weblog.getCurrentUser(userId));
|
---|
332 | fillUploadsPaths(upper, index);
|
---|
333 | upper.message = message;
|
---|
334 | //recently opened
|
---|
335 | if (weblog.getFileOpener(userId).Job != null)
|
---|
336 | ViewBag.active = true;
|
---|
337 | ViewBag.Title = "File deleted - Uploads";
|
---|
338 |
|
---|
339 | return View("Uploads", upper);
|
---|
340 | }
|
---|
341 | else
|
---|
342 | {
|
---|
343 | return RedirectToAction("Index", "Home");
|
---|
344 | }
|
---|
345 | }
|
---|
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>
|
---|
352 | public IActionResult OpenFile(int index, int filedex)
|
---|
353 | {
|
---|
354 | if (init())
|
---|
355 | {
|
---|
356 | fillUploadsPaths(upper, index);
|
---|
357 |
|
---|
358 | var serve = weblog.getFileOpener(userId);
|
---|
359 | serve.NewModel();
|
---|
360 | serve.env = (Microsoft.AspNetCore.Hosting.IHostingEnvironment)_environment;
|
---|
361 | serve.vm.directories = upper.DisplayDatePaths;
|
---|
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 | }
|
---|
377 | serve.setTasks();
|
---|
378 | ViewBag.JobsCount = serve.Job.Job.JobCount;
|
---|
379 | ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Uploads";
|
---|
380 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
381 | return View("OpenFile", serve.vm);
|
---|
382 | }
|
---|
383 | else
|
---|
384 | {
|
---|
385 | return RedirectToAction("Index", "Home");
|
---|
386 | }
|
---|
387 | }
|
---|
388 | /// <summary>
|
---|
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);
|
---|
397 | fillUploadsPaths(upper, -1);
|
---|
398 | serve.vm.directories = upper.DisplayDatePaths;
|
---|
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>
|
---|
410 | /// Adds current opened file to hive, uses FileOpeningService singleton
|
---|
411 | /// </summary>
|
---|
412 | /// <returns></returns>
|
---|
413 | public IActionResult AddToHive()
|
---|
414 | {
|
---|
415 | if (init())
|
---|
416 | {
|
---|
417 | var job = weblog.getFileOpener(userId).AddCurrentModelToHive();
|
---|
418 | while (job.Progress.ProgressValue != 1)
|
---|
419 | { }
|
---|
420 |
|
---|
421 | Thread.Sleep(1000);
|
---|
422 | job.Progress.Status = "Upload finished";
|
---|
423 | Thread.Sleep(2000);
|
---|
424 | return RedirectToAction("Index", "Job");
|
---|
425 | }
|
---|
426 | else
|
---|
427 | {
|
---|
428 | return RedirectToAction("Index", "Home");
|
---|
429 | }
|
---|
430 | }
|
---|
431 | [HttpPost]
|
---|
432 | public IActionResult saveToFile(string fname, string dname)
|
---|
433 | {
|
---|
434 | if (init())
|
---|
435 | {
|
---|
436 | weblog.getFileOpener(userId).SaveToFile(fname, dname);
|
---|
437 | fillUploadsPaths(upper, -1);
|
---|
438 | var ind = upper.DisplayDatePaths.IndexOf(dname);
|
---|
439 | return RedirectToAction("UploadDir", new { index = ind });
|
---|
440 | }
|
---|
441 | else
|
---|
442 | {
|
---|
443 | return RedirectToAction("Index", "Home");
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | public FileResult DownloadFile(int index, int filedex)
|
---|
448 | {
|
---|
449 | if (init())
|
---|
450 | {
|
---|
451 | fillUploadsPaths(upper, index);
|
---|
452 |
|
---|
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 | }
|
---|
463 |
|
---|
464 | #endregion
|
---|
465 |
|
---|
466 | #region Uploader
|
---|
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>
|
---|
474 | [HttpPost]
|
---|
475 | public async Task<IActionResult> Uploader(ICollection<IFormFile> files, string directory)
|
---|
476 | {
|
---|
477 | if (init())
|
---|
478 | {
|
---|
479 | var uploads = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName,
|
---|
480 | directory);
|
---|
481 | Directory.CreateDirectory(uploads);
|
---|
482 | foreach (var file in files)
|
---|
483 | {
|
---|
484 | if (file.Length > 0)
|
---|
485 | {
|
---|
486 | var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
|
---|
487 | await file.CopyToAsync(new FileStream(Path.Combine(uploads, fileName),FileMode.OpenOrCreate));
|
---|
488 | // var ioptimizer = ContentManager.Load(Path.Combine(uploads, fileName));
|
---|
489 | //OptimizerHiveTask task = new OptimizerHiveTask((IOptimizer)ioptimizer);
|
---|
490 | //upper.Tasks.Add(task);
|
---|
491 | }
|
---|
492 | }
|
---|
493 | ViewBag.Title = "Upload complete - Uploads";
|
---|
494 | return RedirectToAction("Uploads", "Job");
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | return RedirectToAction("Index", "Home");
|
---|
499 | }
|
---|
500 | }
|
---|
501 | #endregion
|
---|
502 | }
|
---|
503 | }
|
---|