Ejemplo n.º 1
0
        public static void publishResource(
            string prefix,
            System.IO.FileStream fileStream,
            ResourceType resourceType,
            string resourceName,
            string version,
            PublishDepsInfoContainer publishDepsInfo)
        {
            System.Collections.Specialized.NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);

            queryString["deps"] = publishDepsInfo.serialize();

            string url = resourcePostUrl(prefix, resourceType, resourceName, version) + "?" + queryString.ToString();

            try
            {
                using (var fileContent = new System.Net.Http.StreamContent(fileStream))
                {
                    fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                    {
                        Name     = $"files[{resourceName}]",
                        FileName = resourceName
                    };
                    fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

                    System.Net.Http.HttpResponseMessage statusResult = client.PostAsync(url, fileContent).Result;

                    if (!statusResult.IsSuccessStatusCode)
                    {
                        throw new NetworkUtilsException("Unable to publish: HTTP Error: " + statusResult.StatusCode.ToString());
                    }
                }
            }
            catch (NetworkUtilsException ex)
            {
                throw ex;
            }
            catch (System.Exception)
            {
                throw new NetworkUtilsException($"Unable to publish {resourceType} resource {resourceName} to server");
            }
        }
Ejemplo n.º 2
0
        public static void publishResource(string prefix, ResourceIdentifier resourceDescription, IEnumerable <ResourceIdentifier> deps)
        {
            try
            {
                PublishDepsInfoContainer publishDepsInfo = new PublishDepsInfoContainer();
                //Console.WriteLine(deps);
                foreach (ResourceIdentifier depDescription in deps)
                {
                    if (depDescription.version == null)
                    {
                        Logger.logError(
                            "The versions of dependencies must be supplied. For example, " +
                            "\"dependency.csv\" does not include version, \"[email protected]\" does."
                            );
                        return;
                    }

                    var publishDepDescription = new PublishDepsInfoContainer.PublishDepDescription(depDescription.version);
                    if (depDescription.resourceType == ResourceType.Code)
                    {
                        publishDepsInfo.codeDeps[depDescription.resourceName] = publishDepDescription;
                    }
                    else if (depDescription.resourceType == ResourceType.Data)
                    {
                        publishDepsInfo.dataDeps[depDescription.resourceName] = publishDepDescription;
                    }
                    else if (depDescription.resourceType == ResourceType.Model)
                    {
                        publishDepsInfo.modelDeps[depDescription.resourceName] = publishDepDescription;
                    }
                }

                string       resourceName   = resourceDescription.resourceName;
                ResourceType resourceType   = resourceDescription.resourceType;
                string       publishVersion = resourceDescription.version;

                // check that user has provided version to publish file as
                if (publishVersion == null)
                {
                    publishVersion = "1.0";
                    Logger.logLine($"Using default version {publishVersion}");
                }

                if (!ResourceTypes.hasNecessaryDirs())
                {
                    Logger.logError($"Could not find nyoka resource folders in current directory. Try running {Constants.APPLICATION_ALIAS} init?");
                    return;
                }

                // If a file to publish with the given name can't be found
                if (!ResourceTypes.resourceFileExists(resourceType, resourceName))
                {
                    Logger.logError($"Resource with name {resourceName} not found.");
                    return;
                }

                var resourcesOnServer = NetworkUtils.getAvailableResources(prefix, resourceType);

                // If this resource already exists on server
                if (resourcesOnServer.resourceDescriptions.ContainsKey(resourceName))
                {
                    ResourceVersionsInfoContainer serverVersionsInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);

                    // If this resource exists with the same version on server
                    if (serverVersionsInfo.versions.ContainsKey(publishVersion))
                    {
                        bool continueAnyways = Logger.askYesOrNo(
                            $"Version {publishVersion} of {resourceType.ToString()} resource " +
                            $"{resourceName} already exists on server. Overwrite?"
                            );

                        if (!continueAnyways)
                        {
                            Logger.logLine("Aborting publish.");
                            return;
                        }
                        else
                        {
                            Logger.logLine("Overwriting resource on server.");
                        }
                    }
                }

                Logger.logLine("Opening file.");
                FileStream fileStream = ResourceTypes.readResourceFile(resourceType, resourceName);

                Logger.logLine("Uploading file.");
                NetworkUtils.publishResource(
                    prefix,
                    fileStream,
                    resourceType,
                    resourceName,
                    publishVersion,
                    publishDepsInfo
                    );

                // create or overwrite version file locally for this resource to be the publishVersion
                using (var versionFileStream = ResourceTypes.createOrOverwriteResourceVersionFile(resourceType, resourceName))
                {
                    versionFileStream.WriteLine(publishVersion);
                }
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                Logger.logError($"Network Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                Logger.logError($"File System Error: " + ex.Message);
            }
        }