Esempio n. 1
0
        private async Task <string> ImportImage(string fileName, string id, bool overwrite)
        {
            if (FileMetaDataStore.Contains(fileName))
            {
                return(fileName);
            }
            var prefix = this.GetType().Name;
            var result = default(string);

            if (overwrite || !FileMetaDataStore.Exists(prefix, id, out result))
            {
#if NET40
                Semaphore.Wait();
#else
                await Semaphore.WaitAsync().ConfigureAwait(false);
#endif
                try
                {
                    if (overwrite || !FileMetaDataStore.Exists(prefix, id, out result))
                    {
                        return(await FileMetaDataStore.WriteAsync(prefix, id, fileName).ConfigureAwait(false));
                    }
                }
                finally
                {
                    Semaphore.Release();
                }
            }
            return(result);
        }
Esempio n. 2
0
 public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height, bool cache)
 {
     try
     {
         var id = this.GetImageId(libraryHierarchyNode, width, height);
         if (cache)
         {
             var fileName = default(string);
             if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
             {
                 return(this.ImageLoader.Load(fileName, 0, 0, true));
             }
         }
         using (KeyLock.Lock(id))
         {
             if (cache)
             {
                 var fileName = default(string);
                 if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                 {
                     return(this.ImageLoader.Load(fileName, 0, 0, true));
                 }
             }
             return(this.CreateImageSourceCore(libraryHierarchyNode, width, height, cache));
         }
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Error, "Error creating image source: {0}", e.Message);
         return(null);
     }
 }
Esempio n. 3
0
        protected virtual string Resize(string id, Func <Image> factory, int width, int height)
        {
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(fileName);
            }
            //TODO: Setting throwOnTimeout = false so we ignore synchronization timeout.
            //TODO: I think there exists a deadlock bug in KeyLock but I haven't been able to prove it.
            using (KeyLock.Lock(id, TIMEOUT, false))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(fileName);
                }
                using (var image = new Bitmap(width, height))
                {
                    using (var graphics = Graphics.FromImage(image))
                    {
                        graphics.SmoothingMode     = SmoothingMode.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        this.Resize(graphics, factory, width, height);
                    }
                    using (var stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        stream.Seek(0, SeekOrigin.Begin);
                        return(FileMetaDataStore.Write(PREFIX, id, stream));
                    }
                }
            }
        }
Esempio n. 4
0
        protected virtual string Resize(string id, Func <Image> factory, int width, int height)
        {
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(fileName);
            }
            using (KeyLock.Lock(id))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(fileName);
                }
                using (var image = new Bitmap(width, height))
                {
                    using (var graphics = Graphics.FromImage(image))
                    {
                        graphics.SmoothingMode     = SmoothingMode.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        this.Resize(graphics, factory, width, height);
                    }
                    using (var stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        stream.Seek(0, SeekOrigin.Begin);
                        return(FileMetaDataStore.Write(PREFIX, id, stream));
                    }
                }
            }
        }
Esempio n. 5
0
 public void Clear()
 {
     try
     {
         FileMetaDataStore.Clear(PREFIX);
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Warn, "Failed to clear storage \"{0}\": {1}", PREFIX, e.Message);
     }
 }
Esempio n. 6
0
        private static Task <string> ImportImage(string fileName, string id, bool overwrite)
        {
            if (FileMetaDataStore.Contains(fileName))
            {
                //The file is already in the data store.
#if NET40
                return(TaskEx.FromResult(fileName));
#else
                return(Task.FromResult(fileName));
#endif
            }
            return(FileMetaDataStore.IfNotExistsAsync(PREFIX, id, result => FileMetaDataStore.CopyAsync(PREFIX, id, fileName), overwrite));
        }
Esempio n. 7
0
 public static void Cleanup()
 {
     try
     {
         var instance = ComponentRegistry.Instance.GetComponent <WaveFormCache>();
         if (instance != null)
         {
             instance.Store.Clear();
         }
         FileMetaDataStore.Clear(PREFIX);
     }
     catch (Exception e)
     {
         Logger.Write(typeof(WaveFormCache), LogLevel.Warn, "Failed to clear caches: {0}", e.Message);
     }
 }
Esempio n. 8
0
 protected virtual void Save(string id, WaveFormGenerator.WaveFormGeneratorData data)
 {
     try
     {
         using (var stream = new MemoryStream())
         {
             Serializer.Instance.Write(stream, data);
             stream.Seek(0, SeekOrigin.Begin);
             FileMetaDataStore.Write(PREFIX, id, stream);
             stream.Seek(0, SeekOrigin.Begin);
         }
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Warn, "Failed to save wave form: {0}", e.Message);
     }
 }
