Exemple #1
0
        public IActionResult CreateArticle([FromBody] ArticleDto model)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            model.ArticleId = model.ArticleLicenseDto.ArticleId = DateTime.UtcNow.Ticks;
            model.AttachedAssetsInCloudStorageId = Guid.NewGuid();

            int inMemoryCachyExpireDays = Convert.ToInt32(_configuration["InMemoryCacheDays"]);
            if (inMemoryCachyExpireDays <= 0) throw new ArgumentOutOfRangeException(nameof(inMemoryCachyExpireDays));
            string htmlFileName = _configuration["ArticleHtmlTemplateFileNameWithExt"];
            if (string.IsNullOrWhiteSpace(htmlFileName)) throw new ArgumentNullException(nameof(htmlFileName));
            string googleStorageBucketName = _configuration["ArticleBucketNameInGoogleCloudStorage"];
            if (string.IsNullOrWhiteSpace(googleStorageBucketName)) throw new ArgumentOutOfRangeException(nameof(googleStorageBucketName));
            GoogleStorageArticleFileDto fileModel = model.GoogleStorageArticleFileDto;
            fileModel.CacheExpiryDateTimeForHtmlTemplate = DateTime.UtcNow.AddDays(Convert.ToDouble(inMemoryCachyExpireDays));
            fileModel.HtmlFileTemplateFullPathWithExt = Path.Combine(Directory.GetCurrentDirectory(), htmlFileName);
            fileModel.GoogleStorageBucketName = googleStorageBucketName;
            fileModel.CACHE_KEY = Constants.Article_HTML_FILE_TEMPLATE;
            fileModel.GoogleStorageObjectNameWithExt = string.Format("{0}{1}", model.AttachedAssetsInCloudStorageId.ToString("N"), Path.GetExtension(htmlFileName));
            fileModel.ContentType = Utility.GetMimeTypes()[Path.GetExtension(htmlFileName)];

            model.ArticleLicenseDto.ArticleLicenseId = DateTime.UtcNow.Ticks;
            if (!string.IsNullOrWhiteSpace(model.ArticleLicenseDto.License)) model.ArticleLicenseDto.LicensedDate = DateTime.UtcNow;

            ArticleDto articleDto = _articleService.CreateArticle(model);
            return Ok(articleDto);
        }
Exemple #2
0
        private void UploadObjectInGoogleStorage(GoogleStorageArticleFileDto model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(GoogleStorageArticleFileDto));
            }
            if (model.ArticleAnonymousDataObjectForHtmlTemplate == null)
            {
                throw new ArgumentNullException(nameof(model.ArticleAnonymousDataObjectForHtmlTemplate));
            }
            string content = _cacheService.Get <string>(model.CACHE_KEY);

            if (string.IsNullOrWhiteSpace(content))
            {
                content = System.IO.File.ReadAllText(model.HtmlFileTemplateFullPathWithExt);
                if (string.IsNullOrEmpty(content))
                {
                    throw new Exception(nameof(content));
                }
                content = _cacheService.GetOrAdd <string>(model.CACHE_KEY, () => content, model.CacheExpiryDateTimeForHtmlTemplate);
                if (string.IsNullOrEmpty(content))
                {
                    throw new Exception(nameof(content));
                }
            }
            content = _fileReadService.FillContent(content, model.ArticleAnonymousDataObjectForHtmlTemplate);
            if (string.IsNullOrEmpty(content))
            {
                throw new Exception(nameof(content));
            }
            Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(content));

            if (stream == null || stream.Length <= 0)
            {
                throw new Exception(nameof(stream));
            }
            _googleStorage.UploadObject(model.GoogleStorageBucketName, stream, model.GoogleStorageObjectNameWithExt, model.ContentType);
        }