<commit_before>﻿using System;
using System.IO;
using NMaier.SimpleDlna.Server;
using NMaier.SimpleDlna.Server.Metadata;
using NMaier.SimpleDlna.Utilities;

namespace NMaier.SimpleDlna.FileMediaServer
{
  internal class BaseFile : Logging, IMediaResource, IMediaCover, IMetaInfo
  {
    private WeakReference _cover = new WeakReference(null);

    private static readonly LeastRecentlyUsedDictionary<string, Cover> coverCache = new LeastRecentlyUsedDictionary<string, Cover>(500);

    private DateTime? lastModified = null;

    private long? length = null;

    private readonly FileServer server;

    private readonly string title;


    protected BaseFile(FileServer server, FileInfo aFile, DlnaMime aType, DlnaMediaTypes aMediaType)
    {
      if (server == null) {
        throw new ArgumentNullException("server");
      }
      this.server = server;
      Item = aFile;

      length = Item.Length;
      lastModified = Item.LastWriteTimeUtc;

      Type = aType;
      MediaType = aMediaType;

      title = System.IO.Path.GetFileNameWithoutExtension(Item.Name);
      if (string.IsNullOrEmpty(title)) {
        title = Item.Name;
      }
      if (!string.IsNullOrWhiteSpace(title)) {
        title = Uri.UnescapeDataString(title);
      }
      title = title.StemNameBase();
    }


    protected Cover cover
    {
      get
      {
        return _cover.Target as Cover;
      }
      set
      {
        if (value != null) {
          coverCache[Item.FullName] = value;
        }
        _cover = new WeakReference(value);
      }
    }
    protected FileServer Server
    {
      get
      {
        return server;
      }
    }


    internal FileInfo Item
    {
      get;
      set;
    }


    public Stream Content
    {
      get
      {
        try {
          return new FileStream(
            Item.FullName,
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite | FileShare.Delete,
            1 << 17,
            FileOptions.Asynchronous | FileOptions.SequentialScan
            );
        }
        catch (FileNotFoundException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Deleted);
          throw;
        }
        catch (UnauthorizedAccessException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Changed);
          throw;
        }
        catch (IOException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Changed);
          throw;
        }
      }
    }
    public virtual IMediaCoverResource Cover
    {
      get
      {
        if (cover == null && !LoadCoverFromCache()) {
          cover = new Cover(Item);
          cover.OnCoverLazyLoaded += LazyLoadedCover;
        }
        return cover;
      }
    }
    public string Id
    {
      get;
      set;
    }
    public DateTime InfoDate
    {
      get
      {
        if (lastModified == null) {
          lastModified = Item.LastWriteTimeUtc;
        }
        return lastModified.Value;
      }
    }
    public long? InfoSize
    {
      get
      {
        if (length == null) {
          length = Item.Length;
        }
        return length;
      }
    }
    public DlnaMediaTypes MediaType
    {
      get;
      protected set;
    }
    public string Path
    {
      get
      {
        return Item.FullName;
      }
    }
    public string PN
    {
      get
      {
        return DlnaMaps.PN[Type];
      }
    }
    public virtual IHeaders Properties
    {
      get
      {
        var rv = new RawHeaders();
        rv.Add("Title", Title);
        rv.Add("MediaType", MediaType.ToString());
        rv.Add("Type", Type.ToString());
        rv.Add("SizeRaw", InfoSize.ToString());
        rv.Add("Size", InfoSize.Value.FormatFileSize());
        rv.Add("Date", InfoDate.ToString());
        rv.Add("DateO", InfoDate.ToString("o"));
        try {
          if (Cover != null) {
            rv.Add("HasCover", "true");
          }
        }
        catch (Exception) {
        }
        return rv;
      }
    }
    public virtual string Title
    {
      get
      {
        return title;
      }
    }<commit_msg>Log cover errors<commit_after>﻿using System;
using System.IO;
using NMaier.SimpleDlna.Server;
using NMaier.SimpleDlna.Server.Metadata;
using NMaier.SimpleDlna.Utilities;

