コード例 #1
0
        public async Task UploadTemplateFile(Guid templateId, Stream fileContent, string fileName, string version = null, ChangesType?changesType = null)
        {
            if (string.IsNullOrEmpty(version) && !changesType.HasValue)
            {
                throw new Exception($"Both Version and ChangesType are null or empty.");
            }

            var template = await _templateService.GetTemplate(templateId);

            if (template == null)
            {
                throw new Exception($"Template {templateId} does not found.");
            }

            if (string.IsNullOrEmpty(version))
            {
                var lastVersion = template.Versions.OrderBy(v => v.CreatedDate).LastOrDefault();

                if (lastVersion == null)
                {
                    version = "1.00";
                }
                else
                {
                    if (decimal.TryParse(lastVersion.Version, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal currentVersion))
                    {
                        version = IncrementVersionNumber(currentVersion, changesType.Value);
                    }
                    else
                    {
                        throw new Exception($"Template last version must be Decimal, {lastVersion.Version} is not valid value.");
                    }
                }
            }

            var fileExtension       = Path.GetExtension(fileName);
            var fileNameWithVersion = fileName.Replace(fileExtension, $"_{version.Trim()}{fileExtension}");

            await _fileStorage.UploadFile(fileNameWithVersion, fileContent);

            await _templateService.AddTemplateVersion(templateId, version, fileNameWithVersion);
        }