[gnome-subtitles] Fix bug #607229 - Opening large files, based on a patch from Pawel Brzeski.



commit 22bfffd0187e7610c7f04804c7afee2b6f314d08
Author: Pedro Castro <mail pedrocastro org>
Date:   Tue Jun 14 23:13:49 2011 +0100

    Fix bug #607229 - Opening large files, based on a patch from Pawel Brzeski.

 gnome-subtitles.mdp                                |    3 +-
 .../Unmanaged/SubtitleFileOpenErrorDialog.cs       |    2 +
 src/SubLib/Core/SubtitleFactory.cs                 |   27 ++++++++++++++-
 src/SubLib/Exceptions/FileTooLargeException.cs     |   36 ++++++++++++++++++++
 4 files changed, 65 insertions(+), 3 deletions(-)
---
diff --git a/gnome-subtitles.mdp b/gnome-subtitles.mdp
index 2aa6bcf..7081637 100644
--- a/gnome-subtitles.mdp
+++ b/gnome-subtitles.mdp
@@ -2,6 +2,7 @@
   <Policies>
     <TextStylePolicy FileWidth="120" TabWidth="4" RemoveTrailingWhitespace="True" EolMarker="Unix" inheritsSet="Mono" inheritsScope="text/plain" />
   </Policies>
+  <Deployment.LinuxDeployData generateScript="False" />
   <Configurations active="Debug">
     <Configuration name="Debug" ctype="DotNetProjectConfiguration">
       <Output directory="build" assembly="gnome-subtitles" assemblyKeyFile="/home/noup/Workspace/gnome-subtitles/." />
@@ -258,6 +259,7 @@
     <File subtype="Code" buildaction="Compile" name="src/SubLib/Core/Timing/SplitOperator.cs" />
     <File subtype="Code" buildaction="Compile" name="src/SubLib/Core/Timing/MergeOperator.cs" />
     <File subtype="Code" buildaction="Compile" name="src/GnomeSubtitles/Core/Command/MergeSubtitlesCommand.cs" />
+    <File subtype="Code" buildaction="Compile" name="src/SubLib/Exceptions/FileTooLargeException.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
@@ -271,6 +273,5 @@
     <ProjectReference type="Gac" localcopy="True" refto="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   </References>
   <LanguageParameters StartupObject="GnomeSubtitles.Execution.Executable" ApplicationIcon="." ctype="CSharpProjectParameters" />
-  <Deployment.LinuxDeployData generateScript="False" />
   <DeploymentInformation strategy="File" />
 </Project>
\ No newline at end of file
diff --git a/src/GnomeSubtitles/Dialog/Unmanaged/SubtitleFileOpenErrorDialog.cs b/src/GnomeSubtitles/Dialog/Unmanaged/SubtitleFileOpenErrorDialog.cs
index 099ce75..24425ad 100644
--- a/src/GnomeSubtitles/Dialog/Unmanaged/SubtitleFileOpenErrorDialog.cs
+++ b/src/GnomeSubtitles/Dialog/Unmanaged/SubtitleFileOpenErrorDialog.cs
@@ -47,6 +47,8 @@ public class SubtitleFileOpenErrorDialog : FileOpenErrorDialog {
 			return Catalog.GetString("The specified file is invalid.");
 		else if (exception is FileNotFoundException)
 			return Catalog.GetString("The file could not be found.");
+		else if (exception is FileTooLargeException)
+			return Catalog.GetString("The file appears to be too large for a text-based subtitle file.");
 		else
 			return String.Empty;
 	}
diff --git a/src/SubLib/Core/SubtitleFactory.cs b/src/SubLib/Core/SubtitleFactory.cs
index 0700db0..0076a59 100644
--- a/src/SubLib/Core/SubtitleFactory.cs
+++ b/src/SubLib/Core/SubtitleFactory.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2008,2011 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,10 +18,12 @@
  */
 
 using SubLib.Core.Domain;
+using SubLib.Exceptions;
 using SubLib.IO.SubtitleFormats;
 using SubLib.IO.Input;
 using SubLib.IO.Output;
 using System;
+using System.IO;
 using System.Text;
 using System.Text.RegularExpressions;
 
@@ -38,9 +40,13 @@ public class SubtitleFactory {
 	private Encoding encoding = null; //The encoding to be used to open a file 
 	private Encoding fallbackEncoding = Encoding.GetEncoding(1252); //The encoding to fall back to when no encoding is detected
 	private float inputFrameRate = 25; //The frame rate to be used to open a frame-based file
+	private int maxFileSize = 1000000; //The size limit for a subtitle file, in bytes
 	
 	private SubtitleType subtitleType = SubtitleType.Unknown;
 	
+	
+	/* Public properties */
+	
 	/// <summary>The incomplete subtitles that were found when opening a file.</summary>
 	/// <remarks>This is only used when <see cref="IncludeIncompleteSubtitles" /> is set.</remarks>
 	public IncompleteSubtitleCollection IncompleteSubtitles {
@@ -96,6 +102,16 @@ public class SubtitleFactory {
 		set { inputFrameRate = value; }
 	}
 	
+	/// <summary>The file size limit for subtitle files, in bytes.</summary>
+	/// <remarks>Defaults to 1,000,000 bytes (1MB). Setting the value to -1 disables validation.</remarks>
+	public int MaxFileSize {
+		get { return maxFileSize; }
+		set { maxFileSize = value; }
+	}
+	
+	
+	/* Public methods */
+	
 	/// <summary>Creates new empty <see cref="Subtitles" />.</summary>
 	/// <returns>The newly created subtitles.</returns>
 	public Subtitles New () {
@@ -112,7 +128,14 @@ public class SubtitleFactory {
 	public Subtitles Open (string path){
 		SubtitleFormat format = null;
 		string text = String.Empty;
-		Encoding fileEncoding = null;	
+		Encoding fileEncoding = null;
+		
+		if (maxFileSize != -1) {
+			FileInfo info = new FileInfo(path);
+			if (info.Length > maxFileSize) {
+				throw new FileTooLargeException(String.Format("The file size ({0:n} bytes) is larger than the maximum limit ({1:n} bytes).", info.Length, maxFileSize));
+			}
+		}
 		
 		SubtitleInput input = new SubtitleInput(fallbackEncoding, subtitleType);
 		if (encoding == null) {
diff --git a/src/SubLib/Exceptions/FileTooLargeException.cs b/src/SubLib/Exceptions/FileTooLargeException.cs
new file mode 100644
index 0000000..416c8d7
--- /dev/null
+++ b/src/SubLib/Exceptions/FileTooLargeException.cs
@@ -0,0 +1,36 @@
+/*
+ * This file is part of SubLib.
+ * Copyright (C) 2011 Pedro Castro
+ *
+ * SubLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * SubLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using System;
+
+namespace SubLib.Exceptions {
+
+/// <summary>The exception that is thrown when a file is too large.</summary>
+public class FileTooLargeException : ApplicationException {
+	private static string defaultMessage = "The file is too large for a subtitle file.";
+
+	public FileTooLargeException (string message) : base(message) {
+	}
+	
+	public FileTooLargeException() : base(defaultMessage) {
+	}
+
+}
+
+}
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]