Пример #1
0
        public async Task <MappingContent> GetMapping(string templateName, string templateVersion, string mappingName, string mappingVersion)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException(nameof(templateName));
            }
            if (string.IsNullOrEmpty(mappingName))
            {
                throw new ArgumentNullException(nameof(mappingName));
            }

            if (string.IsNullOrEmpty(mappingVersion))
            {
                return(GetLatestMapping(templateName, templateVersion, mappingName));
            }
            else
            {
                if (string.IsNullOrEmpty(templateVersion))
                {
                    templateVersion = await GetLatestTemplateVersion(templateName);
                }
                var prefix          = $"{templateName}_{templateVersion}_{mappingName}";
                var containerClient = GetMappingsContainer();
                var blobItem        = containerClient
                                      .GetBlobs(BlobTraits.Metadata, BlobStates.Snapshots, prefix)
                                      .FirstOrDefault(o => o.Metadata[MAPPING_VERSION_KEY] == mappingVersion);
                if (blobItem == null)
                {
                    return(null);
                }
                var blobClient = containerClient.GetBlobClient(blobItem.Name);
                var contents   = DownloadContentItem(blobClient, blobItem);
                return(ContentItemFactory.BuildMapping(blobClient.Uri, blobItem, contents));
            }
        }
Пример #2
0
        /// <summary>
        /// The <see cref="CreateMapping(string, string, Stream)"/> operation creates a new mapping.
        /// If the mapping with the same name already exists, the operation creates a new version.
        /// </summary>
        /// <param name="templateName">The name of the parent template.</param>
        /// <param name="mappingName">The name of mapping to create.</param>
        /// <param name="contents">The <see cref="Stream"/> that contains a valid Excel document.</param>
        /// <returns>A <see cref="ContentItem"/> describing the state of the created mapping.</returns>
        /// <remarks>
        /// An <see cref="ArgumentNullException"/> will be thrown if any of the parameters are null or empty.
        /// A <see cref="RequestFailedException"/> will be thrown if a failure occurs.
        /// </remarks>
        public async Task <MappingContent> CreateMapping(string templateName, string mappingName, Stream contents)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException(nameof(templateName));
            }
            if (string.IsNullOrEmpty(mappingName))
            {
                throw new ArgumentNullException(nameof(mappingName));
            }
            if (contents == null || contents.Length == 0)
            {
                throw new ArgumentNullException(nameof(contents));
            }

            var templateVersion = await GetLatestTemplateVersion(templateName);

            if (templateVersion == null)
            {
                throw new ArgumentException(nameof(templateName));
            }

            var containerClient = GetMappingsContainer();
            var blobFileName    = $"{templateName}_{templateVersion}_{mappingName}.xlsm";
            var blobClient      = containerClient.GetBlobClient(blobFileName);

            var mappingVersion = await TryCreateSnapshot(blobClient, MAPPING_VERSION_KEY);

            var metadata = new Dictionary <string, string>
            {
                [TEMPLATE_NAME_KEY]    = templateName,
                [TEMPLATE_VERSION_KEY] = templateVersion,
                [MAPPING_NAME_KEY]     = mappingName,
                [MAPPING_VERSION_KEY]  = mappingVersion
            };

            var response = await blobClient.UploadAsync(contents, metadata : metadata);

            var blobContentInfo = response.Value;

            return(ContentItemFactory.BuildMapping(blobClient.Uri, blobContentInfo, metadata, contents));
        }
Пример #3
0
        public MappingContent GetLatestMapping(string templateName, string templateVersion, string mappingName)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException(nameof(templateName));
            }

            if (string.IsNullOrEmpty(templateVersion))
            {
                templateVersion = GetLatestTemplateVersion(templateName).GetAwaiter().GetResult();
            }
            if (string.IsNullOrEmpty(templateVersion))
            {
                throw new ArgumentNullException(nameof(templateVersion));
            }

            if (string.IsNullOrEmpty(mappingName))
            {
                throw new ArgumentNullException(nameof(mappingName));
            }
            var mappingVersionName = $"{templateName}_{templateVersion}_{mappingName}";

            try
            {
                var containerClient  = GetMappingsContainer();
                var blobFileName     = $"{mappingVersionName}.xlsm";
                var blobClient       = containerClient.GetBlobClient(blobFileName);
                var blobDownloadInfo = DownloadContentItem(blobClient);
                return(ContentItemFactory.BuildMapping(blobClient.Uri, blobDownloadInfo));
            }
            catch (RequestFailedException ex)
            {
                if (ex.Status != 404)
                {
                    throw ex;
                }
            }
            return(null);
        }