blob: 5f991743d9945606bb4e8da07c049b08ee2b3e77 [file] [log] [blame]
Constantin Zieschefa612082020-04-03 09:54:56 +02001/*******************************************************************************
2* Copyright (c) 2020 Robert Bosch GmbH
3* Author: Constantin Ziesche (constantin.ziesche@bosch.com)
4*
5* This program and the accompanying materials are made available under the
6* terms of the Eclipse Public License 2.0 which is available at
7* http://www.eclipse.org/legal/epl-2.0
8*
9* SPDX-License-Identifier: EPL-2.0
10*******************************************************************************/
11using Microsoft.AspNetCore.Mvc;
12using BaSyx.Utils.ResultHandling;
13using BaSyx.API.Components;
14using BaSyx.Models.Export;
15using Microsoft.AspNetCore.Hosting;
16using Microsoft.Extensions.FileProviders;
17
18namespace BaSyx.API.Http.Controllers.PackageService
19{
20 /// <summary>
21 /// The AASX Package Service
22 /// </summary>
23 public class PackageService : Controller
24 {
25 private readonly IAssetAdministrationShellServiceProvider shellServiceProvider;
Constantin Ziesche9ca87782020-04-03 12:06:02 +020026
27#if NETCOREAPP3_1
Constantin Zieschefa612082020-04-03 09:54:56 +020028 private readonly IWebHostEnvironment hostingEnvironment;
Constantin Zieschee837f992020-08-19 12:04:32 +020029
30 /// <summary>
31 /// Constructor for the AASX-Package Services Controller
32 /// </summary>
33 /// <param name="aasServiceProvider">The Asset Administration Shell Service Provider implementation provided by the dependency injection</param>
34 /// <param name="environment">The Hosting Environment provided by the dependency injection</param>
Constantin Zieschefa612082020-04-03 09:54:56 +020035 public PackageService(IAssetAdministrationShellServiceProvider aasServiceProvider, IWebHostEnvironment environment)
36 {
37 shellServiceProvider = aasServiceProvider;
38 hostingEnvironment = environment;
39 }
Constantin Ziesche9ca87782020-04-03 12:06:02 +020040#else
41 private readonly IHostingEnvironment hostingEnvironment;
Constantin Zieschee837f992020-08-19 12:04:32 +020042
43 /// <summary>
44 /// Constructor for the AASX-Package Services Controller
45 /// </summary>
46 /// <param name="aasServiceProvider">The Asset Administration Shell Service Provider implementation provided by the dependency injection</param>
47 /// <param name="environment">The Hosting Environment provided by the dependency injection</param>
Constantin Ziesche9ca87782020-04-03 12:06:02 +020048 public PackageService(IAssetAdministrationShellServiceProvider aasServiceProvider, IHostingEnvironment environment)
49 {
50 shellServiceProvider = aasServiceProvider;
51 hostingEnvironment = environment;
52 }
53#endif
Constantin Zieschefa612082020-04-03 09:54:56 +020054
55 #region REST-Interface AASX-Package
56
57 /// <summary>
58 /// Retrieves the full AASX Package
59 /// </summary>
60 /// <returns>AASX Package as download</returns>
61 /// <response code="200">Success</response>
62 /// <response code="400">Bad Request</response>
63 [HttpGet("aasx", Name = "GetAASXPackage")]
64 [ProducesResponseType(typeof(Result), 400)]
65 public IActionResult GetAASXPackage()
66 {
Constantin Zieschee837f992020-08-19 12:04:32 +020067 var aas = shellServiceProvider.GetBinding();
68 string filename = aas.IdShort + ".aasx";
Constantin Zieschefa612082020-04-03 09:54:56 +020069 using (AASX aasx = new AASX(filename))
70 {
Constantin Zieschee837f992020-08-19 12:04:32 +020071 AssetAdministrationShellEnvironment_V2_0 env = new AssetAdministrationShellEnvironment_V2_0(aas);
72 aasx.AddEnvironment(aas.Identification, env, ExportType.Xml);
73
Constantin Zieschefa612082020-04-03 09:54:56 +020074 if (aasx != null)
75 {
76 IFileProvider fileProvider = hostingEnvironment.ContentRootFileProvider;
77 foreach (var item in fileProvider.GetDirectoryContents("Content/aasx"))
78 {
79 if (item.IsDirectory)
80 {
Constantin Zieschee837f992020-08-19 12:04:32 +020081 foreach (var subItem in fileProvider.GetDirectoryContents("Content/aasx/" + item.Name))
Constantin Zieschefa612082020-04-03 09:54:56 +020082 {
Constantin Zieschee837f992020-08-19 12:04:32 +020083 if (subItem.Exists)
Constantin Zieschefa612082020-04-03 09:54:56 +020084 aasx.AddFileToAASX("/aasx/" + item.Name + "/" + subItem.Name, subItem.PhysicalPath);
85 }
86 }
87 else
88 {
Constantin Zieschee837f992020-08-19 12:04:32 +020089 if (item.Exists)
Constantin Zieschefa612082020-04-03 09:54:56 +020090 aasx.AddFileToAASX("/aasx/" + item.Name, item.PhysicalPath);
91 }
92 }
93
94 var fileInfo = fileProvider.GetFileInfo(filename);
95 var fileResult = new PhysicalFileResult(fileInfo.PhysicalPath, "application/asset-administration-shell-package")
96 {
97 FileDownloadName = filename
98 };
99 return fileResult;
100 }
101 }
102 return new BadRequestResult();
103 }
104
Constantin Zieschee837f992020-08-19 12:04:32 +0200105 #endregion
106
107 #region Helper Methods
Constantin Zieschefa612082020-04-03 09:54:56 +0200108
109
Constantin Zieschee837f992020-08-19 12:04:32 +0200110 #endregion
Constantin Zieschefa612082020-04-03 09:54:56 +0200111 }
112}