<commit_before>﻿using System;
using System.IO;
using System.Runtime.Serialization;
using NMaier.sdlna.Server;
using NMaier.sdlna.Thumbnails;

namespace NMaier.sdlna.FileMediaServer.Files
{
  [Serializable]
  internal sealed class Cover : IMediaCoverResource, ISerializable
  {

    private byte[] _bytes;
    private readonly FileInfo file;
    private int height = 240;
    private static readonly Thumbnailer thumber = new Thumbnailer();
    private int width = 240;



    internal Cover(FileInfo aFile, Stream aStream)
    {
      _bytes = thumber.GetThumbnail(aFile.FullName, MediaTypes.IMAGE, aStream, ref width, ref height);
    }

    private Cover(SerializationInfo info, StreamingContext ctx)
    {
      _bytes = info.GetValue("bytes", typeof(byte[])) as byte[];
      width = info.GetInt32("width");
      height = info.GetInt32("height");
    }

    public Cover(FileInfo aFile)
    {
      file = aFile;
    }



    private byte[] bytes
    {
      get
      {
        if (_bytes == null) {
          ForceLoad();
        }
        if (_bytes.Length == 0) {
          throw new NotSupportedException();
        }
        return _bytes;
      }
    }

    internal void ForceLoad()
    {
      if (_bytes == null) {
        _bytes = thumber.GetThumbnail(file, ref width, ref height);
      }
      if (_bytes == null) {
        _bytes = new byte[0];
      }
      if (OnCoverLazyLoaded != null) {
        OnCoverLazyLoaded(this, null);
      }
    }

    public Stream Content
    {
      get { return new MemoryStream(bytes); }
    }

    public string ID
    {
      get { throw new NotImplementedException(); }
    }

    public MediaTypes MediaType
    {
      get { return MediaTypes.IMAGE; }
    }

    public uint? MetaHeight
    {
      get { return (uint)height; }
    }

    public uint? MetaWidth
    {
      get { return (uint)width; }
    }

    public IMediaFolder Parent
    {
      get { throw new NotImplementedException(); }
    }

    public string PN
    {
      get { return "DLNA.ORG_PN=JPEG_TN"; }
    }

    public IHeaders Properties
    {
      get { throw new NotImplementedException(); }
    }

    public long Size
    {
      get { return bytes.Length; }
    }

    public string Title
    {
      get { throw new NotImplementedException(); }
    }

    public DlnaTypes Type
    {
      get { return DlnaTypes.JPEG; }
    }




    internal event EventHandler OnCoverLazyLoaded;




    public int CompareTo(IMediaItem other)
    {
      throw new NotImplementedException();
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctx)
    {
      info.AddValue("bytes", _bytes);
      info.AddValue("width", width);
      info.AddValue("height", height);
    }
  }
}<commit_msg>Avoid crashing when (again and again) when generting Covers<commit_after>﻿using System;
using System.IO;
using System.Runtime.Serialization;
using NMaier.sdlna.Server;
using NMaier.sdlna.Thumbnails;

namespace NMaier.sdlna.FileMediaServer.Files
{
  [Serializable]
  internal sealed class Cover : Logging, IMediaCoverResource, ISerializable
  {

    private byte[] _bytes;
    private readonly FileInfo file;
    private int height = 240;
    private static readonly Thumbnailer thumber = new Thumbnailer();
    private int width = 240;



    internal Cover(FileInfo aFile, Stream aStream)
    {
      _bytes = thumber.GetThumbnail(aFile.FullName, MediaTypes.IMAGE, aStream, ref width, ref height);
    }

    private Cover(SerializationInfo info, StreamingContext ctx)
    {
      _bytes = info.GetValue("bytes", typeof(byte[])) as byte[];
      width = info.GetInt32("width");
      height = info.GetInt32("height");
    }

    public Cover(FileInfo aFile)
    {
      file = aFile;
    }



    private byte[] bytes
    {
      get
      {
        if (_bytes == null) {
          ForceLoad();
        }
        if (_bytes.Length == 0) {
          throw new NotSupportedException();
        }
        return _bytes;
      }
    }

    internal void ForceLoad()
    {
      try {
        if (_bytes == null) {
          _bytes = thumber.GetThumbnail(file, ref width, ref height);
        }
      }
      catch (Exception ex) {
        Warn("Failed to load thumb for " + file.FullName, ex);
      }
      if (_bytes == null) {
        _bytes = new byte[0];
      }
      if (OnCoverLazyLoaded != null) {
        OnCoverLazyLoaded(this, null);
      }
    }

    public Stream Content
    {
      get { return new MemoryStream(bytes); }
    }

    public string ID
    {
      get { throw new NotImplementedException(); }
    }

    public MediaTypes MediaType
    {
      get { return MediaTypes.IMAGE; }
    }

    public uint? MetaHeight
    {
      get { return (uint)height; }
    }

    public uint? MetaWidth
    {
      get { return (uint)width; }
    }

    public IMediaFolder Parent
    {
      get { throw new NotImplementedException(); }
    }

    public string PN
    {
      get { return "DLNA.ORG_PN=JPEG_TN"; }
    }

    public IHeaders Properties
    {
      get { throw new NotImplementedException(); }
    }

    public long Size
    {
      get { return bytes.Length; }
    }

    public string Title
    {
      get { throw new NotImplementedException(); }
    }

    public DlnaTypes Type
    {
      get { return DlnaTypes.JPEG; }
    }




    internal event EventHandler OnCoverLazyLoaded;




    public int CompareTo(IMediaItem other)
    {
      throw new NotImplementedException();
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctx)
    {
      info.AddValue("bytes", _bytes);
      info.AddValue("width", width);
      info.AddValue("height", height);
    }
  }
}
{'fraction_non_alphanumeric': 0.09783183500793231, 'fraction_numerical': 0.004230565838180857, 'mean_word_length': 2.1876404494382022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3707182', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1691, 'n_words': 432}