Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AttachmentInfo.cs
1using Microsoft.Win32;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Input;
10
12{
16 public class AttachmentInfo : Model
17 {
18 private readonly ContractModel contractModel;
19 private readonly Attachment attachment;
20 private readonly Property<string> id;
21 private readonly Property<string> legalId;
22 private readonly Property<string> contentType;
23 private readonly Property<string> fileName;
24 private readonly Property<string> url;
25 private readonly Property<byte[]> signature;
26 private readonly Property<DateTime> timestamp;
27
28 private readonly Command downloadAttachment;
29 private readonly Command removeAttachment;
30
37 {
38 this.contractModel = ContractModel;
39 this.attachment = Attachment;
40
41 this.id = new Property<string>(nameof(this.Id), Attachment.Id, this);
42 this.legalId = new Property<string>(nameof(this.LegalId), Attachment.LegalId, this);
43 this.contentType = new Property<string>(nameof(this.ContentType), Attachment.ContentType, this);
44 this.fileName = new Property<string>(nameof(this.FileName), Attachment.FileName, this);
45 this.url = new Property<string>(nameof(this.Url), Attachment.Url, this);
46 this.signature = new Property<byte[]>(nameof(this.Signature), Attachment.Signature, this);
47 this.timestamp = new Property<DateTime>(nameof(this.Timestamp), Attachment.Timestamp, this);
48
49 this.downloadAttachment = new Command(this.CanExecuteDownloadAttachment, this.ExecuteDownloadAttachment);
50 this.removeAttachment = new Command(this.CanExecuteRemoveAttachment, this.ExecuteRemoveAttachment);
51 }
52
56 public Attachment Attachment => this.attachment;
57
61 public string Id
62 {
63 get => this.id.Value;
64 set => this.id.Value = value;
65 }
66
70 public string LegalId
71 {
72 get => this.legalId.Value;
73 set => this.legalId.Value = value;
74 }
75
79 public string ContentType
80 {
81 get => this.contentType.Value;
82 set => this.contentType.Value = value;
83 }
84
88 public string FileName
89 {
90 get => this.fileName.Value;
91 set => this.fileName.Value = value;
92 }
93
97 public string Url
98 {
99 get => this.url.Value;
100 set => this.url.Value = value;
101 }
102
106 public byte[] Signature
107 {
108 get => this.signature.Value;
109 set => this.signature.Value = value;
110 }
111
115 public DateTime Timestamp
116 {
117 get => this.timestamp.Value;
118 set => this.timestamp.Value = value;
119 }
120
124 public ICommand DownloadAttachment => this.downloadAttachment;
125
131 {
132 return this.contractModel?.CanDownloadAttachment ?? false;
133 }
134
138 public async Task ExecuteDownloadAttachment()
139 {
140 try
141 {
142 string Extension = Path.GetExtension(this.FileName);
143 if (Extension.StartsWith("."))
144 Extension = Extension[1..];
145
146 SaveFileDialog Dialog = new()
147 {
148 AddExtension = true,
149 CheckFileExists = false,
150 CheckPathExists = true,
151 CreatePrompt = false,
152 DefaultExt = Extension,
153 Filter = "Similar Files (*." + Extension + ")|*." + Extension,
154 OverwritePrompt = true,
155 Title = "Save Attachment"
156 };
157
158 bool? Result = Dialog.ShowDialog(MainWindow.currentInstance);
159 if (!Result.HasValue || !Result.Value)
160 return;
161
163
164 KeyValuePair<string, TemporaryFile> P = await this.contractModel.ContractsClient.GetAttachmentAsync(this.Url, SignWith.CurrentKeys);
165
166 using TemporaryFile Temp = P.Value;
167 using FileStream Destination = File.Create(Dialog.FileName);
168
169 Temp.Position = 0;
170 await Temp.CopyToAsync(Destination);
171
172 MainWindow.SuccessBox("Attachment successfully downloaded.");
173 }
174 catch (Exception ex)
175 {
176 MainWindow.ErrorBox(ex.Message);
177 }
178 }
179
183 public ICommand RemoveAttachment => this.removeAttachment;
184
190 {
191 return this.contractModel?.CanUploadAttachment ?? false;
192 }
193
197 public async Task ExecuteRemoveAttachment()
198 {
199 try
200 {
201 if (MessageBox.Show("Are you sure you want to remove the attachment " + this.Id + "?", "Confirm",
202 MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
203 {
204 return;
205 }
206
208
209 Contract Contract = await this.contractModel.ContractsClient.RemoveContractAttachmentAsync(this.Id);
210
211 await this.contractModel.SetContract(Contract);
212
213 MainWindow.SuccessBox("Attachment successfully removed.");
214 }
215 catch (Exception ex)
216 {
217 MainWindow.ErrorBox(ex.Message);
218 }
219 }
220 }
221}
Contains a reference to an attachment assigned to a legal object.
Definition: Attachment.cs:9
string LegalId
Legal ID of uploader of the attachment
Definition: Attachment.cs:38
string FileName
Filename of attachment.
Definition: Attachment.cs:56
string ContentType
Internet Content Type of binary attachment.
Definition: Attachment.cs:47
DateTime Timestamp
Timestamp of signature. If no signature, value is DateTime.MinValue
Definition: Attachment.cs:84
string Url
URL to retrieve attachment, if provided.
Definition: Attachment.cs:65
byte[] Signature
Binary signature of the attachment, generated by an approved legal identity of the account-holder....
Definition: Attachment.cs:75
Contains the definition of a contract
Definition: Contract.cs:22
Abstract base class of signatures
Definition: Signature.cs:10
Class managing the contents of a temporary file. When the class is disposed, the temporary file is de...
SignWith
Options on what keys to use when signing data.
Definition: Enumerations.cs:84