namespace NMaier.SimpleDlna.FileMediaServer
{
  internal class BaseFile : Logging, IMediaResource, IMediaCover, IMetaInfo
  {
    private WeakReference _cover = new WeakReference(null);

    private static readonly LeastRecentlyUsedDictionary<string, Cover> coverCache = new LeastRecentlyUsedDictionary<string, Cover>(500);

    private DateTime? lastModified = null;

    private long? length = null;

    private readonly FileServer server;

    private readonly string title;


    protected BaseFile(FileServer server, FileInfo aFile, DlnaMime aType, DlnaMediaTypes aMediaType)
    {
      if (server == null) {
        throw new ArgumentNullException("server");
      }
      this.server = server;
      Item = aFile;

      length = Item.Length;
      lastModified = Item.LastWriteTimeUtc;

      Type = aType;
      MediaType = aMediaType;

      title = System.IO.Path.GetFileNameWithoutExtension(Item.Name);
      if (string.IsNullOrEmpty(title)) {
        title = Item.Name;
      }
      if (!string.IsNullOrWhiteSpace(title)) {
        title = Uri.UnescapeDataString(title);
      }
      title = title.StemNameBase();
    }


    protected Cover cover
    {
      get
      {
        return _cover.Target as Cover;
      }
      set
      {
        if (value != null) {
          coverCache[Item.FullName] = value;
        }
        _cover = new WeakReference(value);
      }
    }
    protected FileServer Server
    {
      get
      {
        return server;
      }
    }


    internal FileInfo Item
    {
      get;
      set;
    }


    public Stream Content
    {
      get
      {
        try {
          return new FileStream(
            Item.FullName,
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite | FileShare.Delete,
            1 << 17,
            FileOptions.Asynchronous | FileOptions.SequentialScan
            );
        }
        catch (FileNotFoundException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Deleted);
          throw;
        }
        catch (UnauthorizedAccessException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Changed);
          throw;
        }
        catch (IOException ex) {
          Error("Failed to access: " + Item.FullName, ex);
          server.DelayedRescan(WatcherChangeTypes.Changed);
          throw;
        }
      }
    }
    public virtual IMediaCoverResource Cover
    {
      get
      {
        if (cover == null && !LoadCoverFromCache()) {
          cover = new Cover(Item);
          cover.OnCoverLazyLoaded += LazyLoadedCover;
        }
        return cover;
      }
    }
    public string Id
    {
      get;
      set;
    }
    public DateTime InfoDate
    {
      get
      {
        if (lastModified == null) {
          lastModified = Item.LastWriteTimeUtc;
        }
        return lastModified.Value;
      }
    }
    public long? InfoSize
    {
      get
      {
        if (length == null) {
          length = Item.Length;
        }
        return length;
      }
    }
    public DlnaMediaTypes MediaType
    {
      get;
      protected set;
    }
    public string Path
    {
      get
      {
        return Item.FullName;
      }
    }
    public string PN
    {
      get
      {
        return DlnaMaps.PN[Type];
      }
    }
    public virtual IHeaders Properties
    {
      get
      {
        var rv = new RawHeaders();
        rv.Add("Title", Title);
        rv.Add("MediaType", MediaType.ToString());
        rv.Add("Type", Type.ToString());
        rv.Add("SizeRaw", InfoSize.ToString());
        rv.Add("Size", InfoSize.Value.FormatFileSize());
        rv.Add("Date", InfoDate.ToString());
        rv.Add("DateO", InfoDate.ToString("o"));
        try {
          if (Cover != null) {
            rv.Add("HasCover", "true");
          }
        }
        catch (Exception ex) {
          Debug("Failed to access cover", ex);
        }
        return rv;
      }
    }
    public virtual string Title
    {
      get
      {
        return title;
      }
    }
{'fraction_non_alphanumeric': 0.09649225032047547, 'fraction_numerical': 0.0013984384104416734, 'mean_word_length': 1.7201267828843105, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '1044661', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2449, 'n_words': 536}