Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 11 | using Microsoft.AspNetCore.Mvc; |
| 12 | using BaSyx.Utils.ResultHandling; |
| 13 | using BaSyx.API.Components; |
| 14 | using BaSyx.Models.Export; |
| 15 | using Microsoft.AspNetCore.Hosting; |
| 16 | using Microsoft.Extensions.FileProviders; |
| 17 | |
| 18 | namespace 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 Ziesche | 9ca8778 | 2020-04-03 12:06:02 +0200 | [diff] [blame] | 26 | |
| 27 | #if NETCOREAPP3_1 |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 28 | private readonly IWebHostEnvironment hostingEnvironment; |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 29 | |
| 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 Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 35 | public PackageService(IAssetAdministrationShellServiceProvider aasServiceProvider, IWebHostEnvironment environment) |
| 36 | { |
| 37 | shellServiceProvider = aasServiceProvider; |
| 38 | hostingEnvironment = environment; |
| 39 | } |
Constantin Ziesche | 9ca8778 | 2020-04-03 12:06:02 +0200 | [diff] [blame] | 40 | #else |
| 41 | private readonly IHostingEnvironment hostingEnvironment; |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 42 | |
| 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 Ziesche | 9ca8778 | 2020-04-03 12:06:02 +0200 | [diff] [blame] | 48 | public PackageService(IAssetAdministrationShellServiceProvider aasServiceProvider, IHostingEnvironment environment) |
| 49 | { |
| 50 | shellServiceProvider = aasServiceProvider; |
| 51 | hostingEnvironment = environment; |
| 52 | } |
| 53 | #endif |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 54 | |
| 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 Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 67 | var aas = shellServiceProvider.GetBinding(); |
| 68 | string filename = aas.IdShort + ".aasx"; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 69 | using (AASX aasx = new AASX(filename)) |
| 70 | { |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 71 | AssetAdministrationShellEnvironment_V2_0 env = new AssetAdministrationShellEnvironment_V2_0(aas); |
| 72 | aasx.AddEnvironment(aas.Identification, env, ExportType.Xml); |
| 73 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 74 | 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 Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 81 | foreach (var subItem in fileProvider.GetDirectoryContents("Content/aasx/" + item.Name)) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 82 | { |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 83 | if (subItem.Exists) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 84 | aasx.AddFileToAASX("/aasx/" + item.Name + "/" + subItem.Name, subItem.PhysicalPath); |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 89 | if (item.Exists) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 90 | 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 Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 105 | #endregion |
| 106 | |
| 107 | #region Helper Methods |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 108 | |
| 109 | |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 110 | #endregion |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 111 | } |
| 112 | } |