Permalink
Browse files

Workaround MySQL UTF-8 problem (only accepts chars up to 3 bytes)

  • Loading branch information...
1 parent 62c0243 commit 6d83703f22bcbb6f9d63d665317fe51601767145 @a-fung committed Sep 29, 2012
@@ -30,12 +30,13 @@ public override void HandleRequest(string jsonString, AjaxBase ajax)
}
AdminCollectionAddResponse response = new AdminCollectionAddResponse();
+ request.name = Utility.Remove4PlusBytesUtf8Chars(request.name);
if (!Collection.CheckNewCollectionName(request.name))
{
response.status = 1;
}
- else if ((request.path = Collection.CheckNewCollectionPath(request.path)) == null)
+ else if ((request.path = Collection.CheckNewCollectionPath(request.path)) == null || !Utility.IsValidStringForDatabase(request.path))
{
response.status = 2;
}
@@ -38,6 +38,8 @@ public override void HandleRequest(string jsonString, AjaxBase ajax)
}
else
{
+ request.name = Utility.Remove4PlusBytesUtf8Chars(request.name);
+
if (!Collection.CheckNewCollectionName(request.name))
{
response.status = 1;
@@ -32,7 +32,7 @@ public override void HandleRequest(string jsonString, AjaxBase ajax)
AdminMangaAddResponse response = new AdminMangaAddResponse();
- if ((request.path = Manga.CheckMangaPath(request.path)) == null)
+ if ((request.path = Manga.CheckMangaPath(request.path)) == null || !Utility.IsValidStringForDatabase(request.path))
{
response.status = 1;
}
@@ -51,6 +51,34 @@ public int Status
private set;
}
+ private IMangaProvider provider;
+
+ private IMangaProvider Provider
+ {
+ get
+ {
+ if (provider == null)
+ {
+ switch (MangaType)
+ {
+ case 0:
+ provider = new ZipProvider();
+ break;
+ case 1:
+ provider = new RarProvider();
+ break;
+ case 2:
+ provider = new PdfProvider();
+ break;
+ default:
+ throw new InvalidOperationException("Invalid MangaType");
+ }
+ }
+
+ return provider;
+ }
+ }
+
private Manga()
{
Id = -1;
@@ -61,6 +89,8 @@ public static Manga CreateNewManga(Collection collection, string path)
Manga newManga = new Manga();
newManga.ParentCollection = collection;
newManga.MangaPath = path;
+ newManga.MangaType = CheckMangaType(path);
+ newManga.View = newManga.Status = 0;
return newManga;
}
@@ -78,7 +108,7 @@ public static string CheckMangaPath(string path)
public static int CheckMangaType(string path)
{
- string extension = Path.GetExtension(path).ToLowerInvariant();
+ string extension = Utility.GetExtension(path).ToLowerInvariant();
if (Settings.UseZip && extension == ZipProvider.Extension && new ZipProvider().TryOpen(path))
{
@@ -22,7 +22,7 @@ private int GetNumberOfPages(string path)
string output;
int exitCode;
- ProcessLauncher.Run(Config.PdfinfoPath, path, out output, out exitCode);
+ ProcessLauncher.Run(Config.PdfinfoPath, "\"" + path + "\"", out output, out exitCode);
if (exitCode == 0)
{
@@ -29,7 +29,7 @@ public bool TryOpen(string path)
foreach (string fileName in fileNames)
{
- string extension = Path.GetExtension(Utility.CleanPath(fileName)).ToLowerInvariant();
+ string extension = Utility.GetExtension(fileName).ToLowerInvariant();
if (Constants.FileExtensionsInArchive.Contains(extension))
{
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Text;
using System.Web;
using Newtonsoft.Json;
@@ -35,6 +36,95 @@ public static string Md5(string input)
return s.ToString();
}
+ public static bool IsValidStringForDatabase(string str)
+ {
+ return str == Remove4PlusBytesUtf8Chars(str);
+ }
+
+ public static string Remove4PlusBytesUtf8Chars(string str)
+ {
+ byte[] bytes = Encoding.UTF8.GetBytes(str);
+ byte questionMark = Encoding.UTF8.GetBytes("?")[0];
+
+ int i = 0;
+ while (i < bytes.Length)
+ {
+ byte code = bytes[i];
+
+ if (code >= 0 && code < 128)
+ {
+ // valid one byte char
+ i++;
+ continue;
+ }
+ else
+ {
+ int extraBytes = 0;
+ if (code >= 192 && code < 224)
+ {
+ extraBytes = 1;
+ }
+ else if (code >= 224 && code < 240)
+ {
+ extraBytes = 2;
+ }
+ else if (code >= 240 && code < 248)
+ {
+ extraBytes = 3;
+ }
+ else if (code >= 248 && code < 252)
+ {
+ extraBytes = 4;
+ }
+ else if (code >= 252 && code < 254)
+ {
+ extraBytes = 5;
+ }
+
+ if (extraBytes > 0 && extraBytes < 3 && i + extraBytes < bytes.Length)
+ {
+ bool valid = true;
+ int j = 1;
+
+ while (j <= extraBytes)
+ {
+ byte code2 = bytes[i + j];
+ if (code2 >= 128 && code2 < 192)
+ {
+ }
+ else
+ {
+ valid = false;
+ break;
+ }
+ j++;
+ }
+
+ if (valid)
+ {
+ i += 1 + extraBytes;
+ continue;
+ }
+ }
+ else if (i + extraBytes >= bytes.Length)
+ {
+ extraBytes = bytes.Length - i - 1;
+ }
+
+ // invalid byte or longer than MySQL is accepting
+ byte[] newBytes = new byte[bytes.Length - extraBytes];
+ Array.Copy(bytes, 0, newBytes, 0, i);
+ newBytes[i] = questionMark;
+ Array.Copy(bytes, i + 1 + extraBytes, newBytes, i + 1, bytes.Length - i - 1 - extraBytes);
+ bytes = newBytes;
+ i++;
+ continue;
+ }
+ }
+
+ return Encoding.UTF8.GetString(bytes);
+ }
+
public static string GetFullPath(string path)
{
try
@@ -55,13 +145,21 @@ public static string GetFullPath(string path)
}
}
- public static string CleanPath(string path)
+ public static string GetExtension(string path)
{
- foreach (char c in Path.GetInvalidPathChars())
+ int index;
+ if (path == null)
+ {
+ return null;
+ }
+ if ((index = path.LastIndexOf(".")) == -1)
+ {
+ return string.Empty;
+ }
+ else
{
- path = path.Replace(new string(c, 1), string.Empty);
+ return path.Substring(index - 1);
}
- return path;
}
}
}
@@ -108,6 +108,7 @@
<p id="DeleteItemsConfirm">Are you sure to delete the selected item(s)?</p>
<p id="FileNotFound">File not found.</p>
<p id="PathNotUnderCollection">The specified path is not under the path of the selected collection.</p>
+<p id="InvalidFileType">The type of the specified file is invalid.</p>
<p id="Browse">Browse</p>
<p id="FileBrowser">File Browser</p>
<p id="Select">Select</p>
@@ -46,10 +46,6 @@ class Database
public static function Quote(str:String):String
{
- //if (Std.is(str, String))
- //{
- // str = Util.RemoveNonUtf8Chars(str);
- //}
return DefaultConnection().quote(str);
}
@@ -1,8 +1,10 @@
package afung.mangaWeb3.server;
+import afung.mangaWeb3.server.provider.IMangaProvider;
import afung.mangaWeb3.server.provider.PdfProvider;
import afung.mangaWeb3.server.provider.RarProvider;
import afung.mangaWeb3.server.provider.ZipProvider;
+import php.Exception;
import php.FileSystem;
import php.io.Path;
@@ -27,11 +29,45 @@ class Manga
public var Status(default, null):Int;
+ private var provider:IMangaProvider;
+
+ private var Provider(default, never):IMangaProvider;
+
+ private function get_Provider():IMangaProvider
+ {
+ if (provider == null)
+ {
+ switch(MangaType)
+ {
+ case 0:
+ provider = new ZipProvider();
+ case 1:
+ provider = new RarProvider();
+ case 2:
+ provider = new PdfProvider();
+ default:
+ throw new Exception("Invalid MangaType");
+ }
+ }
+
+ return provider;
+ }
+
private function new()
{
Id = -1;
}
+ public static function CreateNewManga(collection:Collection, path:String):Manga
+ {
+ var newManga:Manga = new Manga();
+ newManga.ParentCollection = collection;
+ newManga.MangaPath = path;
+ newManga.MangaType = CheckMangaType(path);
+ newManga.View = newManga.Status = 0;
+ return newManga;
+ }
+
public static function CheckMangaPath(path:String):String
{
path = FileSystem.fullPath(path);
Oops, something went wrong.

0 comments on commit 6d83703

Please sign in to comment.