-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAdminShellPackageEnvBase.cs
More file actions
809 lines (700 loc) · 28.6 KB
/
Copy pathAdminShellPackageEnvBase.cs
File metadata and controls
809 lines (700 loc) · 28.6 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/
using Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace AdminShellNS
{
/// <summary>
/// This class lets an outer functionality keep track on the supplementary files, which are in or
/// are pending to be added or deleted to an Package.
/// </summary>
public class AdminShellPackageSupplementaryFile /*: IReferable*/
{
public delegate byte[] SourceGetByteChunk();
public enum LocationType { InPackage, AddPending, DeletePending }
public enum SpecialHandlingType { None, EmbedAsThumbnail }
public readonly Uri Uri = null;
public readonly string UseMimeType = null;
public readonly string SourceLocalPath = null;
public readonly SourceGetByteChunk SourceGetBytesDel = null;
public LocationType Location;
public readonly SpecialHandlingType SpecialHandling;
public AdminShellPackageSupplementaryFile(
Uri uri, string sourceLocalPath = null, LocationType location = LocationType.InPackage,
SpecialHandlingType specialHandling = SpecialHandlingType.None,
SourceGetByteChunk sourceGetBytesDel = null, string useMimeType = null)
{
Uri = uri;
UseMimeType = useMimeType;
SourceLocalPath = sourceLocalPath;
SourceGetBytesDel = sourceGetBytesDel;
Location = location;
SpecialHandling = specialHandling;
}
// class derives from Referable in order to provide GetElementName
public string GetElementName()
{
return "File";
}
}
public class ListOfAasSupplementaryFile : List<AdminShellPackageSupplementaryFile>
{
public AdminShellPackageSupplementaryFile FindByUri(string path)
{
if (path == null)
return null;
return this.FirstOrDefault(
x => x?.Uri?.ToString().Trim() == path.Trim());
}
}
/// <summary>
/// Provides (static?) helpers for serializing AAS..
/// </summary>
public static class AdminShellSerializationHelper
{
public static string TryReadXmlFirstElementNamespaceURI(Stream s)
{
string res = null;
try
{
var xr = System.Xml.XmlReader.Create(s);
int i = 0;
while (xr.Read())
{
// limit amount of read
i++;
if (i > 99)
// obviously not found
break;
// find element
if (xr.NodeType == System.Xml.XmlNodeType.Element)
{
res = xr.NamespaceURI;
break;
}
}
xr.Close();
}
catch (Exception ex)
{
LogInternally.That.SilentlyIgnoredError(ex);
}
// return to zero pos
s.Seek(0, SeekOrigin.Begin);
// give back
return res;
}
/// <summary>
/// Skips first few tokens of an XML content until first "real" element is encountered
/// </summary>
/// <param name="xmlReader"></param>
public static void XmlSkipHeader(XmlReader xmlReader)
{
while (xmlReader.NodeType == XmlNodeType.XmlDeclaration ||
xmlReader.NodeType == XmlNodeType.Whitespace ||
xmlReader.NodeType == XmlNodeType.Comment ||
xmlReader.NodeType == XmlNodeType.None)
xmlReader.Read();
}
/// <summary>
/// De-serialize an open stream into Environment. Does version/ compatibility management.
/// </summary>
/// <param name="s">Open for read stream</param>
/// <returns></returns>
public static AasCore.Aas3_0.Environment DeserializeXmlFromStreamWithCompat(Stream s)
{
// not sure
AasCore.Aas3_0.Environment res = null;
// try get first element
var nsuri = TryReadXmlFirstElementNamespaceURI(s);
// read V1.0?
if (nsuri != null && nsuri.Trim() == "http://www.admin-shell.io/aas/1/0")
{
#if !DoNotUseAasxCompatibilityModels
XmlSerializer serializer = new XmlSerializer(
typeof(AasxCompatibilityModels.AdminShellV10.AdministrationShellEnv),
"http://www.admin-shell.io/aas/1/0");
var v10 = serializer.Deserialize(s) as AasxCompatibilityModels.AdminShellV10.AdministrationShellEnv;
res = new AasCore.Aas3_0.Environment(new List<IAssetAdministrationShell>(), new List<ISubmodel>(), new List<IConceptDescription>());
res.ConvertFromV10(v10);
return res;
#else
throw (new Exception("Cannot handle AAS file format http://www.admin-shell.io/aas/1/0 !"));
#endif
}
// read V2.0?
if (nsuri != null && nsuri.Trim() == "http://www.admin-shell.io/aas/2/0")
{
#if !DoNotUseAasxCompatibilityModels
XmlSerializer serializer = new XmlSerializer(
typeof(AasxCompatibilityModels.AdminShellV20.AdministrationShellEnv),
"http://www.admin-shell.io/aas/2/0");
var v20 = serializer.Deserialize(s) as AasxCompatibilityModels.AdminShellV20.AdministrationShellEnv;
res = new AasCore.Aas3_0.Environment(new List<IAssetAdministrationShell>(), new List<ISubmodel>(), new List<IConceptDescription>());
res.ConvertFromV20(v20);
return res;
#else
throw (new Exception("Cannot handle AAS file format http://www.admin-shell.io/aas/1/0 !"));
#endif
}
// read V3.0?
if (nsuri != null && nsuri.Trim() == Xmlization.NS)
{
// dead-csharp off
//XmlSerializer serializer = new XmlSerializer(
// typeof(AasCore.Aas3_0_RC02.Environment), "http://www.admin-shell.io/aas/3/0");
//res = serializer.Deserialize(s) as AasCore.Aas3_0_RC02.Environment;
// dead-csharp on
using (var xmlReader = XmlReader.Create(s))
{
// TODO (MIHO, 2022-12-26): check if could be feature of AAS core
XmlSkipHeader(xmlReader);
res = Xmlization.Deserialize.EnvironmentFrom(xmlReader);
return res;
}
}
// nope!
return null;
}
public static T DeserializeFromJSON<T>(string data) where T : IReferable
{
//using (var tr = new StringReader(data))
//{
//var serializer = BuildDefaultAasxJsonSerializer();
//var rf = (T)serializer.Deserialize(tr, typeof(T));
var node = System.Text.Json.Nodes.JsonNode.Parse(data);
var rf = Jsonization.Deserialize.IReferableFrom(node);
return (T)rf;
//}
}
/// <summary>
/// Use this (new!) to deserialize flexible JSON "coming from the outside"
/// </summary>
public static T DeserializeAdaptiveFromJSON<T>(string jsonInput) where T : IClass
{
try
{
using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonInput)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new AdminShellConverters.AdaptiveAasIClassConverter(
AdminShellConverters.AdaptiveAasIClassConverter.ConversionMode.AasCore));
var res = serializer.Deserialize<T>(reader);
return res;
}
}
catch (Exception ex)
{
LogInternally.That.CompletelyIgnoredError(ex);
}
return default(T);
}
}
public abstract class AdminShellPackageEnvBase : IDisposable
{
public enum SerializationFormat { None, Xml, Json };
protected IEnvironment _aasEnv = new AasCore.Aas3_0.Environment();
public AdminShellPackageEnvBase() { }
public AdminShellPackageEnvBase(AasCore.Aas3_0.IEnvironment env)
{
if (env != null)
_aasEnv = env;
}
public IEnvironment AasEnv
{
get
{
return _aasEnv;
}
}
public void SetEnvironment(IEnvironment environment)
{
_aasEnv = environment;
}
// TODO: remove, is not for base class!!
public virtual void SetFilename(string fileName)
{
}
// TODO: remove, is not for base class!!
public virtual string Filename
{
get
{
return "";
}
}
protected static WebProxy _manualProxy = null;
protected WebProxy GetPossibleManualProxy()
{
// already there?
if (_manualProxy != null)
return _manualProxy;
// no, check (once, static!)
string proxyAddress = "";
string username = "";
string password = "";
string proxyFile = "proxy.txt";
if (System.IO.File.Exists(proxyFile))
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(proxyFile))
{
proxyFile = sr.ReadLine();
}
}
catch (IOException e)
{
Console.WriteLine("proxy.txt could not be read:");
Console.WriteLine(e.Message);
}
}
try
{
using (StreamReader sr = new StreamReader(proxyFile))
{
proxyAddress = sr.ReadLine();
username = sr.ReadLine();
password = sr.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(proxyFile + " not found!");
}
if (proxyAddress != "")
{
_manualProxy = new WebProxy();
Uri newUri = new Uri(proxyAddress);
_manualProxy.Address = newUri;
_manualProxy.Credentials = new NetworkCredential(username, password);
return _manualProxy;
}
return null;
}
public virtual HttpClient CreateDefaultHttpClient()
{
// read via HttpClient (uses standard proxies)
var handler = new HttpClientHandler();
if (GetPossibleManualProxy() != null)
handler.Proxy = GetPossibleManualProxy();
else
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
handler.AllowAutoRedirect = true;
// new http client
var client = new HttpClient(handler);
// ok
return client;
}
public virtual HttpRequestMessage CreateHttpRequest(
HttpMethod method, string requestUri,
ISecurityAccessHandler secureAccess = null,
string acceptHeader = null)
{
// start
var request = new HttpRequestMessage(method, requestUri);
// seems to be required by Phoenix
request.Headers.Add("User-Agent", "aasx-package-explorer/1.0.0");
// would be good to always have an accept header, if in doubt: "*/*"
if (acceptHeader?.HasContent() == true)
request.Headers.Add("Accept", acceptHeader);
// add access headers?
var headerItem = secureAccess?.LookupAuthenticateHeader(requestUri);
headerItem?.Enrich(request);
// ok
return request;
}
/// <summary>
/// Checks for a file within the package or external, e.g. on a file space.
/// In derived classes, could use the AAS/ SM / IdShortPath attributes to
/// refer to files in registry/ repository.
/// </summary>
/// <param name="uriString">Local (within package) or external file. Should have a scheme.
/// Local file start with a leading slash.</param>
/// <param name="aasId">For registry/ repository access.</param>
/// <param name="smId">For registry/ repository access.</param>
/// <param name="idShortPath">For registry/ repository access.</param>
/// <returns>Bytes or <c>null</c></returns>
public virtual byte[] GetBytesFromPackageOrExternal(
string uriString,
string aasId = null,
string smId = null,
ISecurityAccessHandler secureAccess = null,
string acceptHeader = null,
string idShortPath = null)
{
//
// this part of the functionality works on HTTP and absolute files and is
// indepedent from a package storage.
//
// split scheme and part
var sap = AdminShellUtil.GetSchemeAndPath(uriString);
if (sap == null)
return null;
// Check, if remote
if (sap.Scheme.StartsWith("http"))
{
// get httpClient
var client = CreateDefaultHttpClient();
// make request, may add headers
HttpResponseMessage response = null;
using (var request = CreateHttpRequest(HttpMethod.Get, uriString,
acceptHeader: acceptHeader,
secureAccess: secureAccess))
{
// send request
response = client.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult();
}
// MIHO, 2025-07-21: old, working code:
// var response = client.GetAsync(uriString).GetAwaiter().GetResult();
// re-direct?
if (response.StatusCode == HttpStatusCode.Moved
|| response.StatusCode == HttpStatusCode.Found)
{
var location = response.Headers.Location;
response = client.GetAsync(location).GetAwaiter().GetResult();
}
// successfull?
if (!response.IsSuccessStatusCode)
return null;
// get bytes
var resBytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
// TODO (MIHO, 2024-11-09): looks like a hack from Andreas to
// detect indirection
if (resBytes.Length < 500) // indirect load?
{
string json = System.Text.Encoding.UTF8.GetString(resBytes);
var parsed = JObject.Parse(json);
try
{
string url = parsed.SelectToken("url").Value<string>();
response = client.GetAsync(url).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
return null;
resBytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
LogInternally.That.SilentlyIgnoredError(ex);
}
}
return resBytes;
}
// now, has to be file
if (sap.Scheme != "file")
return null;
// if not starting with "/", if has to be an absolute file
if (!sap.Path.StartsWith('/'))
{
try
{
var resBytes = System.IO.File.ReadAllBytes(sap.Path);
return resBytes;
}
catch (Exception ex)
{
LogInternally.That.SilentlyIgnoredError(ex);
}
}
// no, nothing more to find here (in base implementation!)
return null;
}
/// <summary>
/// Checks for a file within the package or external, e.g. on a file space.
/// In derived classes, could use the AAS/ SM / IdShortPath attributes to
/// refer to files in registry/ repository.
/// </summary>
/// <param name="uriString">Local (within package) or external file. Should have a scheme.
/// Local file start with a leading slash.</param>
/// <param name="aasId">For registry/ repository access.</param>
/// <param name="smId">For registry/ repository access.</param>
/// <param name="idShortPath">For registry/ repository access.</param>
/// <returns>Bytes or <c>null</c></returns>
public virtual async Task<byte[]> GetBytesFromPackageOrExternalAsync(
string uriString,
string aasId = null,
string smId = null,
ISecurityAccessHandler secureAccess = null,
string idShortPath = null)
{
await Task.Yield();
return GetBytesFromPackageOrExternal(uriString, aasId, smId, secureAccess, idShortPath);
}
/// <summary>
/// Writes to a file within the package or to external registry/ repository.
/// Does not write to external files e.g. on a filespace!
/// In derived classes, could use the AAS/ SM / IdShortPath attributes to
/// refer to files in registry/ repository.
/// </summary>
/// <param name="uriString">Local (within package) or external file. Should have a scheme.
/// Local file start with a leading slash.</param>
/// <param name="data">Data given by the bytes</param>
/// <param name="aasId">For registry/ repository access.</param>
/// <param name="smId">For registry/ repository access.</param>
/// <param name="idShortPath">For registry/ repository access.</param>
/// <returns><c>True</c>, if the file was successfully stored.</returns>
public virtual bool PutBytesToPackageOrExternal(
string uriString,
byte[] data,
string aasId = null,
string smId = null,
string idShortPath = null)
{
// here, nothing to do, as external file storage is not allowed.
return false;
}
/// <summary>
/// Writes to a file within the package or to external registry/ repository.
/// Does not write to external files e.g. on a filespace!
/// In derived classes, could use the AAS/ SM / IdShortPath attributes to
/// refer to files in registry/ repository.
/// </summary>
/// <param name="uriString">Local (within package) or external file. Should have a scheme.
/// Local file start with a leading slash.</param>
/// <param name="data">Data given by the bytes</param>
/// <param name="aasId">For registry/ repository access.</param>
/// <param name="smId">For registry/ repository access.</param>
/// <param name="idShortPath">For registry/ repository access.</param>
/// <returns><c>True</c>, if the file was successfully stored.</returns>
public virtual async Task<bool> PutBytesToPackageOrExternalAsync(
string uriString,
byte[] data,
string aasId = null,
string smId = null,
string idShortPath = null)
{
// here, nothing to do, as external file storage is not allowed.
await Task.Yield();
return PutBytesToPackageOrExternal(uriString, data, aasId, smId, idShortPath);
}
public virtual void PrepareSupplementaryFileParameters(ref string targetDir, ref string targetFn)
{
}
public virtual string AddSupplementaryFileToStore(
string sourcePath, string targetDir, string targetFn, bool embedAsThumb,
AdminShellPackageSupplementaryFile.SourceGetByteChunk sourceGetBytesDel = null, string useMimeType = null)
{
return null;
}
/// <summary>
/// Gets the thumbnail data of the local package (and only the local package!)
/// </summary>
public virtual byte[] GetLocalThumbnailBytes(ref Uri thumbUri)
{
return null;
}
/// <summary>
/// Gets the thumbnail data of the local package or (1st prio) the AAS with the
/// given id.
/// Note: does not access remote content from registry/ repository!
/// </summary>
public virtual byte[] GetThumbnailBytesFromAasOrPackage(string aasId)
{
// find aas?
var aas = AasEnv?.FindAasById(aasId);
if (aas?.AssetInformation?.DefaultThumbnail?.Path?.HasContent() == true)
{
try
{
// Note: could also use http://...
var bytes = GetBytesFromPackageOrExternal(uriString: aas.AssetInformation.DefaultThumbnail.Path);
if (bytes != null)
return bytes;
}
catch (Exception ex)
{
LogInternally.That.CompletelyIgnoredError(ex);
}
}
// or local package?
try
{
Uri dummy = null;
var bytes = GetLocalThumbnailBytes(ref dummy);
if (bytes != null)
return bytes;
}
catch (Exception ex)
{
LogInternally.That.CompletelyIgnoredError(ex);
}
// no
return null;
}
public virtual ListOfAasSupplementaryFile GetListOfSupplementaryFiles()
{
return null;
}
public virtual void DeleteSupplementaryFile(AdminShellPackageSupplementaryFile psf)
{
}
/// <summary>
/// Copies/ download contents and will return filename of temp file.
/// </summary>
/// <returns></returns>
public virtual string MakePackageFileAvailableAsTempFile(string packageUri, bool keepFilename = false)
{
// this uses the virtual implementation and should therefore work ok in the base class
// access
if (packageUri == null)
return null;
// get input stream
var inputBytes = GetBytesFromPackageOrExternal(packageUri);
if (inputBytes == null)
return null;
// generate tempfile name
string tempext = System.IO.Path.GetExtension(packageUri);
string temppath = System.IO.Path.GetTempFileName().Replace(".tmp", tempext);
// maybe modify tempfile name?
if (keepFilename)
{
var masterFn = System.IO.Path.GetFileNameWithoutExtension(packageUri);
var tmpDir = System.IO.Path.GetDirectoryName(temppath);
var tmpFnExt = System.IO.Path.GetFileName(temppath);
temppath = System.IO.Path.Combine(tmpDir, "" + masterFn + "_" + tmpFnExt);
}
// copy to temp file
System.IO.File.WriteAllBytes(temppath, inputBytes);
return temppath;
}
/// <summary>
/// Copies/ download contents and will return filename of temp file.
/// </summary>
/// <returns></returns>
public virtual async Task<string> MakePackageFileAvailableAsTempFileAsync(
string packageUri,
string aasId = null,
string smId = null,
string idShortPath = null,
bool keepFilename = false,
ISecurityAccessHandler secureAccess = null)
{
// this uses the virtual implementation and should therefore work ok in the base class
// access
if (packageUri == null)
return null;
// get input stream
var inputBytes = await GetBytesFromPackageOrExternalAsync(packageUri, aasId, smId,
idShortPath: idShortPath, secureAccess: secureAccess);
if (inputBytes == null)
return null;
// generate tempfile name
string tempext = System.IO.Path.GetExtension(packageUri);
string temppath = System.IO.Path.GetTempFileName().Replace(".tmp", tempext);
// maybe modify tempfile name?
if (keepFilename)
{
var masterFn = System.IO.Path.GetFileNameWithoutExtension(packageUri);
var tmpDir = System.IO.Path.GetDirectoryName(temppath);
var tmpFnExt = System.IO.Path.GetFileName(temppath);
temppath = System.IO.Path.Combine(tmpDir, "" + masterFn + "_" + tmpFnExt);
}
// copy to temp file
await System.IO.File.WriteAllBytesAsync(temppath, inputBytes);
return temppath;
}
public virtual bool SaveAs(
string fn, bool writeFreshly = false,
SerializationFormat prefFmt = SerializationFormat.None,
MemoryStream useMemoryStream = null, bool saveOnlyCopy = false)
{
return false;
}
/// <summary>
/// Temporariyl saves & closes package and executes lambda. Afterwards, the package is re-opened
/// under the same file name
/// </summary>
/// <param name="lambda">Action which is to be executed while the file is CLOSED</param>
/// <param name="prefFmt">Format for the saved file</param>
public virtual void TemporarilySaveCloseAndReOpenPackage(
Action lambda,
AdminShellPackageFileBasedEnv.SerializationFormat prefFmt = AdminShellPackageFileBasedEnv.SerializationFormat.None)
{
}
public virtual bool IsOpen
{
get
{
// negative default behaviour
return false;
}
}
public virtual bool IsLocalFile(string uriString)
{
return false;
}
public virtual void Close()
{
}
public virtual void Flush()
{
}
public virtual void Dispose()
{
}
//
// Binary file read + write
//
public async Task<bool> PutByteArrayToExternalUri(string uri, byte[] ba)
{
// split uri and access
var sap = AdminShellUtil.GetSchemeAndPath(uri);
if (ba == null || sap == null)
return false;
// directly a HTTP resource?
if (sap.Scheme.StartsWith("http"))
{
try
{
using (var client = new HttpClient())
using (var content = new ByteArrayContent(ba))
{
content.Headers.ContentType = new MediaTypeHeaderValue("*/*");
var response = await client.PostAsync(uri, content);
return response.IsSuccessStatusCode;
}
}
catch (Exception ex)
{
LogInternally.That.SilentlyIgnoredError(ex);
return false;
}
}
// no other schemes now
if (sap.Scheme != "file")
return false;
// just write
try
{
System.IO.File.WriteAllBytes(sap.Path, ba);
return true;
}
catch (Exception ex)
{
LogInternally.That.SilentlyIgnoredError(ex);
}
return false;
}
}
}