<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
using NLog;
using OpenIIoT.Core.Platform;
using OpenIIoT.Core.Plugin;
using OpenIIoT.SDK;
using OpenIIoT.SDK.Common;
using Utility.OperationResult;
using OpenIIoT.SDK.Package;
using OpenIIoT.Core.Service.WebAPI;
using System.IO;
using System;

namespace OpenIIoT.Core.Package.WebAPI
{
    /// <summary>
    ///     Handles the API methods for AppPackages.
    /// </summary>
    public class PackageController : ApiBaseController
    {
        #region Variables

        /// <summary>
        ///     The default serialization properties for an AppPackage.
        /// </summary>
        private static List<string> pluginPackageSerializationProperties = new List<string>(new string[] { "Files" });

        /// <summary>
        ///     The ApplicationManager for the application.
        /// </summary>
        private IApplicationManager manager = ApplicationManager.GetInstance();

        #endregion Variables

        #region Instance Methods

        [Route("api/package/{fqn}/delete")]
        [HttpGet]
        public async Task<HttpResponseMessage> DeletePackage(string fqn)
        {
            HttpResponseMessage retVal;
            HttpStatusCode statusCode = HttpStatusCode.OK;

            IResult deleteResult = await manager.GetManager<IPackageManager>().DeletePackageAsync(fqn);

            retVal = Request.CreateResponse(HttpStatusCode.OK, deleteResult, JsonFormatter());
            return retVal;
        }

        [Route("api/package/{fqn}/download")]
        [HttpGet]
        public async Task<HttpResponseMessage> DownloadPackage(string fqn)
        {
            HttpResponseMessage retVal;

            IResult<byte[]> readResult = await manager.GetManager<IPackageManager>().ReadPackageAsync(fqn);

            return Request.CreateResponse(HttpStatusCode.OK, readResult, JsonFormatter());
        }

        /// <summary>
        ///     Returns the Package from the list of available Packages that matches the supplied Fully Qualified Name.
        /// </summary>
        /// <param name="fqn">The Fully Qualified Name of the Package to return.</param>
        /// <returns>The matching Package.</returns>
        [Route("api/package/{fqn}")]
        [HttpGet]
        public async Task<HttpResponseMessage> FindPackage(string fqn)
        {
            HttpResponseMessage retVal;
            IPackage findResult = await manager.GetManager<IPackageManager>().FindPackageAsync(fqn);

            retVal = Request.CreateResponse(HttpStatusCode.OK, findResult, JsonFormatter(ContractResolverType.OptOut, "Files"));

            return retVal;
        }

        [Route("api/package")]
        [HttpGet]
        public HttpResponseMessage GetPackages()
        {
            IList<IPackage> packages = manager.GetManager<IPackageManager>().Packages;

            return Request.CreateResponse(HttpStatusCode.OK, packages, JsonFormatter(ContractResolverType.OptOut, "Files"));
        }

