public static Task <Stream> ProcessAsync(IOwinContext context, WebMediaType mediatype, WebFileType filetype, string id, int offset)
        {
            ISet <Guid> necessaryMIATypes = new HashSet <Guid>();

            necessaryMIATypes.Add(MediaAspect.ASPECT_ID);
            necessaryMIATypes.Add(ProviderResourceAspect.ASPECT_ID);
            necessaryMIATypes.Add(ImporterAspect.ASPECT_ID);

            MediaItem item = MediaLibraryAccess.GetMediaItemById(context, id, necessaryMIATypes, null);

            if (item == null)
            {
                throw new NotFoundException("RetrieveFile: no media item found");
            }

            var files = ResourceAccessUtils.GetResourcePaths(item);
            var ra    = GetResourceAccessor(files[offset]);
            IFileSystemResourceAccessor fsra = ra as IFileSystemResourceAccessor;

            if (fsra == null)
            {
                throw new InternalServerException("RetrieveFile: failed to create IFileSystemResourceAccessor");
            }

            return(fsra.OpenReadAsync());
        }
 /// <summary>
 /// Tries to read an nfo-file into a byte array (<see cref="_nfoBytes"/>)
 /// </summary>
 /// <param name="nfoFsra">FileSystemResourceAccessor pointing to the nfo-file</param>
 /// <returns><c>true</c>, if the file was successfully read; otherwise <c>false</c></returns>
 protected async Task <bool> TryReadNfoFileAsync(IFileSystemResourceAccessor nfoFsra)
 {
     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);
         }
     }
     catch (Exception e)
     {
         _debugLogger.Error("[#{0}]: Cannot extract metadata; cannot read nfo-file", e, _miNumber);
         return(false);
     }
     return(true);
 }
        /// <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);
            }
        }