Пример #1
0
        public async Task <IActionResult> InstallSystemPackage()
        {
            string packageFile = Path.GetTempFileName();

            try
            {
                try
                {
                    // Write package file
                    using (FileStream stream = new(packageFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        await Request.Body.CopyToAsync(stream);
                    }

                    // Install it
                    using CommandConnection connection = await BuildConnection();

                    await connection.InstallSystemPackage(packageFile);

                    return(NoContent());
                }
                catch (Exception e)
                {
                    if (e is AggregateException ae)
                    {
                        e = ae.InnerException;
                    }
                    if (e is IncompatibleVersionException)
                    {
                        _logger.LogError($"[{nameof(InstallSystemPackage)}] Incompatible DCS version");
                        return(StatusCode(502, "Incompatible DCS version"));
                    }
                    if (e is SocketException)
                    {
                        _logger.LogError($"[{nameof(InstallSystemPackage)}] DCS is not started");
                        return(StatusCode(503, "Failed to connect to Duet, please check your connection (DCS is not started)"));
                    }
                    _logger.LogWarning(e, $"[{nameof(InstallSystemPackage)} Failed to upload package file to {packageFile}");
                    return(StatusCode(500, e.Message));
                }
            }
            finally
            {
                System.IO.File.Delete(packageFile);
            }
        }