Ejemplo n.º 1
0
        /// <summary>
        /// Gets the size of the image.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="imagePath">The image path.</param>
        /// <returns>ImageSize.</returns>
        private ImageSize GetImageSize(string keyName, string imagePath)
        {
            // Now check the file system cache
            var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".pb");

            try
            {
                var result = _protobufSerializer.DeserializeFromFile <int[]>(fullCachePath);

                return(new ImageSize {
                    Width = result[0], Height = result[1]
                });
            }
            catch (FileNotFoundException)
            {
                // Cache file doesn't exist no biggie
            }

            var size = ImageHeader.GetDimensions(imagePath, _logger);

            var imageSize = new ImageSize {
                Width = size.Width, Height = size.Height
            };

            // Update the file system cache
            CacheImageSize(fullCachePath, size.Width, size.Height);

            return(imageSize);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the size of the image.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="imagePath">The image path.</param>
        /// <returns>ImageSize.</returns>
        private async Task <ImageSize> GetImageSize(string keyName, string imagePath)
        {
            // Now check the file system cache
            var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt");

            try
            {
                var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray();

                return(new ImageSize {
                    Width = result[0], Height = result[1]
                });
            }
            catch (IOException)
            {
                // Cache file doesn't exist or is currently being written to
            }

            var semaphore = GetLock(fullCachePath);

            await semaphore.WaitAsync().ConfigureAwait(false);

            try
            {
                var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray();

                return(new ImageSize {
                    Width = result[0], Height = result[1]
                });
            }
            catch (FileNotFoundException)
            {
                // Cache file doesn't exist no biggie
            }
            catch (DirectoryNotFoundException)
            {
                // Cache file doesn't exist no biggie
            }
            catch
            {
                semaphore.Release();

                throw;
            }

            try
            {
                var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false);

                var parentPath = Path.GetDirectoryName(fullCachePath);

                if (!Directory.Exists(parentPath))
                {
                    Directory.CreateDirectory(parentPath);
                }

                // Update the file system cache
                File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture));

                return(new ImageSize {
                    Width = size.Width, Height = size.Height
                });
            }
            finally
            {
                semaphore.Release();
            }
        }