Esempio n. 1
0
        public async Task <string> UploadFixtureImageAsync(IFormFile file)
        {
            bool isImage = false;

            try {
                isImage = ImageUtils.IsImage(file);
            } catch (ArgumentException) {
                throw new ArgumentException("Image not valid");
            }

            if (isImage)
            {
                string hash = "";
                using (var fileStream = file.OpenReadStream()) {
                    hash = ImageUtils.FileHash(fileStream);
                }

                // check if already exists
                StoredImage existing = await _context.StoredImages.AsNoTracking().SingleOrDefaultAsync(i => i.Hash == hash);

                if (existing != default(StoredImage))
                {
                    return(existing.Id);
                }

                string filePath = Path.Combine("/openld-data/fixture-images", Path.GetRandomFileName());

                // convert image to jpeg if necessary, with white background
                await ImageUtils.SaveAsJpeg(file, filePath);

                StoredImage image = new StoredImage {
                    Path = filePath, Hash = hash
                };

                await _context.StoredImages.AddAsync(image);

                await _context.SaveChangesAsync();

                return(image.Id);
            }
            else
            {
                throw new ArgumentException("Image not valid");
            }
        }
Esempio n. 2
0
        public async Task <StoredImage> CreateSymbolBitmapAsync(string svgPath)
        {
            string bitmapPath = Path.Combine("/openld-data/fixture-symbol-bitmaps", Path.GetRandomFileName());

            ImageUtils.SaveSvgAsPng(svgPath, bitmapPath, 500);

            string hash = "";

            using (var fileStream = new FileStream(bitmapPath, FileMode.Open, FileAccess.Read)) {
                hash = ImageUtils.FileHash(fileStream);
            }

            StoredImage bitmap = new StoredImage {
                Path = bitmapPath, Hash = hash
            };

            _context.StoredImages.Add(bitmap);
            await _context.SaveChangesAsync();

            return(bitmap);
        }