Esempio n. 9
0
        private ImageSource Render(LibraryHierarchyNode libraryHierarchyNode, DrawingVisual visual, int width, int height)
        {
            var target = new RenderTargetBitmap(width, height, DPIX, DPIY, PixelFormats.Pbgra32);

            target.Render(visual);
            var encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(target));
            using (var stream = new MemoryStream())
            {
                encoder.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                FileMetaDataStore.Write(PREFIX, this.GetImageId(libraryHierarchyNode, width, height), stream);
                stream.Seek(0, SeekOrigin.Begin);
            }
            target.Freeze();
            return(target);
        }
Esempio n. 10
0
        public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height)
        {
            var id       = this.GetImageId(libraryHierarchyNode, width, height);
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(this.ImageLoader.Load(id, fileName, 0, 0, true));
            }
            //TODO: Setting throwOnTimeout = false so we ignore synchronization timeout.
            //TODO: I think there exists a deadlock bug in KeyLock but I haven't been able to prove it.
            using (KeyLock.Lock(id, TIMEOUT, false))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(this.ImageLoader.Load(id, fileName, 0, 0, true));
                }
                return(this.CreateImageSourceCore(libraryHierarchyNode, width, height));
            }
        }
Esempio n. 11
0
        protected virtual async Task <string> ImportImage(Discogs.ReleaseLookup releaseLookup)
        {
            var urls = new[]
            {
                releaseLookup.Release.CoverUrl,
                releaseLookup.Release.ThumbUrl
            };

            foreach (var url in urls)
            {
                if (!string.IsNullOrEmpty(url))
                {
                    try
                    {
                        var fileName = await FileMetaDataStore.IfNotExistsAsync(PREFIX, url, async result =>
                        {
                            Logger.Write(this, LogLevel.Debug, "Downloading data from url: {0}", url);
                            var data = await this.Discogs.GetData(url).ConfigureAwait(false);
                            if (data == null)
                            {
                                Logger.Write(this, LogLevel.Error, "Failed to download data from url \"{0}\": Unknown error.", url);
                                return(string.Empty);
                            }
                            return(await FileMetaDataStore.WriteAsync(PREFIX, url, data).ConfigureAwait(false));
                        }).ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            return(fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Write(this, LogLevel.Error, "Failed to download data from url \"{0}\": {1}", url, e.Message);
                        releaseLookup.AddError(e.Message);
                    }
                }
            }
            return(string.Empty);
        }
Esempio n. 12
0
        public WaveFormGenerator.WaveFormGeneratorData GetOrCreate(IOutputStream stream, int resolution, Func <WaveFormGenerator.WaveFormGeneratorData> factory)
        {
            var key = new Key(stream.FileName, stream.Length, resolution);

            return(this.Store.GetOrAdd(key, () =>
            {
                if (this.Enabled.Value)
                {
                    var id = this.GetDataId(stream, resolution);
                    var fileName = default(string);
                    if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                    {
                        var data = default(WaveFormGenerator.WaveFormGeneratorData);
                        if (this.TryLoad(fileName, out data))
                        {
                            return data;
                        }
                    }
                }
                return factory();
            }));
        }
 public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height, bool cache)
 {
     var id = this.GetImageId(libraryHierarchyNode, width, height);
     if (cache)
     {
         var fileName = default(string);
         if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
         {
             return this.ImageLoader.Load(fileName, 0, 0, true);
         }
     }
     using (KeyLock.Lock(id))
     {
         if (cache)
         {
             var fileName = default(string);
             if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
             {
                 return this.ImageLoader.Load(fileName, 0, 0, true);
             }
         }
         return this.CreateImageSourceCore(libraryHierarchyNode, width, height, cache);
     }
 }
Esempio n. 14
0
 private Task <string> ImportImage(string fileName, string id)
 {
     return(FileMetaDataStore.IfNotExistsAsync(PREFIX, id, result => FileMetaDataStore.CopyAsync(PREFIX, id, fileName)));
 }
Esempio n. 15
0
 private static Task <string> ImportImage(IPicture value, string id, bool overwrite)
 {
     return(FileMetaDataStore.IfNotExistsAsync(PREFIX, id, result => FileMetaDataStore.WriteAsync(PREFIX, id, value.Data.Data), overwrite));
 }
Esempio n. 16
0
 public static void Cleanup()
 {
     FileMetaDataStore.Clear(PREFIX);
 }