Esempio n. 1
0
        public IActionResult Post([FromBody] GetImageDownloadInfoRequest request)
        {
            //Ensure that the application version exists and that the device has access to it.
            using (var connection = _connectionFactory.CreateAndOpen())
            {
                var applicationVersion = connection.Get <ApplicationVersion>(request.Id);

                if (applicationVersion == null)
                {
                    return(NotFound());
                }

                var device = connection.Get <Device>(DeviceId);

                if (device == null)
                {
                    return(NotFound());
                }

                if (device.ApplicationId != applicationVersion.ApplicationId)
                {
                    return(BadRequest());
                }

                //Verify that the device has access to this *specific* version.
                var application = connection.Get <Application>(device.ApplicationId);

                if (application.ApplicationVersionId != request.Id && device.ApplicationVersionId != request.Id)
                {
                    return(BadRequest());
                }

                var response = new ImageDownloadInfo()
                {
                    ImageId    = applicationVersion.ImageId,
                    Registry   = _registryConfig.RegistryHost,
                    AuthToken  = null, //TODO: This is where the auth token will go
                    Repository = _repositoryNameFactory.FromApplication(device.ApplicationId),
                    Name       = applicationVersion.Name,
                };

                //TODO: Add a device event for getting this token

                return(Ok(response));
            }
        }
        public IActionResult Post([FromBody] GetApplicationUploadInfoRequest request)
        {
            //Make sure we can find the application
            using (var connection = _connectionFactory.CreateAndOpen())
            {
                var application = connection.Get <Application>(request.ApplicationId);

                if (application == null)
                {
                    return(NotFound(new Error($"Unable to find application '{request.ApplicationId}'.")));
                }

                if (string.IsNullOrWhiteSpace(request.Name))
                {
                    return(BadRequest(new Error("No name was specified.")));
                }

                if (string.IsNullOrWhiteSpace(request.ImageId))
                {
                    return(BadRequest(new Error("No image id was specified.")));
                }

                //Check for duplicate name.
                if (connection.IsApplicationVersionNameInUse(request.ApplicationId, request.Name))
                {
                    return(Ok(new GetUploadInfoResponse
                    {
                        Reason = $"Name '{request.Name}' is already in use for application '{application.Name}'. Specify a new name."
                    }));
                }

                //Craft the response
                var response = new GetUploadInfoResponse
                {
                    CanUpload    = true,
                    RegistryHost = _registryConfig.RegistryHost,
                    Repository   = _repositoryNameFactory.FromApplication(request.ApplicationId)
                };

                return(Ok(response));
            }
        }