        [Route("api/package/{fqn}/install")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackage(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/overwrite")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageOverwrite(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, PackageInstallOptions.Overwrite, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/overwrite/skipverification")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageOverwriteSkipVerification(string fqn, string publicKey = "")
        {
            PackageInstallOptions options = PackageInstallOptions.Overwrite | PackageInstallOptions.SkipVerification;
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, options, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/skipverification")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageSkipVerification(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, PackageInstallOptions.SkipVerification, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        /// <summary>
        ///     Reloads the list of Packages from disk and returns the list.<commit_msg>Renamed DownloadPackage to ReadPackage, re-implemented DownloadPackage<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
using NLog;
using OpenIIoT.Core.Platform;
using OpenIIoT.Core.Plugin;
using OpenIIoT.SDK;
using OpenIIoT.SDK.Common;
using Utility.OperationResult;
using OpenIIoT.SDK.Package;
using OpenIIoT.Core.Service.WebAPI;
using System.IO;
using System;

namespace OpenIIoT.Core.Package.WebAPI
{
    /// <summary>
    ///     Handles the API methods for AppPackages.
    /// </summary>
    public class PackageController : ApiBaseController
    {
        #region Variables

        /// <summary>
        ///     The default serialization properties for an AppPackage.
        /// </summary>
        private static List<string> pluginPackageSerializationProperties = new List<string>(new string[] { "Files" });

        /// <summary>
        ///     The ApplicationManager for the application.
        /// </summary>
        private IApplicationManager manager = ApplicationManager.GetInstance();

        #endregion Variables

        #region Instance Methods

        [Route("api/package/{fqn}/delete")]
        [HttpGet]
        public async Task<HttpResponseMessage> DeletePackage(string fqn)
        {
            HttpResponseMessage retVal;
            HttpStatusCode statusCode = HttpStatusCode.OK;

            IResult deleteResult = await manager.GetManager<IPackageManager>().DeletePackageAsync(fqn);

            retVal = Request.CreateResponse(HttpStatusCode.OK, deleteResult, JsonFormatter());
            return retVal;
        }

        [Route("api/package/{fqn}/download")]
        [HttpGet]
        public async Task<HttpResponseMessage> DownloadPackage(string fqn)
        {
            HttpResponseMessage retVal;

            IPackage findResult = await manager.GetManager<IPackageManager>().FindPackageAsync(fqn);

            if (findResult != default(IPackage))
            {
                IResult<byte[]> readResult = await manager.GetManager<IPackageManager>().ReadPackageAsync(fqn);

                if (readResult.ResultCode != ResultCode.Failure)
                {
                    retVal = Request.CreateResponse(HttpStatusCode.OK);
                    retVal.Content = new ByteArrayContent(readResult.ReturnValue);
                    retVal.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    retVal.Content.Headers.ContentDisposition.FileName = Path.GetFileName(findResult.FileName);
                }
                else
                {
                    retVal = Request.CreateResponse(HttpStatusCode.InternalServerError);
                }
            }
            else
            {
                retVal = Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }

        /// <summary>
        ///     Returns the Package from the list of available Packages that matches the supplied Fully Qualified Name.
        /// </summary>
        /// <param name="fqn">The Fully Qualified Name of the Package to return.</param>
        /// <returns>The matching Package.</returns>
        [Route("api/package/{fqn}")]
        [HttpGet]
        public async Task<HttpResponseMessage> FindPackage(string fqn)
        {
            HttpResponseMessage retVal;
            IPackage findResult = await manager.GetManager<IPackageManager>().FindPackageAsync(fqn);

            retVal = Request.CreateResponse(HttpStatusCode.OK, findResult, JsonFormatter(ContractResolverType.OptOut, "Files"));

            return retVal;
        }

        [Route("api/package")]
        [HttpGet]
        public HttpResponseMessage GetPackages()
        {
            IList<IPackage> packages = manager.GetManager<IPackageManager>().Packages;

            return Request.CreateResponse(HttpStatusCode.OK, packages, JsonFormatter(ContractResolverType.OptOut, "Files"));
        }

        [Route("api/package/{fqn}/install")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackage(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/overwrite")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageOverwrite(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, PackageInstallOptions.Overwrite, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/overwrite/skipverification")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageOverwriteSkipVerification(string fqn, string publicKey = "")
        {
            PackageInstallOptions options = PackageInstallOptions.Overwrite | PackageInstallOptions.SkipVerification;
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, options, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/install/skipverification")]
        [HttpGet]
        public async Task<HttpResponseMessage> InstallPackageSkipVerification(string fqn, string publicKey = "")
        {
            IResult installResult = await manager.GetManager<IPackageManager>().InstallPackageAsync(fqn, PackageInstallOptions.SkipVerification, publicKey);

            return Request.CreateResponse(HttpStatusCode.OK, installResult, JsonFormatter());
        }

        [Route("api/package/{fqn}/read")]
        [HttpGet]
        public async Task<HttpResponseMessage> ReadPackage(string fqn)
        {
            HttpResponseMessage retVal;

            IResult<byte[]> readResult = await manager.GetManager<IPackageManager>().ReadPackageAsync(fqn);

            return Request.CreateResponse(HttpStatusCode.OK, readResult, JsonFormatter());
        }

        /// <summary>
        ///     Reloads the list of Packages from disk and returns the list.
{'fraction_non_alphanumeric': 0.09991379310344828, 'fraction_numerical': 0.0, 'mean_word_length': 2.778827361563518, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1787917', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2875, 'n_words': 605}