private async Task ManageUploadedFile(WorkOffer workOffer, IFormFile file)
        {
            string Id = "file-" + workOffer.Email.Replace('.', '-').Replace('@', '-');

            if (!System.IO.Directory.Exists("./temp"))
            {
                System.IO.Directory.CreateDirectory("./temp");
            }
            using (var stream = System.IO.File.Create("./temp/" + file.FileName))
            {
                await file.CopyToAsync(stream);
            }
            var bytes = System.IO.File.ReadAllBytes("./temp/" + file.FileName);

            //check the existence of the asset
            try
            {
                var assetFound = await managementClient.GetAsset(Id);

                if (assetFound.SystemProperties.PublishedVersion != null)
                {
                    await managementClient.UnpublishAsset(Id, assetFound.SystemProperties.Version ?? 1);
                }
                await managementClient.DeleteAsset(Id, assetFound.SystemProperties.Version ?? 1);
            }
            catch (ContentfulException)
            {
                //do nothing, the asset doesn't existst and it's ok
            }


            var managementAsset = new ManagementAsset
            {
                SystemProperties = new SystemProperties {
                    Id = Id, Version = 1
                },
                Title = new Dictionary <string, string> {
                    { "en-US", workOffer.Email }
                },
                Files = new Dictionary <string, File>
                {
                    { "en-US", new File()
                      {
                          ContentType = "text/plain",
                          FileName    = file.FileName
                      } }
                }
            };

            await managementClient.UploadFileAndCreateAsset(managementAsset, bytes)
            .ContinueWith(async delegate
            {
                // in this phase the upper function seems to make a different version is supposed to make, probably  UploadFileAndCreateAsset
                // in order to upload and create the assets it makes 2 operation recognized by Contentful
                // var uploadedAsset = await managementClient.GetAsset(Id);
                var uploadedAsset = await managementClient.GetAsset(Id);
                await managementClient.PublishAsset(Id, version: uploadedAsset.SystemProperties.Version ?? 2);
                workOffer.Offer = await client.GetAsset(Id);
            });
        }
 public async Task <Asset> Get(string assetId)
 {
     try
     {
         return(await _client.GetAsset(assetId, QueryBuilder <Asset> .New));
     }
     catch (ContentfulException ex)
     {
         _logger.LogWarning(new EventId(), ex, $"There was a problem with getting assetId: {assetId} from contentful");
         return(null);
     }
 }
        /// <summary>
        /// Executes the taghelper.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (string.IsNullOrEmpty(AssetId))
            {
                return;
            }

            var queryBuilder = QueryBuilder <Asset> .New;

            if (!string.IsNullOrEmpty(Locale))
            {
                queryBuilder = queryBuilder.LocaleIs(Locale);
            }

            var asset = await _client.GetAsset(AssetId, queryBuilder);

            output.Attributes.RemoveAll("asset-id");
            output.Attributes.RemoveAll("locale");

            output.Attributes.SetAttribute("href", asset.File.Url);
        }
        private async Task <List <Asset> > RetrieveAssetField(Dictionary <string, object> dict)
        {
            List <Asset> images = new List <Asset>();

            foreach (KeyValuePair <string, object> lan in dict)
            {
                List <object> lanlist = (List <object>)lan.Value;
                Dictionary <string, object> lanDict = (Dictionary <string, object>)lanlist[0];
                foreach (KeyValuePair <string, object> item in lanDict)
                {
                    Dictionary <string, object> sysDict = (Dictionary <string, object>)item.Value;

                    var qb = QueryBuilder <Asset> .New.ContentTypeIs("product");

                    var image = await _contentfulClient.GetAsset((string)sysDict["id"], qb);

                    images.Add(image);
                }
            }

            return(images);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Builds a url to a contentful image using the specified properties.
        /// </summary>
        /// <returns>The url.</returns>
        public async Task <string> BuildUrl()
        {
            if (Asset != null)
            {
                Url = Asset.File?.Url;
                if (string.IsNullOrEmpty(Url))
                {
                    AssetId = Asset.SystemProperties?.Id;
                }
            }

            if (string.IsNullOrEmpty(Url))
            {
                Asset = await _client.GetAsset(AssetId, "");

                Url = Asset.File.Url;
            }

            var contentType = Asset?.File?.ContentType;

            if (contentType == null && !string.IsNullOrEmpty(Url))
            {
                contentType = Url.ToLower().EndsWith(".jpg") || Url.ToLower().EndsWith(".jpeg") ? "image/jpeg" : "";
            }

            var isJpg = (contentType?.ToLower() == "image/jpeg" && Format == ImageFormat.Default) || Format == ImageFormat.Jpg;

            var queryBuilder = new ImageUrlBuilder();

            if (Width > 0)
            {
                queryBuilder.SetWidth(Width);
            }

            if (Height > 0)
            {
                queryBuilder.SetHeight(Height);
            }

            if (JpgQuality.HasValue && isJpg)
            {
                queryBuilder.SetJpgQuality(JpgQuality.Value);
            }

            if (CornerRadius.HasValue)
            {
                queryBuilder.SetCornerRadius(CornerRadius.Value);
            }

            if (!string.IsNullOrEmpty(BackgroundColor))
            {
                queryBuilder.SetBackgroundColor(BackgroundColor);
            }

            queryBuilder.SetFocusArea(FocusArea);
            queryBuilder.SetResizingBehaviour(ResizeBehaviour);
            queryBuilder.SetFormat(Format);

            if (ProgressiveJpg && isJpg)
            {
                queryBuilder.UseProgressiveJpg();
            }

            return($"{Url}{queryBuilder.Build()}");
        }