1 | using Microsoft.AspNet.Mvc;
|
---|
2 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
8 | using System.ServiceModel.Security;
|
---|
9 | using Microsoft.AspNet.Http;
|
---|
10 | using System.IO;
|
---|
11 | using Microsoft.Net.Http.Headers;
|
---|
12 | using Microsoft.AspNet.Hosting;
|
---|
13 | using HeuristicLab.Common;
|
---|
14 | using HeuristicLab.Core;
|
---|
15 | using HeuristicLab.Optimization;
|
---|
16 | using System.Threading;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
19 | {
|
---|
20 | /// <summary>
|
---|
21 | /// Controller for everything Job related (includes uploads and uploader)
|
---|
22 | /// </summary>
|
---|
23 | public class JobController : Controller
|
---|
24 | {
|
---|
25 | private WebLoginService weblog;
|
---|
26 |
|
---|
27 | private HiveServiceLocatorWeb serviceLocator;
|
---|
28 | private HiveServiceClient serviceClient;
|
---|
29 | private HiveClientWeb clientWeb;
|
---|
30 | private Guid userId;
|
---|
31 |
|
---|
32 | private JobViewModel vm;
|
---|
33 | private IHostingEnvironment _environment;
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | public JobController(IHostingEnvironment env)
|
---|
38 | {
|
---|
39 | weblog = WebLoginService.Instance;
|
---|
40 | vm = new JobViewModel();
|
---|
41 | _environment = env;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// initializes the connection for the right user. Not constructor because httpcontext is needed.
|
---|
46 | /// </summary>
|
---|
47 | private bool init()
|
---|
48 | {
|
---|
49 | var u = HttpContext.Session.GetString("UserId");
|
---|
50 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
51 | {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | userId = Guid.Parse(u);
|
---|
57 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
58 | serviceClient = serviceLocator.getHiveServiceClient();
|
---|
59 | clientWeb = weblog.getClientWeb(userId);
|
---|
60 | return serviceLocator.CheckLogin();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | #region Jobs
|
---|
65 | /// <summary>
|
---|
66 | /// initial job page, shows all jobs
|
---|
67 | /// </summary>
|
---|
68 | /// <returns></returns>
|
---|
69 | public IActionResult Index()
|
---|
70 | {
|
---|
71 | if (init())
|
---|
72 | {
|
---|
73 | clientWeb.Refresh();
|
---|
74 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
75 |
|
---|
76 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
77 | ViewBag.Title = "Jobs";
|
---|
78 | return View(vm);
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | return RedirectToAction("Index", "Home");
|
---|
83 | }
|
---|
84 | }
|
---|
85 | /// <summary>
|
---|
86 | /// Initial page and a selected job
|
---|
87 | /// </summary>
|
---|
88 | /// <param name="id">Job id selected</param>
|
---|
89 | /// <returns></returns>
|
---|
90 | public IActionResult Selected(Guid id)
|
---|
91 | {
|
---|
92 | if (init())
|
---|
93 | {
|
---|
94 | clientWeb.Refresh();
|
---|
95 |
|
---|
96 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
97 | foreach (var j in vm.userJobs)
|
---|
98 | {
|
---|
99 | if (j.Id == id)
|
---|
100 | {
|
---|
101 | vm.selectedJob = j;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | //vm.selectedJob.RefreshAutomatically = true;
|
---|
105 | clientWeb.LoadJob(vm.selectedJob);
|
---|
106 | weblog.getFileOpener(userId).NewModel();
|
---|
107 | weblog.getFileOpener(userId).Job = vm.selectedJob;
|
---|
108 | ViewBag.Title = vm.selectedJob.Job.Name + " - Jobs";
|
---|
109 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
110 | return View("Index", vm);
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | return RedirectToAction("Index", "Home");
|
---|
115 | }
|
---|
116 | }
|
---|
117 | /// <summary>
|
---|
118 | /// Deletes a job
|
---|
119 | /// </summary>
|
---|
120 | /// <param name="id">Job id</param>
|
---|
121 | /// <returns></returns>
|
---|
122 | public IActionResult Delete(Guid id)
|
---|
123 | {
|
---|
124 | if (init())
|
---|
125 | {
|
---|
126 |
|
---|
127 | vm.message = serviceClient.GetJob(id).Name + " deleted";
|
---|
128 | serviceClient.DeleteJob(id);
|
---|
129 | clientWeb.Refresh();
|
---|
130 |
|
---|
131 | vm.userJobs = clientWeb.Jobs.ToList();
|
---|
132 | ViewBag.Title = vm.message + " - Jobs";
|
---|
133 | return View("Index", vm);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | return RedirectToAction("Index", "Home");
|
---|
138 | }
|
---|
139 | }
|
---|
140 | #endregion
|
---|
141 |
|
---|
142 | #region Uploads
|
---|
143 | /// <summary>
|
---|
144 | /// Shows the uploaded directories
|
---|
145 | /// </summary>
|
---|
146 | /// <returns></returns>
|
---|
147 | public IActionResult Uploads()
|
---|
148 | {
|
---|
149 | if (init())
|
---|
150 | {
|
---|
151 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
152 | fillUploadsPaths(upper, -1);
|
---|
153 | ViewBag.Name = serviceClient.ClientCredentials.UserName.UserName;
|
---|
154 | ViewBag.Title = "Uploads";
|
---|
155 | return View("Uploads", upper);
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | return RedirectToAction("Index", "Home");
|
---|
160 | }
|
---|
161 | }
|
---|
162 | /// <summary>
|
---|
163 | /// //Shows content of selected dir
|
---|
164 | /// </summary>
|
---|
165 | /// <param name="index">Array index selected directory</param>
|
---|
166 | /// <returns></returns>
|
---|
167 | public IActionResult UploadDir(int index)
|
---|
168 | {
|
---|
169 | if (init())
|
---|
170 | {
|
---|
171 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
172 | fillUploadsPaths(upper, index);
|
---|
173 | if (index != -1)
|
---|
174 | ViewBag.Title = upper.DisplayDatePaths[index] + " - Uploads";
|
---|
175 | else
|
---|
176 | ViewBag.Title = "Add files - Uploads";
|
---|
177 | return View("Uploads", upper);
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | return RedirectToAction("Index", "Home");
|
---|
182 | }
|
---|
183 | }
|
---|
184 | /// <summary>
|
---|
185 | /// Loads all the paths from the selected uploads folder
|
---|
186 | /// </summary>
|
---|
187 | /// <param name="vm">Viewmodel to save every directory path</param>
|
---|
188 | /// <param name="index">Index selected directory</param>
|
---|
189 | private void fillUploadsPaths(UploadedJobViewModel vm, int index)
|
---|
190 | {
|
---|
191 | var tempdex = index; //Fix when maps gets deleted
|
---|
192 | var start = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName);
|
---|
193 | var dirs = Directory.GetDirectories(start);
|
---|
194 | foreach (string dir in dirs)
|
---|
195 | {
|
---|
196 | if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
|
---|
197 | {
|
---|
198 | Directory.Delete(dir, false);
|
---|
199 | tempdex = -1;
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | vm.FullDatePaths.Add(dir);
|
---|
204 | var temp = dir.Split('\\');
|
---|
205 | vm.DisplayDatePaths.Add(temp[temp.Length - 1]);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | if (tempdex != -1)
|
---|
209 | {
|
---|
210 | vm.SelectedIndex = tempdex;
|
---|
211 | dirs = Directory.GetFiles(vm.FullDatePaths[tempdex]);
|
---|
212 | foreach (string dir in dirs)
|
---|
213 | {
|
---|
214 | vm.FullFilesPaths.Add(dir);
|
---|
215 | var temp = dir.Split('\\');
|
---|
216 | vm.DisplayFilesPaths.Add(temp[temp.Length - 1]);
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | /// <summary>
|
---|
221 | /// Deletes a file
|
---|
222 | /// </summary>
|
---|
223 | /// <param name="index">Index directory</param>
|
---|
224 | /// <param name="filedex">Index file to delete</param>
|
---|
225 | /// <returns></returns>
|
---|
226 | public IActionResult DeleteFile(int index, int filedex)
|
---|
227 | {
|
---|
228 | if (init())
|
---|
229 | {
|
---|
230 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
231 | fillUploadsPaths(upper, index);
|
---|
232 | System.IO.File.Delete(upper.FullFilesPaths[filedex]);
|
---|
233 | var message = upper.DisplayFilesPaths[filedex] + " has been deleted";
|
---|
234 |
|
---|
235 | upper = new UploadedJobViewModel();
|
---|
236 | fillUploadsPaths(upper, index);
|
---|
237 | upper.message = message;
|
---|
238 | ViewBag.Title = "File deleted - Uploads";
|
---|
239 |
|
---|
240 | return View("Uploads", upper);
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | return RedirectToAction("Index", "Home");
|
---|
245 | }
|
---|
246 | }
|
---|
247 | /// <summary>
|
---|
248 | /// Opens a selected file
|
---|
249 | /// </summary>
|
---|
250 | /// <param name="index">Index directory</param>
|
---|
251 | /// <param name="filedex">Index selected file</param>
|
---|
252 | /// <returns></returns>
|
---|
253 | public IActionResult OpenFile(int index, int filedex)
|
---|
254 | {
|
---|
255 | if (init())
|
---|
256 | {
|
---|
257 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
258 | fillUploadsPaths(upper, index);
|
---|
259 |
|
---|
260 | var serve = weblog.getFileOpener(userId);
|
---|
261 | serve.NewModel();
|
---|
262 | serve.env = _environment;
|
---|
263 |
|
---|
264 | var ioptimizer = ContentManager.Load(upper.FullFilesPaths[filedex]);
|
---|
265 |
|
---|
266 | serve.vm.SelectedTask = new OptimizerHiveTask((IOptimizer)ioptimizer);
|
---|
267 | if (serve.vm.SelectedTask.ItemTask.Item is IAlgorithm)
|
---|
268 | {
|
---|
269 | serve.vm.SelectedAlgorithm = (IAlgorithm)serve.vm.SelectedTask.ItemTask.Item;
|
---|
270 | }
|
---|
271 | else if (serve.vm.SelectedTask.ItemTask.Item is BatchRun)
|
---|
272 | {
|
---|
273 | serve.vm.SelectedBatchRun = (BatchRun)serve.vm.SelectedTask.ItemTask.Item;
|
---|
274 | }
|
---|
275 | else if (serve.vm.SelectedTask.ItemTask.Item is Experiment)
|
---|
276 | {
|
---|
277 | serve.vm.SelectedExperiment = (Experiment)serve.vm.SelectedTask.ItemTask.Item;
|
---|
278 | }
|
---|
279 | serve.setTasks();
|
---|
280 | ViewBag.JobsCount = serve.Job.Job.JobCount;
|
---|
281 | ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Uploads";
|
---|
282 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
283 | return View("OpenFile", serve.vm);
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | return RedirectToAction("Index", "Home");
|
---|
288 | }
|
---|
289 | }
|
---|
290 | /// <summary>
|
---|
291 | /// Adds current opened file to hive, uses FileOpeningService singleton
|
---|
292 | /// </summary>
|
---|
293 | /// <returns></returns>
|
---|
294 | public IActionResult AddToHive()
|
---|
295 | {
|
---|
296 | if (init())
|
---|
297 | {
|
---|
298 | var job = weblog.getFileOpener(userId).AddCurrentModelToHive();
|
---|
299 | while (job.Progress.ProgressValue != 1)
|
---|
300 | { }
|
---|
301 |
|
---|
302 | Thread.Sleep(1000);
|
---|
303 | job.Progress.Status = "Upload finished";
|
---|
304 | Thread.Sleep(2000);
|
---|
305 | return RedirectToAction("Index", "Job");
|
---|
306 | }
|
---|
307 | else
|
---|
308 | {
|
---|
309 | return RedirectToAction("Index", "Home");
|
---|
310 | }
|
---|
311 | }
|
---|
312 | //TODO Work in progress
|
---|
313 | /* public async Task<IActionResult> DownloadFile(int index, int filedex)
|
---|
314 | {
|
---|
315 | if (((HiveServiceLocatorWeb)(HiveServiceLocatorWeb.Instance)).CheckLogin())
|
---|
316 | {
|
---|
317 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
318 | fillUploadsPaths(upper, index);
|
---|
319 |
|
---|
320 | HttpContext.Response.ContentType = "APPLICATION/OCTET-STREAM";
|
---|
321 | String Header = "Attachment; Filename=" + upper.DisplayFilesPaths[filedex];
|
---|
322 | HttpContext.Response.Headers.Add("Content-Disposition", Header);
|
---|
323 | System.IO.FileInfo Dfile = new System.IO.FileInfo(upper.FullFilesPaths[filedex]);
|
---|
324 | await HttpContext.Response.WriteAsync(Dfile.FullName);
|
---|
325 | //HttpContext.Response.End();
|
---|
326 |
|
---|
327 | var message = upper.DisplayFilesPaths[filedex] + " has been downloaded";
|
---|
328 |
|
---|
329 | upper.message = message;
|
---|
330 | ViewBag.Title = "Downloaded file";
|
---|
331 |
|
---|
332 | return View("Uploads", upper);
|
---|
333 | }
|
---|
334 | else
|
---|
335 | {
|
---|
336 | HiveServiceLocatorWeb.SetLoginErrorMessage();
|
---|
337 | return RedirectToAction("Index", "Home");
|
---|
338 | }
|
---|
339 | }*/
|
---|
340 |
|
---|
341 | #endregion
|
---|
342 |
|
---|
343 | #region Uploader
|
---|
344 |
|
---|
345 | /// <summary>
|
---|
346 | /// Uploads file onto server directory
|
---|
347 | /// </summary>
|
---|
348 | /// <param name="files">Files</param>
|
---|
349 | /// <param name="directory">Directory path for server</param>
|
---|
350 | /// <returns></returns>
|
---|
351 | [HttpPost]
|
---|
352 | public async Task<IActionResult> Uploader(ICollection<IFormFile> files, string directory)
|
---|
353 | {
|
---|
354 | if (init())
|
---|
355 | {
|
---|
356 | UploadedJobViewModel upper = new UploadedJobViewModel();
|
---|
357 | var uploads = Path.Combine(_environment.WebRootPath, "uploads", serviceClient.ClientCredentials.UserName.UserName,
|
---|
358 | directory);
|
---|
359 | Directory.CreateDirectory(uploads);
|
---|
360 | foreach (var file in files)
|
---|
361 | {
|
---|
362 | if (file.Length > 0)
|
---|
363 | {
|
---|
364 | var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
|
---|
365 | await file.SaveAsAsync(Path.Combine(uploads, fileName));
|
---|
366 | // var ioptimizer = ContentManager.Load(Path.Combine(uploads, fileName));
|
---|
367 | //OptimizerHiveTask task = new OptimizerHiveTask((IOptimizer)ioptimizer);
|
---|
368 | //upper.Tasks.Add(task);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | ViewBag.Title = "Upload complete - Uploads";
|
---|
372 | return RedirectToAction("Uploads", "Job");
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | return RedirectToAction("Index", "Home");
|
---|
377 | }
|
---|
378 | }
|
---|
379 | #endregion
|
---|
380 | }
|
---|
381 | }
|
---|