示例#1
0
        /// <summary>
        /// Retrieves image as byte array by its id.
        /// </summary>
        /// <param name="id">The ID of the media library element.</param>
        /// <param name="imageId">The ID of the image.</param>
        /// <returns></returns>
        public async Task <byte[]> GetImage(string id, string imageId, ImageTypeEnum imageType)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(imageId))
            {
                return(null);
            }

            ImageCacheItem cacheItem = new ImageCacheItem
            {
                Id             = imageId,
                ImageType      = imageType.ToString(),
                MediaElementId = id
            };

            if (isCacheEnabled)
            {
                ImageCacheItem retrievedItem = await _localCacheService.Get <ImageCacheItem>(cacheItem.GetFileName());

                if (retrievedItem != null)
                {
                    return(retrievedItem.Image);
                }
            }

            string imageEndpoint = string.Format(GetImageEndpoint, id, imageType, imageId);

            using (HttpClient cli = new HttpClient())
            {
                cli.AddAuthorizationHeaders();

                HttpResponseMessage result = await cli.GetAsync(imageEndpoint);

                byte[] retrievedImage = await result.Content.ReadAsByteArrayAsync();

                if (isCacheEnabled)
                {
                    cacheItem.Image = retrievedImage;
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    _localCacheService.Set(cacheItem.GetFileName(), cacheItem);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                }

                return(retrievedImage);
            }
        }
        private void RegisterImage(string message, string imageName, string imageAliasName, ImageTypeEnum imageType, string imageAttributes, Guid sdkMessageProcessingStepId, string pluginType)
        {
            if (imageAliasName.Length == 0)
            {
                imageAliasName = imageName;
            }
            if (imageAttributes.Length == 0)
            {
                CliLog.WriteLine();
                CliLog.WriteLine();
                CliLog.WriteLine(ConsoleColor.Red, "!!! Plugin Type: ", ConsoleColor.Green, pluginType, ConsoleColor.Red, " register ", ConsoleColor.Green, "Image", ConsoleColor.Red, " need provide ", ConsoleColor.Green, "ImageAttributes", ConsoleColor.Red, " value !!!");
                CliLog.WriteLine();
                CliLog.WriteLine();
                CliLog.WriteLine(ConsoleColor.Red, "!!! DEPLOY [PLUGIN] FAILED !!!");
                throw new Exception();
            }
            var image = new Entity("sdkmessageprocessingstepimage")
            {
                ["name"]      = imageName,
                ["imagetype"] = new OptionSetValue((int)imageType),
                ["sdkmessageprocessingstepid"] = new EntityReference("sdkmessageprocessingstep", sdkMessageProcessingStepId),
                ["attributes"]          = imageAttributes.Replace(" ", ""),
                ["entityalias"]         = imageAliasName,
                ["messagepropertyname"] = message == "Create" ? "Id" : "Target"
            };
            var fetchData = new
            {
                name = imageName,
                sdkmessageprocessingstepid = sdkMessageProcessingStepId,
                imagetype = (int)imageType
            };
            var fetchXml = $@"
<fetch>
  <entity name='sdkmessageprocessingstepimage'>
    <attribute name='sdkmessageprocessingstepimageid' />
    <attribute name='name' />
    <attribute name='entityalias' />
    <attribute name='attributes' />
    <attribute name='imagetype' />
    <filter type='and'>
      <condition attribute='sdkmessageprocessingstepid' operator='eq' value='{fetchData.sdkmessageprocessingstepid}'/>
      <condition attribute='imagetype' operator='eq' value='{fetchData.imagetype}'/>
      <condition attribute='name' operator='eq' value='{fetchData.name}'/>
    </filter>
  </entity>
</fetch>";
            var rows     = crmServiceClient.RetrieveMultiple(new FetchExpression(fetchXml));

            if (rows.Entities.Count == 0)
            {
                if (imageAttributes.Replace(" ", "").Length > 0)
                {
                    CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorRed, "     Registering", CliLog.ColorGreen, " Image: ", CliLog.ColorCyan, $"{imageName}");
                    try
                    {
                        crmServiceClient.Create(image);
                    }
                    catch
                    {
                        CliLog.WriteLine();
                        CliLog.WriteLine();
                        CliLog.WriteLine(ConsoleColor.Red, "!!! Plugin Type: ", ConsoleColor.Green, pluginType, ConsoleColor.Red, " does not support: ", ConsoleColor.Green, imageType.ToString());
                        CliLog.WriteLine();
                        CliLog.WriteLine();
                        CliLog.WriteLine(ConsoleColor.Red, "!!! DEPLOY [PLUGIN] FAILED !!!");
                        throw;
                    }
                }
            }
            else
            {
                var row         = rows.Entities[0];
                var name        = row.GetAttributeValue <string>("name");
                var entityalias = row.GetAttributeValue <string>("entityalias");
                var attributes  = row.GetAttributeValue <string>("attributes");
                var imagetype   = row.GetAttributeValue <OptionSetValue>("imagetype").Value;
                if (name == imageName &&
                    entityalias == imageAliasName &&
                    attributes == imageAttributes.Replace(" ", "") &&
                    imagetype == (int)imageType)
                {
                    CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorGreen, "     Image: ", CliLog.ColorCyan, $"{imageName}");
                }
                else
                {
                    if (attributes != imageAttributes.Replace(" ", "") && imageAttributes.Replace(" ", "").Length != 0)
                    {
                        image["sdkmessageprocessingstepimageid"] = rows.Entities[0].Id;
                        CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorRed, "     Updating", CliLog.ColorGreen, " Image: ", CliLog.ColorCyan, $"{imageName}");
                        crmServiceClient.Update(image);
                    }
                    else if (imageAttributes.Replace(" ", "").Length == 0)
                    {
                        CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorRed, "     Deleting", CliLog.ColorGreen, " Image: ", CliLog.ColorCyan, $"{imageName}");
                        crmServiceClient.Delete("sdkmessageprocessingstepimage", rows.Entities[0].Id);
                    }
                }
            }
        }