Exemplo n.º 1
0
        public IEnumerable <ImageMeta> GetOlderImagesThan(CyanometerDataSource source, DateTimeOffset now)
        {
            string path = imagesFileManager.GetRelativePhysicalPathForDate(source, now);

            var fileProvider = webHostEnvironment.WebRootFileProvider;

            bool isToday = true;

            logger.LogDebug($"Checking images for {fileProvider.GetFileInfo(Path.Combine("cyano", path)).PhysicalPath}");
            var end = now.AddYears(-1);

            while (now > end)
            {
                var    content = fileProvider.GetDirectoryContents(Path.Combine("cyano", path));
                string uriPath = GetRelativeUriPathForDate(source, now);
                var    images  = imagesFileManager.ReadMetaForDate(source, content, uriPath, isToday);
                foreach (var image in images.OrderByDescending(i => i.Date))
                {
                    yield return(image);
                }
                now     = now.AddDays(-1);
                isToday = false;
                path    = imagesFileManager.GetRelativePhysicalPathForDate(source, now);
            }
        }
            public void GivenSourceAndDate_ConstructsPathCorrectly()
            {
                var ljubljana = new CyanometerDataSource(Guid.NewGuid(), "Slovenia", "Ljubljana", AirQualitySource.Arso,
                                                         rootUriPath: "slovenia/ljubljana", cameraLocationPath: "Central-Square", "E21");

                var actual = Target.GetRelativePhysicalPathForDate(ljubljana, new DateTimeOffset(2017, 8, 2, 5, 38, 36, TimeSpan.Zero));

                Assert.That(actual, Is.EqualTo(Path.Combine("slovenia", "ljubljana", "Central-Square", "2017", "08", "02")));
            }
Exemplo n.º 3
0
 internal ImageMeta CreateMetaFromSourceAndInfo(CyanometerDataSource source, ImageInfo info, string uriPath, string fileName)
 {
     return(new ImageMeta(
                Url.Combine("/", WwwRoot, uriPath, $"{fileName}.jpg"),
                Url.Combine("/", WwwRoot, uriPath, $"thumb-{fileName}.jpg"),
                info.Date,
                info.BluenessIndex
                ));
 }
Exemplo n.º 4
0
        public void SaveImage(CyanometerDataSource source, string wwwRootDirectory, Image <Rgba32> image, Image <Rgba32> thumb, ImageInfo info, string fullName)
        {
            string directory = Path.Combine(wwwRootDirectory, WwwRoot, GetRelativePhysicalPathForDate(source, info.Date));

            Directory.CreateDirectory(directory);
            image.Save(Path.Combine(directory, fullName));
            logger.LogDebug($"Image saved to {Path.Combine(directory, fullName)}");

            string thumbnailName = CreateThumbnailFileName(fullName);

            thumb.Save(Path.Combine(directory, thumbnailName));
            logger.LogDebug($"Image saved to {Path.Combine(directory, thumbnailName)}");

            string infoFileName = CreateInfoFileName(fullName);

            File.WriteAllText(Path.Combine(directory, infoFileName), JsonConvert.SerializeObject(info));
            logger.LogDebug($"Info saved to {Path.Combine(directory, infoFileName)}");
        }
Exemplo n.º 5
0
        public ImmutableArray <ImageMeta> ReadMetaForDate(CyanometerDataSource source, IDirectoryContents content, string uriPath, bool current)
        {
            string key    = $"{CacheKeys.ImageFile}_{uriPath}";
            var    result = cache.GetOrCreate(key, ce =>
            {
                logger.LogInformation("Missed cache on image files {key}", key);
                var query                     = from f in content
                                      let ext = Path.GetExtension(f.Name)
                                                where string.Equals(ext, ".info", System.StringComparison.Ordinal)
                                                let fn                 = Path.GetFileNameWithoutExtension(f.Name)
                                                                 let i = ReadImageInfoFromInfoFile(f)
                                                                         orderby i.Date descending
                                                                         select CreateMetaFromSourceAndInfo(source, i, uriPath, fn);
                ce.SetAbsoluteExpiration(current ? TimeSpan.FromMinutes(10) : TimeSpan.FromDays(1));
                return(query.ToImmutableArray());
            });

            return(result);
        }
Exemplo n.º 6
0
        public void SaveImage(CyanometerDataSource source, string fileName, Stream stream)
        {
            string         safeFileName = Path.GetFileName(fileName);
            DateTimeOffset takenAt      = DateFromFileName(safeFileName);

            logger.LogDebug($"File date is {takenAt}");
            using (var image = Image.Load <Rgba32>(stream))
                using (var thumb = image.Clone())
                {
                    thumb.Mutate(x => x.Resize(800, 600));
                    var colors = CollectColors(thumb);
                    var info   = new ImageInfo(
                        takenAt,
                        calculator.GetBluenessIndexTopPixels(colors, 30).Index
                        );
                    logger.LogDebug("Thumbnail created, saving");
                    var fileProvider = (PhysicalFileProvider)webHostEnvironment.WebRootFileProvider;
                    imagesFileManager.SaveImage(source, fileProvider.Root, image, thumb, info, safeFileName);
                }
        }
Exemplo n.º 7
0
 internal string GetRelativeUriPathForDate(CyanometerDataSource source, DateTimeOffset now)
 {
     return(Url.Combine(source.RootUriPath, source.CameraLocationPath,
                        now.Year.ToString("0000"), now.Month.ToString("00"), now.Day.ToString("00")));
 }
Exemplo n.º 8
0
 public string GetRelativePhysicalPathForDate(CyanometerDataSource source, DateTimeOffset now)
 {
     return(Path.Combine(source.RootDiskPath, source.CameraLocationPath,
                         now.Year.ToString("0000"), now.Month.ToString("00"), now.Day.ToString("00")));
 }