File size: 6,361 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using DWSIM.Simulate365.FormFactories;
using DWSIM.Simulate365.Models;
using DWSIM.UI.Web.Settings;
using Microsoft.Graph;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DWSIM.Simulate365.Services
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class FilePickerService
    {
        public event EventHandler S3365DashboardFileOpenStarted;
        public event EventHandler<S365File> S3365DashboardFileOpened;
        public event EventHandler<S365DashboardSaveFile> S365DashboardSaveFileClicked;
        public event EventHandler S365DashboardFolderCreated;

        public S365DashboardSaveFile SelectedSaveFile { get; private set; } = null;
        public S365File SelectedOpenFile { get; private set; } = null;

        public void OpenFile(string fileUniqueIdentifier)
        {
            try
            {
                S3365DashboardFileOpenStarted?.Invoke(this, new EventArgs());

                var token = UserService.GetInstance().GetUserToken();
                var client = GetDashboardClient(token);

                var result = Task.Run(async () => await client.GetAsync($"/api/files/{fileUniqueIdentifier}/single?includeBreadcrumbs=true")).Result;
                var resultContent = Task.Run(async () => await result.Content.ReadAsStringAsync()).Result;
                var itemWithBreadcrumbs = JsonConvert.DeserializeObject<FilesWithBreadcrumbsResponseModel>(resultContent);

                var item = itemWithBreadcrumbs.File;
                // Get drive item
                var stream = Task.Run(async () => await client.GetStreamAsync($"/api/files/{fileUniqueIdentifier}/download")).Result;

                var extension = System.IO.Path.GetExtension(item.Name);
                var tmpFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}{extension}");

                Task.Run(async () =>
                {
                    using (var destStream = System.IO.File.OpenWrite(tmpFilePath))
                        await stream.CopyToAsync(destStream);
                }).Wait();

                var parentFolderBreadcrumb = itemWithBreadcrumbs.BreadcrumbItems.LastOrDefault();

                this.SelectedOpenFile = new S365File(tmpFilePath)
                {
                    Filename = item.Name,
                    ParentUniqueIdentifier = parentFolderBreadcrumb?.UniqueIdentifier.ToString() ?? string.Empty,
                    FileUniqueIdentifier = item.UniqueIdentifier.ToString(),
                    FullPath = GetFullPath(itemWithBreadcrumbs.BreadcrumbItems,item)
                };

                S3365DashboardFileOpened?.Invoke(this, this.SelectedOpenFile);
            }
            catch (Exception ex)
            {
                this.SelectedOpenFile = null;
                throw new Exception("An error occurred while opening file from S365 Dashboard.", ex);
            }
        }
        private string GetFullPath(List<BreadcrumbItem> breadcrumbItems, FileModel file)
        {
            var filePath = "//Simulate 365 Dashboard";

            if (breadcrumbItems?.Any() ?? false)
            {
                var breadcrumbPath = string.Join("/", breadcrumbItems.Select(x => x.Name));
                filePath = $"{filePath}/{breadcrumbPath}/{file.Name}";
            }
            else
            {
                filePath = $"{filePath}/{file.Name}";
            }
            return filePath;
        }

        public void SaveFile(string filename, string parentDirectoryUniqueId, string fullPath, int? conflictAction)
        {
            try
            {
                this.SelectedSaveFile = new S365DashboardSaveFile
                {
                    Filename = filename,
                    ParentUniqueIdentifier = parentDirectoryUniqueId,
                    SimulatePath = fullPath,
                    ConflictAction = conflictAction.HasValue ? (UploadConflictAction)conflictAction.Value : (UploadConflictAction?)null
                };

                this.S365DashboardSaveFileClicked?.Invoke(this, this.SelectedSaveFile);
            }
            catch (Exception ex)
            {
                this.SelectedSaveFile = null;
                throw new Exception("An error occurred while saving file to S365 Dashboard.", ex);
            }
        }

        public void CreateFolder(string folderName, string parentDirectoryUniqueId)
        {
            try
            {
                var token = UserService.GetInstance().GetUserToken();
                var client = GetDashboardClient(token);

                var model = new
                {
                    ParentDirectoryUniqueId = !string.IsNullOrWhiteSpace(parentDirectoryUniqueId) ? Guid.Parse(parentDirectoryUniqueId) : (Guid?)null,
                    DirectoryName = folderName,
                    // 0 = Overwrite folder, 1= Keep both
                    ConflictAction = 0
                };

                var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");

                Task.Run(async () => { await client.PostAsync($"/api/files", content); }).Wait();

                S365DashboardFolderCreated?.Invoke(this, new EventArgs());
            }
            catch (Exception ex)
            {

                throw new Exception("An error occurred while creating folder on S365 Dashboard.", ex);
            }
        }

        public void ShowLoginForm()
        {
            var userService = UserService.GetInstance();
            userService.ShowLogin();
        }

        private HttpClient GetDashboardClient(string token)
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri(DashboardSettings.DashboardServiceUrl);
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

            return client;

        }
    }

    public class S365DashboardSaveFile
    {
        public string Filename { get; set; }
        public string ParentUniqueIdentifier { get; set; }
        public string SimulatePath { get; set; }
        public UploadConflictAction? ConflictAction { get; set; }
    }
}