コード例 #1
0
        /// <summary>
        /// Tries to read an nfo-file into a <see cref="XDocument"/>
        /// </summary>
        /// <param name="nfoFsra"><see cref="IFileSystemResourceAccessor"/> pointing to the nfo-file</param>
        /// <returns><c>true</c> if any usable metadata was found; else <c>false</c></returns>
        public virtual async Task <bool> TryReadMetadataAsync(IFileSystemResourceAccessor nfoFsra)
        {
            var nfoFileWrittenToDebugLog = false;

            // Make sure the nfo-file was read into _nfoBytes as byte array
            if (_nfoBytes == null && !await TryReadNfoFileAsync(nfoFsra).ConfigureAwait(false))
            {
                return(false);
            }

            if (_settings.EnableDebugLogging && _settings.WriteRawNfoFileIntoDebugLog)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                // TryReadNfoFileAsync makes sure that _nfoBytes is not null
                using (var nfoMemoryStream = new MemoryStream(_nfoBytes))
                    using (var nfoReader = new StreamReader(nfoMemoryStream, true))
                    {
                        var nfoString = nfoReader.ReadToEnd();
                        _debugLogger.Debug("[#{0}]: Nfo-file (Encoding: {1}):{2}{3}", _miNumber, nfoReader.CurrentEncoding, Environment.NewLine, nfoString);
                        nfoFileWrittenToDebugLog = true;
                    }
            }

            try
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                // TryReadNfoFileAsync makes sure that _nfoBytes is not null
                using (var memoryNfoStream = new MemoryStream(_nfoBytes))
                    using (var xmlReader = new XmlNfoReader(memoryNfoStream))
                    {
                        var nfoDocument = XDocument.Load(xmlReader);
                        return(await TryReadNfoDocumentAsync(nfoDocument, nfoFsra).ConfigureAwait(false));
                    }
            }
            catch (Exception e)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                // TryReadNfoFileAsync makes sure that _nfoBytes is not null
                using (var nfoMemoryStream = new MemoryStream(_nfoBytes))
                    using (var nfoReader = new StreamReader(nfoMemoryStream, true))
                    {
                        try
                        {
                            if (!nfoFileWrittenToDebugLog)
                            {
                                var nfoString = nfoReader.ReadToEnd();
                                _debugLogger.Warn("[#{0}]: Cannot parse nfo-file with XMLReader (Encoding: {1}):{2}{3}", e, _miNumber, nfoReader.CurrentEncoding, Environment.NewLine, nfoString);
                            }
                        }
                        catch (Exception ex)
                        {
                            _debugLogger.Error("[#{0}]: Cannot extract metadata; neither XMLReader can parse nor StreamReader can read the bytes read from the nfo-file", ex, _miNumber);
                        }
                    }
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Tries to read an nfo-file into a <see cref="XDocument"/>
        /// </summary>
        /// <param name="nfoFsra"><see cref="IFileSystemResourceAccessor"/> pointing to the nfo-file</param>
        /// <returns><c>true</c> if any usable metadata was found; else <c>false</c></returns>
        public virtual async Task <bool> TryReadMetadataAsync(IFileSystemResourceAccessor nfoFsra)
        {
            byte[] nfoBytes;
            var    nfoFileWrittenToDebugLog = false;

            try
            {
                using (var nfoStream = await nfoFsra.OpenReadAsync().ConfigureAwait(false))
                {
                    // For xml-files it is recommended to read them as byte array. Reason is that reading as byte array does
                    // not yet consider any encoding. After that, it is recommended to use the XmlReader (instead of a StreamReader)
                    // because the XmlReader first considers the "Byte Order Mark" ("BOM"). If such is not present, UTF-8 is used.
                    // If the XML declaration contains an encoding attribute (which is optional), the XmlReader (contrary to the
                    // StreamReader) automatically switches to the enconding specified by the XML declaration.
                    nfoBytes = new byte[nfoStream.Length];
                    await nfoStream.ReadAsync(nfoBytes, 0, (int)nfoStream.Length).ConfigureAwait(false);

                    if (_settings.EnableDebugLogging && _settings.WriteRawNfoFileIntoDebugLog)
                    {
                        using (var nfoMemoryStream = new MemoryStream(nfoBytes))
                            using (var nfoReader = new StreamReader(nfoMemoryStream, true))
                            {
                                var nfoString = nfoReader.ReadToEnd();
                                _debugLogger.Debug("[#{0}]: Nfo-file (Encoding: {1}):{2}{3}", _miNumber, nfoReader.CurrentEncoding, Environment.NewLine, nfoString);
                                nfoFileWrittenToDebugLog = true;
                            }
                    }
                }
            }
            catch (Exception e)
            {
                _debugLogger.Error("[#{0}]: Cannot extract metadata; cannot read nfo-file", e, _miNumber);
                return(false);
            }

            try
            {
                using (var memoryNfoStream = new MemoryStream(nfoBytes))
                    using (var xmlReader = new XmlNfoReader(memoryNfoStream))
                    {
                        var nfoDocument = XDocument.Load(xmlReader);
                        return(await TryReadNfoDocumentAsync(nfoDocument, nfoFsra).ConfigureAwait(false));
                    }
            }
            catch (Exception e)
            {
                using (var nfoMemoryStream = new MemoryStream(nfoBytes))
                    using (var nfoReader = new StreamReader(nfoMemoryStream, true))
                    {
                        try
                        {
                            if (!nfoFileWrittenToDebugLog)
                            {
                                var nfoString = nfoReader.ReadToEnd();
                                _debugLogger.Warn("[#{0}]: Cannot extract metadata; cannot parse nfo-file with XMLReader (Encoding: {1}):{2}{3}", e, _miNumber, nfoReader.CurrentEncoding, Environment.NewLine, nfoString);
                            }
                        }
                        catch (Exception)
                        {
                            _debugLogger.Error("[#{0}]: Cannot extract metadata; neither XMLReader can parse nor StreamReader can read the bytes read from the nfo-file", e, _miNumber);
                        }
                    }
                return(false);
            }
        }