aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs
diff options
context:
space:
mode:
authorAvi Levkovich <avi@twine-s.com>2018-02-26 16:30:42 +0200
committerAvi Levkovich <avi@twine-s.com>2018-02-26 16:30:42 +0200
commit5942bb7a13e5ad26c720a1b95ae4ea766eeeda25 (patch)
tree2a855ce1065c875e615f5b040f984cb3fb84e518 /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs
parent4c052df707280abd208b03aa88cf904e0eb6b9bf (diff)
parent6549d8672a93893599e921d9f1938af7dcabb8bf (diff)
downloadTango-5942bb7a13e5ad26c720a1b95ae4ea766eeeda25.tar.gz
Tango-5942bb7a13e5ad26c720a1b95ae4ea766eeeda25.zip
MERGE!
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs')
-rw-r--r--Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs207
1 files changed, 207 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs
new file mode 100644
index 000000000..5cc95c617
--- /dev/null
+++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UpdateService/MachineStudioUpdateService.svc.cs
@@ -0,0 +1,207 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.IO;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.ServiceModel;
+using System.ServiceModel.Web;
+using System.Text;
+using Tango.Core.Helpers;
+using Tango.Integration.Observables;
+using Tango.Logging;
+using Tango.MachineStudio.Common.Update;
+
+namespace Tango.MachineStudio.UpdateService
+{
+ public class MachineStudioUpdateService : IMachineStudioUpdateService
+ {
+ private class PendingUpload
+ {
+ public String Token { get; set; }
+
+ public String Version { get; set; }
+
+ public String UserGuid { get; set; }
+
+ public String Comments { get; set; }
+
+ public String FilePath { get; set; }
+ }
+
+ private static List<PendingUpload> _pendingUploads;
+
+ static MachineStudioUpdateService()
+ {
+ _pendingUploads = new List<PendingUpload>();
+ }
+
+ public CheckForUpdatesResponse CheckForUpdates(CheckForUpdatesRequest request)
+ {
+ LogManager.Log("Request received...");
+
+ try
+ {
+ CheckForUpdatesResponse response = new CheckForUpdatesResponse();
+
+ using (ObservablesContext db = ObservablesContext.CreateDefaultForWeb())
+ {
+ db.Configuration.LazyLoadingEnabled = false;
+
+ //Load relation first...
+ db.Roles.ToList();
+ db.Permissions.ToList();
+ db.UsersRoles.ToList();
+ db.RolesPermissions.ToList();
+
+ var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password);
+
+ if (user != null && user.HasPermission(Permissions.RunMachineStudio))
+ {
+ var latestVersion = db.MachineStudioVersions.FirstOrDefault();
+ Version currentVersion = Version.Parse(request.Version);
+
+ if (latestVersion != null && Version.Parse(latestVersion.Version) > currentVersion)
+ {
+ response.IsUpdateAvailable = true;
+
+ response.FtpHost = ConfigurationManager.AppSettings["FtpHost"].ToString();
+ response.UserName = ConfigurationManager.AppSettings["UserName"].ToString();
+ response.Password = ConfigurationManager.AppSettings["Password"].ToString();
+ response.FilePath = latestVersion.FtpFilePath;
+ response.Version = latestVersion.Version;
+ }
+ }
+ else
+ {
+ throw new FaultException("Invalid user credentials.");
+ }
+ }
+
+ return response;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex);
+ throw new FaultException(ex.ToString());
+ }
+ }
+
+ public UploadVersionResponse UploadVersion(UploadVersionRequest request)
+ {
+ try
+ {
+ UploadVersionResponse response = new UploadVersionResponse();
+
+ using (ObservablesContext db = ObservablesContext.CreateDefaultForWeb())
+ {
+ db.Configuration.LazyLoadingEnabled = false;
+
+ //Load relation first...
+ db.Roles.ToList();
+ db.Permissions.ToList();
+ db.UsersRoles.ToList();
+ db.RolesPermissions.ToList();
+
+ var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password);
+
+ if (user != null && user.HasPermission(Permissions.PublishMachineStudioVersion))
+ {
+ var latestVersion = db.MachineStudioVersions.FirstOrDefault();
+ Version currentVersion = Version.Parse(request.Version);
+
+ String newVersionFileName = Path.GetRandomFileName() + ".zip";
+
+ String newVersionFilePath = "/machine studio versions/" + newVersionFileName;
+
+ if (currentVersion > Version.Parse(latestVersion.Version))
+ {
+ response.FtpHost = ConfigurationManager.AppSettings["FtpHost"].ToString();
+ response.UserName = ConfigurationManager.AppSettings["UserName"].ToString();
+ response.Password = ConfigurationManager.AppSettings["Password"].ToString();
+ response.FilePath = newVersionFilePath;
+ response.FileName = newVersionFileName;
+ response.Token = Guid.NewGuid().ToString();
+
+ _pendingUploads.Add(new PendingUpload()
+ {
+ UserGuid = user.Guid,
+ Comments = request.Comments,
+ Token = response.Token,
+ Version = request.Version,
+ FilePath = response.FilePath,
+ });
+ }
+ else
+ {
+ throw new FaultException("New version must be greater than latest version.");
+ }
+ }
+ else
+ {
+ throw new FaultException("Invalid user credentials.");
+ }
+ }
+
+ return response;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex);
+ throw new FaultException(ex.ToString());
+ }
+ }
+
+ public void NotifyUploadCompleted(UploadCompletedRequest request)
+ {
+ try
+ {
+ PendingUpload upload = _pendingUploads.FirstOrDefault(x => x.Token == request.Token);
+
+ if (upload != null)
+ {
+ _pendingUploads.RemoveAll(x => x.Token == upload.Token);
+
+ using (ObservablesContext db = ObservablesContext.CreateDefaultForWeb())
+ {
+ db.Configuration.LazyLoadingEnabled = false;
+
+ db.MachineStudioVersions.Add(new MachineStudioVersion()
+ {
+ Comments = upload.Comments,
+ FtpFilePath = upload.FilePath,
+ UserGuid = upload.UserGuid,
+ Version = upload.Version
+ });
+
+ db.SaveChanges();
+ }
+ }
+ else
+ {
+ throw new FaultException("Invalid Token.");
+ }
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex);
+ throw new FaultException(ex.ToString());
+ }
+ }
+
+ public string GetLatestVersion()
+ {
+ try
+ {
+ using (ObservablesContext db = ObservablesContext.CreateDefaultForWeb())
+ {
+ return db.MachineStudioVersions.FirstOrDefault().Version;
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new FaultException(ex.ToString());
+ }
+ }
+ }
+}