From 31c8c3bf7f1cb5e79d36b0b1d5c28907ea526011 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 27 Oct 2016 18:54:56 -0400 Subject: make open subtitle project portable --- OpenSubtitlesHandler/Utilities.cs | 57 +++++++++++++++------------------------ 1 file changed, 21 insertions(+), 36 deletions(-) (limited to 'OpenSubtitlesHandler/Utilities.cs') diff --git a/OpenSubtitlesHandler/Utilities.cs b/OpenSubtitlesHandler/Utilities.cs index 9a90462f6..56fc0589d 100644 --- a/OpenSubtitlesHandler/Utilities.cs +++ b/OpenSubtitlesHandler/Utilities.cs @@ -20,10 +20,10 @@ using System; using System.Collections.Generic; using System.Text; using System.IO; -using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; +using MediaBrowser.Model.Cryptography; namespace OpenSubtitlesHandler { @@ -32,50 +32,33 @@ namespace OpenSubtitlesHandler /// public sealed class Utilities { + public static ICryptographyProvider CryptographyProvider { get; set; } private const string XML_RPC_SERVER = "https://api.opensubtitles.org/xml-rpc"; /// /// Compute movie hash /// - /// The complete media file path /// The hash as Hexadecimal string - public static string ComputeHash(string fileName) + public static string ComputeHash(Stream stream) { - byte[] hash = MovieHasher.ComputeMovieHash(File.OpenRead(fileName)); + byte[] hash = MovieHasher.ComputeMovieHash(stream); return MovieHasher.ToHexadecimal(hash); } /// - /// Compute md5 for a file - /// - /// The complete file path - /// MD5 of the file - public static string ComputeMd5(string filename) - { - var md5 = MD5.Create(); - var sb = new StringBuilder(); - Stream str = new FileStream(filename, FileMode.Open, FileAccess.Read); - - foreach (var b in md5.ComputeHash(str)) - sb.Append(b.ToString("x2").ToLower()); - str.Close(); - return sb.ToString(); - } - /// /// Decompress data using GZip /// /// The stream that hold the data /// Bytes array of decompressed data public static byte[] Decompress(Stream dataToDecompress) { - MemoryStream target = new MemoryStream(); - - using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress, - System.IO.Compression.CompressionMode.Decompress)) + using (MemoryStream target = new MemoryStream()) { - decompressionStream.CopyTo(target); + using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress, System.IO.Compression.CompressionMode.Decompress)) + { + decompressionStream.CopyTo(target); + } + return target.ToArray(); } - return target.GetBuffer(); - } /// @@ -127,17 +110,19 @@ namespace OpenSubtitlesHandler /// The string of the stream after decode using given encoding public static string GetStreamString(Stream responseStream, Encoding encoding) { - // Handle response, should be XML text. - List data = new List(); - while (true) + using (responseStream) { - int r = responseStream.ReadByte(); - if (r < 0) - break; - data.Add((byte)r); + // Handle response, should be XML text. + List data = new List(); + while (true) + { + int r = responseStream.ReadByte(); + if (r < 0) + break; + data.Add((byte)r); + } + return encoding.GetString(data.ToArray()); } - responseStream.Close(); - return encoding.GetString(data.ToArray()); } /// /// Handle server response stream and decode it as ASCII encoding string. -- cgit v1.2.3