示例#1
0
        private async Task <IActionResult> UploadNewCategoryImageActionAsync(int id, IFormFile file)
        {
            var category = _unitOfWork.Repository <Categories>().GetById(id);

            if (category == null)
            {
                return(NotFound());
            }

            await using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream).ConfigureAwait(false);

                category.Picture = memoryStream.ToArray();
            }

            _unitOfWork.Repository <Categories>().Update(category);
            _unitOfWork.Context.SaveChanges();

            var filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var fs       = _imageHelper.CreateImageFileStream(filePath, category.Picture);

            var uri = Url.Link("GetImage", new { id = category.CategoryId });

            return(Created(uri, fs));
        }
示例#2
0
        public void CacheImage(int id, byte[] bytes)
        {
            _memoryCache.Remove(id);

            var tempPath = _config["TempDirectoryPath"];

            var filePath = Path.Combine(string.IsNullOrWhiteSpace(tempPath) ? Path.GetTempPath() : tempPath, Path.GetRandomFileName());

            _imageHelper.CreateImageFileStream(filePath, bytes);

            var cacheEntryOptions = GetMemoryCacheEntryOptions();

            _memoryCache.Set(id, filePath, cacheEntryOptions);

            if (_memoryCache is MemoryCache cache && cache.Count > _maxTotalAmountOfCachedItems)
            {
                cache.Compact(0.5);
            }
        }
        private FileStream GetImageFileStream(byte[] bytes)
        {
            var filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            return(_imageHelper.CreateImageFileStream(filePath, bytes));
        }